0

I experience different rounding of numbers in printf depending on conditions I don't understand.

printf('%1.0f\n',0.5)
printf('%1.1f\n',0.05)
printf('%1.2f\n',0.005)
printf('%1.3f\n',0.0005)
printf('%1.0f\n',1.5)
printf('%1.1f\n',1.05)
printf('%1.2f\n',1.005)
printf('%1.3f\n',1.0005)

Gives me the following output:

0
0.1
0.01
0.001
2
1.1
1.00
1.000

Why is 0.0005 rounded to 0.001, but 1.0005 to 1.000?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
ramden
  • 3
  • 1
  • 2
    What are you using, MATLAB or Octave? The two can have different behaviour. Also, what is the problem here, what don't you understand? Most likely, an attempt is made at rounding, but `1.0005` probably is something like `1.0004999999999` in binary representation. – Adriaan May 29 '20 at 11:14
  • https://www.h-schmidt.net/FloatConverter/IEEE754.html for playing with representation – matzeri May 29 '20 at 11:19
  • 1
    "Why is 0.0005 rounded to 0.001, but 1.0005 to 1.000?" --> Print using `printf('%.20f\n',constant_of_choice)` for greater insight. – chux - Reinstate Monica May 29 '20 at 14:44

0 Answers0