Home python rounding numbers in python

rounding numbers in python [duplicate]

Author

Date

Category

why in python int (0.99999999999999) == 0 , and int (0.999999999999999) == 1 ?


Answer 1, Authority 100%

Because the accuracy of real numbers in your computer is limited.

@ & gt; & gt; & gt; f '{0.9999999999999999: .60g}'
'0.99999999999999988897769753748434595763683319091796875'
@ & gt; & gt; & gt; f '{0.99999999999999999: .60g}'
'1'

Both numbers cannot be imagined precisely in the form of binary fractions (and real numbers are stored in this form: M * 2 ^ E , M and E – whole). Python is looking for the best approximation. The more nines the closer is the approach to one. Sooner or later, one and it turns out.

p.s. int does not apply to the problem. The problem happens earlier when Python turns the line 0.99999999999999999 in the real number 1

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions