简体   繁体   中英

Is floating point representation compiler-dependent in C++?

The question is in the title. It seems that the software I deliver to my customer has varying behaviors depending on wether some parameters passed as integers or as floats. I build a DLL for my customer with MinGW and he integrates it in his Visual Studio project, which uses some other compiler (no idea which, I guess the standard one for VS).

Could it be that floats are represented differently by him than by me ?

Thanks for the heads up, Charles

Yes, floating point representation is compiler dependent.

In theory you can use std::numeric_limits to determine the main aspects of the representation, such as whether it's IEEE 754, or whether it's binary or decimal.

In practice you can't rely on that except for the memory layout, because with a main compiler, g++, the semantics of floating point operations are influenced strongly by the optimization options (eg, whether NaN compares equal to itself or not).

Ie in practice it's not only compiler dependent but also option-dependent.

Happily compilers for a given platform will generally conform to that platform's standard memory layout for floating point, eg IEEE 754 in Windows (the standard originated on the PC platform). And so floating point values should in general work OK when exchanged between g++ and Visual C++, say. One exception is that with g++ long double maps to 80-bit IEEE 754, while with Visual C++ it maps to ordinary double , ie 64-bit, and that could conceivably be what makes trouble for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM