0

Useful Functions: Numeric Functions

Say a number has accuracy upto 18 decimal places. If I try taking round of this number upto 16 decimal places or 17 decimal places, I get the same output and rounded number is upto 15 decimal places. why is that so? Also, say if I want to print the number upto 16 decimal places without rounding it off, what will be the syntax? =============================== In [82]: x=2.123456789123456789 In [83]: print(round(x,16)) 2.123456789123457 In [84]: print(round(x,17)) 2.123456789123457 ========================

7th Apr 2017, 7:35 AM
deepak deo
deepak deo - avatar
1 Answer
+ 4
Accuracy of floats is limited as they are exponent-wise stored. So don't be surprised to see your if statements become True when you compare numbers beyond that precision. More on this: https://en.m.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems
12th Apr 2017, 4:44 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar