This is for GCC/Linux:
#include <stdio.h> #include <stdlib.h> #include <fenv.h> double n[] = { 0.000005, 0.000015, 0.000025, 0.000035, 0.000045, 0.000 +055, 0.000065 }; char *str[] = { "0.000005", "0.000015", "0.000025", "0.000035", "0.000 +045", "0.000055", "0.000065" }; int main(int argc, char *argv[]) { int i, j; for (j = 0; j < 4; j++) { fesetround(j); printf("rounding: %d\n", j); double m = n[0]; for (i = 0; i < sizeof(n)/sizeof(*n); i++) { printf(" compiler: %40.30a => %40.30f\n", n[i], n[i]); printf(" atof: %40.30a => %40.30f\n", atof(str[i]), a +tof(str[i])); printf(" calc: %40.30a => %40.30f\n", m, m); m += 0.00001; } } return 0; }
rounding: 0 compiler: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 atof: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 calc: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 compiler: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 atof: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 calc: 0x1.f75104d551d6a00000000000000000p-17 => 0.0000 +15000000000000002074078756 compiler: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 atof: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 calc: 0x1.a36e2eb1c432e00000000000000000p-16 => 0.0000 +25000000000000004586175190 compiler: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 atof: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 calc: 0x1.2599ed7c6fbd300000000000000000p-15 => 0.0000 +35000000000000003710139834 compiler: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 atof: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 calc: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 compiler: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 atof: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 calc: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 compiler: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 atof: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 calc: 0x1.10a137f38c54400000000000000000p-14 => 0.0000 +65000000000000007858297346 rounding: 1 compiler: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 atof: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 calc: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 compiler: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 atof: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 calc: 0x1.f75104d551d6a00000000000000000p-17 => 0.0000 +15000000000000002074078756 compiler: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 atof: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 calc: 0x1.a36e2eb1c432e00000000000000000p-16 => 0.0000 +25000000000000004586175190 compiler: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 atof: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 calc: 0x1.2599ed7c6fbd300000000000000000p-15 => 0.0000 +35000000000000003710139834 compiler: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 atof: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 calc: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 compiler: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 atof: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 calc: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 compiler: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 atof: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 calc: 0x1.10a137f38c54400000000000000000p-14 => 0.0000 +65000000000000007858297346 rounding: 2 compiler: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 atof: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 calc: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 compiler: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 atof: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 calc: 0x1.f75104d551d6a00000000000000000p-17 => 0.0000 +15000000000000002074078756 compiler: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 atof: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 calc: 0x1.a36e2eb1c432e00000000000000000p-16 => 0.0000 +25000000000000004586175190 compiler: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 atof: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 calc: 0x1.2599ed7c6fbd300000000000000000p-15 => 0.0000 +35000000000000003710139834 compiler: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 atof: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 calc: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 compiler: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 atof: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 calc: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 compiler: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 atof: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 calc: 0x1.10a137f38c54400000000000000000p-14 => 0.0000 +65000000000000007858297346 rounding: 3 compiler: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 atof: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 calc: 0x1.4f8b588e368f100000000000000000p-18 => 0.0000 +05000000000000000409015270 compiler: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 atof: 0x1.f75104d551d6900000000000000000p-17 => 0.0000 +15000000000000000380012861 calc: 0x1.f75104d551d6a00000000000000000p-17 => 0.0000 +15000000000000002074078756 compiler: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 atof: 0x1.a36e2eb1c432d00000000000000000p-16 => 0.0000 +25000000000000001198043401 calc: 0x1.a36e2eb1c432e00000000000000000p-16 => 0.0000 +25000000000000004586175190 compiler: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 atof: 0x1.2599ed7c6fbd200000000000000000p-15 => 0.0000 +34999999999999996933876256 calc: 0x1.2599ed7c6fbd300000000000000000p-15 => 0.0000 +35000000000000003710139834 compiler: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 atof: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 calc: 0x1.797cc39ffd60f00000000000000000p-15 => 0.0000 +45000000000000002834104479 compiler: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 atof: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 calc: 0x1.cd5f99c38b04b00000000000000000p-15 => 0.0000 +55000000000000001958069124 compiler: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 atof: 0x1.10a137f38c54300000000000000000p-14 => 0.0000 +64999999999999994305770190 calc: 0x1.10a137f38c54400000000000000000p-14 => 0.0000 +65000000000000007858297346
And rounding is as follows:
  1. 0 - Rounding is towards 0.
  2. 1 - Rounding is towards nearest number.
  3. 2 - Rounding is towards positive infinity.
  4. 3 - Rounding is towards negative infinity.

In reply to Re^10: RFC: Large Floating Point Numbers - Rounding Errors by salva
in thread RFC: Large Floating Point Numbers - Rounding Errors by GAVollink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.