number comparison problem

Dennis Lee Bieber wlfraed at ix.netcom.com
Tue Oct 15 23:58:14 EDT 2002


Chris Fonnesbeck fed this fish to the penguins on Tuesday 15 October 
2002 08:10 pm:

> 
>>>> print fb,fc
> 
> 0.132945911028 0.132945911028

        What do you get is you don't use "print", just the variable on the 
line.

> 
>>>> 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?
>
        What does

print fb - fc

show?

        Almost all texts covering numerical analysis with computers will 
emphasize that floating point operations are /not/ exact! The proper 
method of comparing floating point data is to first define an epsilon 
at which differences are to be ignored.

        Instead of

if fb == fc:

use

if (abs(fb - fc) < eps):        # eps selected to fit the problem domain


        For your example

print (fb - fc) > 0.0
 


--  
 > ============================================================== <
 >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
 >      wulfraed at dm.net     |       Bestiaria Support Staff       <
 > ============================================================== <
 >        Bestiaria Home Page: http://www.beastie.dm.net/         <
 >            Home Page: http://www.dm.net/~wulfraed/             <



More information about the Python-list mailing list