NO

Author Topic: Conversions  (Read 4086 times)

albertn

  • Guest
Conversions
« on: March 27, 2007, 04:17:00 PM »
Hello!

(Note that the following could perhaps have been placed under "General discussions" since I'm not sure if it is a bug!)

The following code

Code: [Select]
#include <stdio.h>
#include <math.h>

int main(void)
{
    printf("%f\n", sqrt(4.0L));
    printf("%lf\n", sqrt(4.0L));
    printf("%Lf\n", sqrt(4.0L));
    return 0;
}

produces differing ouputs in different compilers. (See below...)
Does anyone know if the C99 standard specifies what the exact ouput should be?


Kind regards,
Albert


Pelles C Ouput:
2.000000
2.000000
2.000000

Lcc-win32 Output:
0.000000
0.000000
2.000000

GCC Output (gcc from cygwin: "gcc -v" says:version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125))
2.000000
2.000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 and so on 0000000000000.000000


Synfire

  • Guest
Re: Conversions
« Reply #1 on: March 27, 2007, 05:45:31 PM »
Pelles C Ouput:
2.000000
2.000000
2.000000

I prefer this myself as it's consistant and is at least giving a result every time.

Lcc-win32 Output:
0.000000
0.000000
2.000000

I can only assume that Lcc-win32 is expecting some form of explicit typecast for the first two versions to display a value. It's been a long time since I've used Lcc-win32 but I do remember that, in a lot of instances, you needed to typecast almost everything to get proper results.

GCC Output (gcc from cygwin: "gcc -v" says:version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125))
2.000000
2.000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 and so on 0000000000000.000000

GCC always tends to go it's own way. As mentioned time and time again, GCC doesn't even try to stick with C99's standard, they mearly try to be backwards compatible with their older releases. If you are going to use PellesC, LCC, Lcc-Win32, or the like. I don't suggest comparing them to the GCC line of compilers. I made that mistake myself when I first started using PellesC. I was a long time GCC user on GNU/Linux systems, but as Pelle pointed out to me when I was complaining about a "missing feature" in the macro system of PellesC. GCC really doesn't follow the standards at all. So in the case of this GCC output. I would say that "%Lf\n" was probably supposed to display 2.000000 also but due to the hack-ish way GCC is designed in the first place it's not getting the correct answer.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Conversions
« Reply #2 on: March 28, 2007, 05:15:23 PM »
One thing that should matter here is that in Pelles C, long double is same type as double, for Microsoft compatibility. C99 recommends that long double should match the IEC 60559 extended format, but an implementation is free to use a non-IEC 60559 extended format, or the same format as double.
/Pelle