I want to find the remainder of 23.153 when divided by 23 i.e. 23.153%23. The remainder should come 0.153 but its coming as 0.1529999999999987.
Asked
Active
Viewed 94 times
0
-
Take a look at this guide: [What Every Programmer Should Know About Floating-Point Arithmetic](http://floating-point-gui.de/) – pault Nov 28 '17 at 17:57
-
If you always want the same level of precision, say 3 digits, you can get your numbers to print nicely with this: `float("{:0.3f}".format(23.153%23))`, but it's just a hack. – pault Nov 28 '17 at 18:00
-
@pault is this a modification of float specific to this answer or to whole program ? – Samneet Singh Wilkhoo Nov 28 '17 at 18:22
-
The _exact_ remainder as you asked can not be obtained in general (Please read the link I provided about floating point arithmetic to understand why). The hack I provided is just a way to force the precision to be what you specify. You format the number as a string to _n_ decimal places (in this case 3) and then convert it back into a string. – pault Nov 28 '17 at 19:05