Same calculation on Linux and Windows & # 8594; different results

I encoded the following algorithm to convert decimal to binary / hexadecimal, etc.

string toFormatFromDecimal(long long t, Format format) { int digitCount = ceil(log(t) / log((int) format)); string hex = ""; for (int i = 0; i < digitCount; i++) { long double cur = (long double)t / (long double)(format); long long ganzzahl = (long long) cur; long double kommazahl = cur - ganzzahl; hex += digits[(long long) (kommazahl * format)]; t = ganzzahl; } return string(hex.rbegin(), hex.rend()); } 

I use GCC on Linux and the Visual Studio C ++ compiler on Windows. It seems that I got different values ​​in the "integer" section here:

 long long ganzzahl = (long long) cur; 

Any idea how this could happen? Are there other restrictions for Linux and Windows?

Thanks Florian

- Decision -

 string toFormatFromDecimal(long long t, Format format) { int digitCount = ceil(log(t) / log((int) format)); string hex = ""; for (int i = 0; i < digitCount; i++) { hex += digits[(int) (t%format)]; t = t/format; } return string(hex.rbegin(), hex.rend()); } 
+4
source share
1 answer

Yes, GCC and Visual Studio C ++ have different long double types. When generating GCC code for x86, a long double is an 80-bit format with an extended IEEE 754 (*), while Visual Studio C ++ treats a long double as a 64-bit IEEE 754 format with double precision (*).

Thus, (long double)t does not have to be the same number on both platforms, and the division is not the same. Although you noted your question "integer-division", this is a floating point separation between different types of floating point.

(*) almost: it behaves very much like the 79-bit IEEE 754 type with 15 exponential bits and 63 significant bits, but it has a slightly wider range of exponents, since it uses an explicit bit for leading 1 in value.

(**) almost: since the compiler generates instructions that use historical x87 instructions after setting x87 for 53-bit values, denormal results can be double rounded ( link ).

+15
source

Source: https://habr.com/ru/post/1482656/


All Articles