0

I have problem with some floats in python.Some float number when I multiply( or subtracting) they get wrong answer.I understand that binary code can't store this type of numbers and he convert it that number which he can store and is nearest to first number.But none say about it how fix it .

How get real number?

I tried round it but it doesn't work it also get wrong number.

THIS IS SAMPLE CODE WHAT I MEAN:

float_1=1.2
float_2=1.4
float_3=1.5
print(float_1*float_2)
#this print 1.68 and this is True
print(float_2*float_3)
#But this print 2.0999999999999996 and this is False it must print  2.1
Community
  • 1
  • 1
  • try the `fractions` module, which can store the exact value of any rational number. – Kevin Sep 17 '14 at 18:23
  • `print(round(1.4*1.5,6))` gives the answer you are looking for. – Robᵩ Sep 17 '14 at 18:28
  • @Robᵩ did they change the way `str(float)` or `print(float)` works in Python 3? In Python 2.7 I get the expected result, `str` doesn't give as many digits as `repr` and performs rounding on the dropped digits. Never mind, I tested it for myself, going to open a question now. – Mark Ransom Sep 17 '14 at 18:46
  • I get the same answer in python2.7.6 and python3.4.0. – Robᵩ Sep 17 '14 at 19:15

0 Answers0