in reply to Re^9: RFC: Large Floating Point Numbers - Rounding Errors
in thread RFC: Large Floating Point Numbers - Rounding Errors

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.