How to get repeating decimal in C -
how can repeating decimal 0.99999999... in c ?
1.0/9.0 give me repeating decimal: 0.1111111, if use double = 1.0/9.0 * 9.0 result 1.0
thank you.
the problem running while 1/9 repeating decimal both in base 10 (0.1111..) , in base 16, (0.1c71c7..) when multiply 9 again, 1... far floating point routines concerned. way 0.9999 in c break repeated decimal subtracting little bit won't round 1. if want see 9s, need limit how gets printed out means other floating point formatting. need:
#include <stdio.h> #include <string.h> main() { double val=9; char disp[21]; val=1.0/9.0; val -= 0.0000000000000001; printf("%20.19f\n", val); val *=9; printf("%20.19f\n", val); sprintf(disp,"%20.19f",val); printf("%.15s\n", disp); } this gives:
0.1111111111111110078 0.9999999999999991118 0.9999999999999 on machine.
Comments
Post a Comment