jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
@jlm699: 2.2^2 = 4.84 (and not 4.840000000001, try it with a calculator or the google link you gave)
Yes, but that .000000000001 is obviously due to binary floating-point arithmetic.
>>> '%f' % 2.2**2
'4.840000'
>>> '%.2f' % 2.2**2
'4.84'
>>>
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
Most computer languages would give you the same problem. The computer tries to represent a floating point number as a binary internally. To compare floating point numbers you need to use fuzzy logic. Here as an function to use:
def fuzzyequals(a, b, delta=0.0000001):
"""
returns True if a is between b-delta and b+delta
used for comparison of floating point numbers a and b
"""
return abs(a-b) < delta
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212