0

N00b here, in C I assign a value using

#include <stdio.h>

int main(){
    double test_num;
    test_num = 0.99999999;
    printf("%11.10f\t",test_num);
    printf("foo");
    return 1;
}

Here's the print output

0.9999999900    foo

Here's the value from debugger (Clion 2020.1, LLDB 9.0.1, Bundled compiler)

0.99999998999999994

Why are these numbers different? The compiled/runtime value seems to be the debugger value which is breaking my program

joshp
  • 706
  • 5
  • 22
  • Values are not different, the printout differs due to different roundings. – chux - Reinstate Monica May 22 '20 at 02:23
  • 1
    double is a "floating point" value, which is an approximation of a real number's value. You cannot expect any 2 floating point values to be equal to each other, either when printed out or compared in a program. The best you can expect is approximate equality – JoelFan May 22 '20 at 02:27
  • 1
    It is surprising that you get an 8 printed in the 8th decimal place from the debugger. But if you printed with `%.17f` (instead of `%111.10f`), you'd like see what the debugger shows. – Jonathan Leffler May 22 '20 at 02:29

1 Answers1

1

The closest IEEE 754 64-bit binary float to 0.99999999 is 0.99999998999999994975240724670584313571453094482421875. That is the value your program is actually working with. It differs from 0.99999999 by about 5e-17. Is that difference big enough to matter in your program?

0.99999998999999994 is the result of truncating to 17 significant digits. Rounding to 10 digits would get the printf result.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • Thanks! That makes sense, I'll have to check my calculations. I was reprogramming this from a Matlab app I have and am getting different values during the calculation. Unfortunately, I'm already doing compensation for numerical underflow and I'm unsure whether this inaccuracy will cause problems. Do you have any idea why Matlab (and Python) would show different values? – joshp May 22 '20 at 04:13
  • @joshp For small rounding error type differences, because they are doing the calculations with different grouping. Remember floating point addition and multiplication are not associative. For larger differences, probably because there is a bug in one of the programs. – Patricia Shanahan May 22 '20 at 07:01