lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.04k stars 420 forks source link

Doesnt compile anymore on VS #109

Closed pierreinglebert closed 4 years ago

pierreinglebert commented 4 years ago

While removing powf.h, you added some division by zero (line 299) that breaks when compiling with VS error C2124: divide or mod by zero

zvezdochiot commented 4 years ago

https://github.com/lvandeve/lodepng/blob/e17569276f9b6504cc4fc23e32fd52823d9e0082/lodepng_util.cpp#L297-L301

lvandeve commented 4 years ago

It's trying to return infinity or nan here.

Since this function should be C90 compatible, unfortunately the INFINITY or NAN macros are not available.

I'll look into finding a better way that's compatible with C90, visual studio and all other platforms.

lvandeve commented 4 years ago

Does commit 3e74e9f fix it?

zvezdochiot commented 4 years ago

Maybe?:

static const float lodepng_flt_inf = 0x7FF00000;
static const float lodepng_flt_nan = 0x7FFF0000;
lvandeve commented 4 years ago

That one would assign values 2146435072.0 and 2147418112.0 to the floats. With bit (aka reinterpret) casting that would work, but such bit cast has about as many portability issues than the division through 0.0 solution, if not more due to endianness and depending on exact IEEE bit representation.

zvezdochiot commented 4 years ago

@lvandeve say:

but such bit cast has about as many portability

Can just write an auxiliary function for checking conditions? And execute entire sections of code by condition?

lvandeve commented 4 years ago

If the current proposed solution works, it's simpler, platform dependent #ifdefs are a next step if the current idea doesn't fix it.

I'd just like to double check that the current solution fixes it for VS users: whether the static const lodepng_fltzero works, or if it should be not static and/or not const for VS to not give error.

lvandeve commented 4 years ago

Commit 6bef71b33f90f716a5f16047e2127439f622d1ba should fix it fully, closing the issue with this assumption