![]() Here are the double-precision floating-point approximations of the latter four values: ![]() The second four numbers are not exactly representable in binary, so are not halfway cases the tie-breaking rule does not apply. The first four numbers are exactly representable in binary, so are true halfway cases they should round according to the tie-breaking rule in effect. #ACTIVEPERL 5.12.2 CODE#(I modified the code to make the call to dtoa() in “mode 3” with ndigits=1.)įor some of the eight programming languages, I tried multiple compilers or interpreters and ran on different operating systems, resulting in nineteen test environments. To serve as a metric of sorts, I also tried David Gay’s g_fmt() function, the formatting wrapper for his floating-point to decimal conversion routine dtoa(). I formatted two-digit decimal numbers as one-digit decimal numbers, as indicated in this table (the variable ‘d’ represents a double-precision variable):Įxplicit Formatting of Floating-Point Numbers to One Decimal Place Language I tested the rounding of printed floating-point numbers in C, Java, Python, Perl, Visual Basic, PHP, JavaScript, VBScript, and Calc. I also discovered an anomaly in some environments: numbers like 0.15 - which look like halfway cases but are actually not when viewed in binary - may be rounded incorrectly. In all, I tested fixed-format printing in nineteen environments: in thirteen of them, round-half-away-from-zero was used in the remaining six, round-half-to-even was used. This inconsistency of printed output is not limited to C - it spans many programming environments. Visual C++ uses the round-half-away-from-zero rule, and gcc (actually, glibc) uses the round-half-to-even rule, also known as bankers’ rounding. The two-digit number 0.25, which has an exact binary floating-point representation, is equally near two one-digit decimal numbers: 0.2 and 0.3 either is an acceptable answer. The compilers - actually, their run time libraries - are using different rules to break decimal rounding ties. ![]() If you compile the program with Visual C++ and run on it on Windows, it prints 0.3 if you compile it with gcc and run it on Linux, it prints 0.2. The answer depends on which compiler you use. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |