2

I'm only going to paste part of my code since it's very long, but I was wondering if any one might know potential causes of this problem. So I have this code here:

print "part a", working_weight
cells[working_cell_position][4] = working_weight
print "part b", working_weight, cells[working_cell_position][4], cells[working_cell_position]

and what it prints is this:

part a 62.4
part b 62.4 62.4 [6, 6, '', '', 62.400000000000006]

So if you didn't quite get it, basically I have the variable working_weight which is 62.4, but when I insert it into a list, it changes it to 62.400000000000006, yet if I only print that number from the list it prints as 62.4 still. If anyone could help, or suggest a solution to fix this it would be greatly appreciated.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3160749
  • 37
  • 1
  • 1
  • 2
  • 1
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – M4rtini Mar 15 '14 at 21:36
  • See also http://stackoverflow.com/questions/2986150/python-floating-number – Ffisegydd Mar 15 '14 at 21:36
  • 1
    Unfortunately, the question marked as a duplicate doesn't really answer this either. The reason for the *difference* between the two displays is that Python 2.x has *two* distinct built-in ways for formatting a float: `str`, which rounds to 12 significant figures, and `repr`, which prints as many digits as are needed to represent the value faithfully. `print` applied to a float by itself uses the `str`, while `print` of a list applies the `repr` to each item. – Mark Dickinson Mar 16 '14 at 09:16

3 Answers3

1

This is because floats are inherently imprecise in pretty much every language, as they cannot be represented easily in 64-bit binary at the lowest level. What is happening to your code has nothing to do with how it is put into the list or anything like that.

If you want to keep a precise decimal you should use decimal.Decimal.

>>> from decimal import Decimal
>>> working_weight = Decimal(str(working_weight))
>>> working_weight
Decimal('62.4')

This Decimal can then have operations performed on it like any float.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
0

Floating point math is tough, that's the problem. If you have decimals, use decimal.Decimal.

from decimal import Decimal

a = Decimal("30")
b = Decimal("32.4")
print(a+b)
# Decimal ('62.4')
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • What is the point of this answer when my answer was present first with the same idea? – anon582847382 Mar 15 '14 at 21:38
  • @AlexThornton A better question would be, why bother answering this at all, instead of finding one of the countless possible duplicates of this. – M4rtini Mar 15 '14 at 21:49
0

It's hard to say for sure without having a reproducible problem, but one way to get something similar is for example

import numpy
a = numpy.array([62.4], numpy.float)
print a[0]      # outputs 62.4
print [a[0]]    # outputs [62.399999999999999]

where is working_height coming from? What is working_height.__class__?

6502
  • 112,025
  • 15
  • 165
  • 265