Windows/Linux上巨大的printf浮点数/整数位数的双精度差 [英] huge printf float/double difference in integer digits on windows/linux
问题描述
#include <float.h>
#include <stdio.h>
int main(int argc, char** argv)
{
printf("[0] %f\n", FLT_MAX);
printf("[1] %lf\n", FLT_MAX);
printf("[2] %Lf\n", FLT_MAX); // gcc warning: expects argument of type ‘long double’
printf("[3] %f\n", DBL_MAX);
printf("[4] %lf\n", DBL_MAX);
printf("[5] %Lf\n", 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打印描述:Ubuntu 15.04"gcc --version显示"gcc(Ubuntu 4.9.2-10ubuntu13)4.9.2"ldd --version显示"ldd(Ubuntu GLIBC 2.21-0ubuntu4)2.21"
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(最新版本10.0.40219.1 SP1Rel)Debug/Win32
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
FLT_MAX的差异VS2010:340282346638528860000000000000000000000.000000GCC4.9.2:340282346638528859811704183484516925440.000000
difference on FLT_MAX VS2010: 340282346638528860000000000000000000000.000000 GCC4.9.2: 340282346638528859811704183484516925440.000000
是1.8829581651548307456e + 20(不是那么小)-并且使用双打变得越来越大
is 1.8829581651548307456e+20 (not that small) - and getting much bigger using doubles
更新:实际问题
是否有一种方法(只需稍作更改即可)在Linux和Windows(及其他)上获得相同的结果,还是我需要在所有系统上使用完全相同的实现?我担心自己在Windows/Linux/Linux-ARM/VxWorks/Solaris平台上有自己的实现.
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.
推荐答案
printf
函数在这些平台上的实现方式有所不同.
The printf
function is implemented differently on these platforms.
看下面的代码:
#include <stdio.h>
int main()
{
printf("%lf\n", ((double)1e100)/3);
return 0;
}
此使用VC ++编译的程序提供:
This program compiled with VC++ gives:
3333333333333333200000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
而用g ++编译的同一程序给出:
while the same program compiled with g++ gives:
3333333333333333224453896013722304246165110619355184909726539264904319486405759542029132894851563520.000000
这篇关于Windows/Linux上巨大的printf浮点数/整数位数的双精度差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!