954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with power in python 3.1

hi,

i just start to learn and doing programming with python (was an old timer programmer in other languages). when i saw a project idea about creating your own squareroot function, i tried it.

the problem start was when i try to compare the result of

x**2

where x is not an integer. one example that i tried is:

2.2**2 resulting 4.840000000000001

which we all know that is incorrect. i also tried using the math.pow function, but the result is the same.

can anyone help me to solve this problem? thanks.

P.S. I also tried it on python 2.6.2 which results is even more strange (4.8400000000000007)

Lingson
Light Poster
27 posts since Aug 2009
Reputation Points: 24
Solved Threads: 1
 

its because you have a floating point number, im not sure exactly how to fix it but at least you now know the problem

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

2.2**2 resulting 4.840000000000001

which we all know that is incorrect.


Why is that incorrect? http://www.google.com/search?q=2.2^2

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
 

You can use the decimal module if you want very, very precise results http://docs.python.org/library/decimal.html#quick-start-tutorial
One of the hits from a search on this forum
http://www.daniweb.com/forums/thread95473.html

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

@woooee: Thanks for the info. Exactly what I needed. After twisting with the code and struggling with the syntax, I managed to solve the problem.

@jlm699: 2.2^2 = 4.84 (and not 4.840000000001, try it with a calculator or the google link you gave)

Lingson
Light Poster
27 posts since Aug 2009
Reputation Points: 24
Solved Threads: 1
 
@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
 

yes.. that im aware of that.. but that wasnt my question to the problem. the question was how to solve it because i needed the exact result for my calculation in the program.

but the problem is solved anyway.

Lingson
Light Poster
27 posts since Aug 2009
Reputation Points: 24
Solved Threads: 1
 

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
 

even though i solved the problem with the Decimal() function, and i prefer to solve a problem directly instead of going around a problem, but the fuzzy logic approach is interesting and gave me another point of view. Thanks.

Lingson
Light Poster
27 posts since Aug 2009
Reputation Points: 24
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You