in reply to Decimal precision issue: Windows vs. Unix
Not to detract from the general point that you should never rely on the exact value of a floating point number...
...I am increasingly of the opinion that this demonstates a fault in the Windows library.
When I try it on my Windows XP 32-bit perl 5.10.0, and on my Linux 64-bit perl 5.10.0, the result of the sum is the same, to whit an IEEE 754 double 0xC057_CFA3_AEE5_258A, which is approximately -95.244365428710949572632671333849
It appears that when stringifying floating value in IEEE 754 double form, Perl is working to 15 significant decimal digits. This is the usual value -- chosen because this ensures that conversion from decimal to binary and back again gives the original decimal value. Under Windows the perl I have is using sprintf(b, "%.*g", 15, x), where under Linux it is using gcvt(x, 15, b) -- at least that is what use Config tells me d_Gconvert is set to.
Anyway, as you know, the results are:
which demonstrates that the Linux value is 'correctly rounded' to 15 significant decimal digits, while the Windows value... isn't. IEEE 754 requires correct rounding for numbers of this sort of size.-95.2443654287109 -- Linux -95.244365428710949572632671333849 ~ true value -95.244365428711 -- Windows
One of the tricky things about binary to decimal conversion is: to do it right you have to delay a rounding step until after you've generated the decimal value, and then round to the required number of decimals. It is reasonable to limit the conversion of IEEE 754 doubles to 17 significant decimal digits -- because that is sufficient for binary to decimal and back to binary to give the original value, for reasonable size values.
You can see that when rounded to 17 significant digits the value is -95.244365428710950, rounding that to 15 digits gives -95.244365428711. I observe that sprintf('%24.20f', ...) under Windows gives zeros after the 17th significant digit...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Decimal precision issue: Windows vs. Unix
by BrowserUk (Patriarch) on Jan 10, 2009 at 21:04 UTC | |
by gone2015 (Deacon) on Jan 11, 2009 at 00:25 UTC |