array precision

Emile van Sebille emile at fenx.com
Fri Feb 8 11:29:17 EST 2002


"Hugo Martires" <hugomartires at hotmail.com> wrote in message
news:d0380df9.0202080516.7a76905d at posting.google.com...
[testing floating point problem]
>
> I know that the problem is in the internal storage of the numbers, but
> i canot understand how to solve that.
> I just want to make a simple calculation and an if statement.
>
> Tanks for help

Perhaps one of these may help:

>>> '%.2f' % myArray[1]
'0.60'


class tolerate:
    def __init__(self, val, giveortake):
        self.hi = val + giveortake
        self.lo = val - giveortake
    def __eq__(self, other):
        return self.lo <= other <= self.hi

for a in (.595, .599, .6, .601, .605):

    if a == tolerate(.6, .001):
        print '\n\n',a, "OK"
    else:
        print '\n\n',a, "not OK"

because-round(.6,2)-is-0.59999999999999998-ly y'rs,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list