[Tutor] Range of float value
Daniel Yoo
dyoo at cs.wpi.edu
Thu Feb 8 23:23:04 CET 2007
On Thu, 8 Feb 2007, Johan Geldenhuys wrote:
> OK, this what I wanted:
>
> I have a value: a = 48.41
>
> My lowValue is: lowValue = 48.35
> My highValue is : highvalue = 48.45
Range does not work on floats: it's meant to work on integers.
> I though that it could be possible to have a range between 48.35 and 48.45
> that could have a step of 0.1
There's an unsafe assumption here: in general, comparing floats for
equality won't work unless you are very very careful. See:
http://www.python.org/doc/tut/node16.html
Because floats aren't directly comparable (at least under normal cases),
that negates the idea of build a list of floats and comparing for equality
against one of them.
However, Kent mentioned a solution that should work better, a chained
comparison:
a <= b <= c
which is true if b is squeezed between a and b.
More information about the Tutor
mailing list