a % b == b when a is a very small negative number

Ludmila Druker ledruker at rogers.com
Sun Nov 24 12:56:28 EST 2002


"Lee Harr" <missive at frontiernet.net> wrote in message
news:uu1uocd4db4be5 at corp.supernews.com...
>
> I have some code which is storing the direction an object
> is moving, and I want to keep that value: 0 <= direction < 2*PI
...
> This works fine until direction is some very very small negative number,
> like -0.1e-16.
...
> Is this not a proper use of the % operator?
> Should not a % b always be less than b?

Let b > 0. The operation a%b must result in 0 <= x < b.
It can be imagined as a snippet

x = a
while x >= b: x -= b       # b=2*pi
while x < 0.0: x += b     # your case

The comparison of x with 0.0 has no problem.
The addition of 2*pi for small number results to 2*pi.
Use:
if a+2*math.pi == 2*math.pi: print '=='
else: print '<>'
as a test. So, you have what you want.

ED







More information about the Python-list mailing list