0

I am using Python for reading a file and converting numerical value written as string to float. I observe a weird conversion:

 a="-5.970471694E+02"

 b = float(a)
 b
 >> -597.0471694

 bb = np.float64(a)
 bb
 >> -597.04716940000003

 e="-5.970471695E+02"
 ee = np.float64(e)
 ee
 >> -597.0471695

 ee-bb
 >> -9.9999965641472954e-08

What is the reason of the term "0000003" at the end of bb. Why I don't observe the same thing for ee. Is this really a problem? I think this issue is due to the floating-point accuracy but the result seems to be perturbed before I start to use the variables...

Guuk
  • 505
  • 3
  • 17
  • 3
    something seems wrong with your example >>> a="-5.970471694E+09" >>> b = float(a) >>> b -5970471694.0 >>> is what I get... addendum, I am using python 3.4.1 what version are you using? –  Jan 04 '16 at 10:27
  • In your example, `b` should be `-5970471694.0`, not `-597.04716940000003`. – acdr Jan 04 '16 at 10:28
  • @DanPatterson yes yes.... I correct it !! Sorry – Guuk Jan 04 '16 at 10:30
  • 1
    in python 3.4.1 and numpy version 1.9.0 ... >>> a="-5.970471694E+02" >>> bb = np.float(a) >>> bb -597.0471694 >>> but I still get bb-ee = 9.9999965641472954e-08 which is floating point representation –  Jan 04 '16 at 10:45
  • Duplicate of http://stackoverflow.com/questions/8215437/floating-point-accuracy-in-python? Also read [this](https://docs.python.org/2/tutorial/floatingpoint.html) – Reti43 Jan 04 '16 at 11:10

2 Answers2

2

What is the reason of the term "0000003" at the end of bb. Why I don't observe the same thing for ee.

b and bb have identical values (try evaluating b == bb). The difference comes down to how they are represented by the interpreter. By default, numpy floats are displayed with 8 digits after the decimal place, whereas Python floats are printed to 13 significant digits (including those before the decimal place).

Is this really a problem?

Since the actual values of b and bb are identical then the answer is almost certainly no. If the display differences bother you, you can use np.set_printoptions to control how numpy floats are represented in the interpreter. If you use IPython, you can also use the %precision magic to control how regular Python floats are printed.

ali_m
  • 71,714
  • 23
  • 223
  • 298
  • Ok thank you! As I expected this "issue" (not a real one) is the same as we observe with many scientific softwares and that can lead to some round-off errors. – Guuk Jan 04 '16 at 12:02
  • Don't fall into the trap of mistaking the number of *displayed* digits for the precision of the underlying float value. On a 64 bit machine, `float` and `np.float64` will both be 64 bit. Regardless of whether you display them with 1 or 100 decimal places they will still have exactly the same underlying precision, and will show the same degree of rounding error when you use them in calculations. – ali_m Jan 04 '16 at 12:05
  • Yes I agree with you. The use of the `decimal`module can help to see the decimals – Guuk Jan 04 '16 at 12:11
-1

Both float and float64 use a binary representation of the number. Both have to save the approximation that is caused by conversion from the number with base 10 to the number with base 2. The float uses less bits, so the error is greater and it is made visible when the a is copied to b. That is because b takes the a including the rounding error without the loss of information, and the a contains that 000..03 value. In other words, it is a rounding error from converting a decimal number to a binary number.

pepr
  • 20,112
  • 15
  • 76
  • 139