5

I have coded following algorithm to convert a decimal value into 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 in linux and Visual Studio c++ compiler on Windows It seems that i got different values at the "integer" Division here:

long long ganzzahl = (long long) cur;

Any Idea how this could happen? are there different precissions on Linux and Windows?

Thanks Florian

--Solution--

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());
}
user2071938
  • 2,055
  • 6
  • 28
  • 60

1 Answers1

17

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

So (long double)t does not have to be the same number on both platforms, and the division is not the same either. Although you have tagged your question “integer-division”, it is a floating-point division between different floating-point types.

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

(**) almost: because the compiler generates instructions that use the historical x87 instructions after having configured the x87 for 53-bit significands, denormal results may be double-rounded (reference).

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • Thank you very much! Is there a oppurtunity to get the same results on both platforms? Acctually I convert a double into 64 bit IEEE 754 where I get the same results on both platforms, but when I try to convert these long long into Hexadecimal with the given algorithm I get different results – user2071938 May 25 '13 at 09:40
  • 1
    @user2071938 To make the program work the same on both platforms, avoid floating-point completely. Your computation `kommazahl = cur - ganzzahl … kommazahl * format` looks like a complicated way of avoiding the remainder operator `%` between integers: http://stackoverflow.com/q/7070346/139746 – Pascal Cuoq May 25 '13 at 09:46
  • thank you very much, this solved my problem! I posted my adapted algorithm in my Question – user2071938 May 25 '13 at 10:37
  • 1
    @user2071938 I think that you can write `(long long) (((t%format)/double(format)) * format)` simply `t % format`. – Pascal Cuoq May 25 '13 at 10:40