number comparison problem

Manfred Babenhauserheide ea1847 at fen-net.de
Wed Oct 16 06:06:04 EDT 2002


Chris Fonnesbeck wrote:
> 
> I am using python to code an optimization function that requires
> numbers to be compared to one another, as is common in many
> algorithms.  However, the comparison operators (<,>,<=,>=,==) seem not
> to be working properly.  Regard the following:
> 
> >>> print fb,fc
> 
> 0.132945911028 0.132945911028
> 
> >>> print fb>fc
> 
> 1
> 
> These numbers look the same to me; what do I have to do to be able to
> test numbers accurately in python?

Never assume that two floating point numbers are equal!
Use (fb-fc)>eps for ">"
and abs(fb-fc)<eps for "=="
Choose a proper eps.

Kind regards
Manfred


-- 
"Inside every intelligent person is a dumb person 
trying to get out."    - A Prairie Home Companion



More information about the Python-list mailing list