Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

#include <float.h> 
#include <stdio.h> 

int main(int argc, char** argv) 
{ 
  printf("[0] %f
", FLT_MAX); 
  printf("[1] %lf
", FLT_MAX); 
  printf("[2] %Lf
", FLT_MAX); // gcc warning: expects argument of type     ‘long double’ 
  printf("[3] %f
", DBL_MAX); 
  printf("[4] %lf
", DBL_MAX); 
  printf("[5] %Lf
", DBL_MAX); // gcc warning: expects argument of type     ‘long double’ 

  //using C++ und std::numeric_limits<float/double>::max() gives same     results

  return 0; 
} 

Linux: x64 lsb_release -d prints "Description: Ubuntu 15.04" gcc --version prints "gcc (Ubuntu 4.9.2-10ubuntu13) 4.9.2" ldd --version prints "ldd (Ubuntu GLIBC 2.21-0ubuntu4) 2.21"

[0] 340282346638528859811704183484516925440.000000 
[1] 340282346638528859811704183484516925440.000000 
[2] --> warning-line disabled 
[3] 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 
[4] 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 
[5] --> warning-line disabled

Windows 7 x64: VS2010 (latest Version 10.0.40219.1 SP1Rel) Debug/Win32

[0] 340282346638528860000000000000000000000.000000 
[1] 340282346638528860000000000000000000000.000000 
[2] 340282346638528860000000000000000000000.000000
[3] 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 
[4] 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 
[5] 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000    

difference on FLT_MAX VS2010: 340282346638528860000000000000000000000.000000 GCC4.9.2: 340282346638528859811704183484516925440.000000

is 1.8829581651548307456e+20 (not that small) - and getting much bigger using doubles

UPDATE: actual question

Is there a way (with only a small change of the code) to get the same result on Linux and Windows (and others) or do I need to use the very same implementation on all systems? I'm afraid of having my own implementation for my Windows/Linux/Linux-ARM/VxWorks/Solaris platforms.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

The printf function is implemented differently on these platforms.

Look at this code:

#include <stdio.h>

int main()
{
    printf("%lf
", ((double)1e100)/3);
    return 0;
}

This program compiled with VC++ gives:

3333333333333333200000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000

while the same program compiled with g++ gives:

3333333333333333224453896013722304246165110619355184909726539264904319486405759542029132894851563520.000000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...