0

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

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Charles
  • 988
  • 1
  • 11
  • 28
  • related: http://stackoverflow.com/questions/18494237/floating-point-mismatch-between-compilers-visual-studio-2010-and-gcc – NathanOliver Sep 19 '16 at 14:54
  • 3
    In short: Yes it is. – πάντα ῥεῖ Sep 19 '16 at 14:54
  • Generally, linking code compiled with different compilers is not a good idea - there's no guarantee it will work correctly. You should usually always build everything with the *exact* same compiler. – Jesper Juhl Sep 19 '16 at 14:55
  • All Windows compilers are going to use the same representation for `float`, or for `double` -- the one used by the platform. `long double` does vary. Passing a `float` to a function that expects an `int` and vice versa will cause all manner of problems. – Ben Voigt Sep 19 '16 at 14:56
  • @JesperJuhl: If the function signatures are C-compliant, there's no problem. C++ objects can't be trivially passed around, however. – Ben Voigt Sep 19 '16 at 14:57
  • @Jesper Juhl Great, gotta thank my architect for telling me that beforehand. Oh wait, there is no architect in my project. That's why. – Charles Sep 19 '16 at 14:57
  • @Ben Voigt I know. That's why I used words like "generally" and "usually". In any case, if you just follow the rule of thumb that is "always build everything with the exact same compiler" then you'll never run into (certain kinds of) trouble - which was my main point :) – Jesper Juhl Sep 19 '16 at 14:59
  • @JesperJuhl: But it simply isn't true... what you're saying is that you cannot have any application that is not built with the exact same compiler version that the OS team used, because you are *constantly* calling into Win32 DLLs. Languages other than C would be completely impossible. In reality, foreign function interfaces are a thing, and C-compatible signatures are perfectly usable across different compiler vendors, versions, and languages. – Ben Voigt Sep 19 '16 at 15:32
  • @Ben Voigt I think you know exactly what I'm saying - we both know there's a stable C ABI but not a C++ one. We both know that *iff* you stick to what is guaranteed to be binary compatible you can use whatever compiler. *But* we also both know that sticking to the guaranteed subset can sometimes be tricky (especially for non-expert users), so "use the same compiler for all your code" is a useful guideline that prevents a lot of pain. Why do you argue that it is not? – Jesper Juhl Sep 19 '16 at 18:38
  • @JesperJuhl: Designing a system that must be wholly built using a single compiler is incredibly short-sighted and dangerous. Let me lay out the abbreviated explanation, and you can let me know which step you don't understand. (1) The system is built using a single compiler, ergo (2) The system relies on being built using a single compiler, ergo (3) The system cannot be built using any later compiler version, ergo (4) Components cannot be updated to their latest versions, ergo (5) The system has bugs and security vulnerabilities. QED – Ben Voigt Sep 19 '16 at 23:44

1 Answers1

4

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 (e.g., whether NaN compares equal to itself or not).

I.e. 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, e.g. 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, i.e. 64-bit, and that could conceivably be what makes trouble for you.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331