Problems using modulo

G

Griff

Test program:
===============================================================
t = 5.8
interval = 2.0

while t < 6.1:
print "%s mod %s = %s " % (t, interval, t % interval )
t += 0.1
================================================================
Results:
5.8 mod 2.0 = 1.8
5.9 mod 2.0 = 1.9
6.0 mod 2.0 = 2.0 !!!!!!
6.1 mod 2.0 = 0.1

=================================================================

if we change t to initialise at 5.9 and run the program again,

=================================================================

t = 5.9
interval = 2.0

while t < 6.1:
print "%s mod %s = %s " % (t, interval, t % interval )
t += 0.1
=================================================================
Results:

C:\Python22>python modprob.py
5.9 mod 2.0 = 1.9
6.0 mod 2.0 = 0.0 # that's better

====================================================

I have tried this on Windows and Unix for Python versions between 1.52
and 2.3.

I don't know much about how Python does its floating point, but it
seems like a bug to me ?

What is the best workaround so that I can get my code working as
desired ?
(ie I just want to tell whether my time "t" is an exact multiple of
the time interval, 2.0 seconds in this case).

Would be grateful for any suggestions

cheers

- Griff
 
D

Diez B. Roggisch

Griff said:
Would be grateful for any suggestions

Try this:

t = 5.9
interval = 2.0

while t < 6.1:
print (t, interval, t % interval)
t+=0.1

Then you see what actually gets divided....
I'm not sure what you actually try to do, but AFAIK modulo is mathematically
only properly defined on integers - so you should stick to them. If you
need fractions, it might be sufficient to use fixed-point arithmetics by
simply multiplying by 10 ar 100.
 
M

Mike C. Fletcher

Use repr to see the true values you are dealing with when printing
floating point numbers (as distinct from the pretty "%s" formatter):

t = 5.8
interval = 2.0

while t < 6.1:
print "%r mod %r = %r " % (t, interval, t % interval )
t += 0.1

Then you'll see what's going on:

P:\temp>modulofloat.py
5.7999999999999998 mod 2.0 = 1.7999999999999998
5.8999999999999995 mod 2.0 = 1.8999999999999995
5.9999999999999991 mod 2.0 = 1.9999999999999991
6.0999999999999988 mod 2.0 = 0.099999999999998757

So, 5.99999... modulo 2.0 gives you 1.999999... as you would expect.

HTH,
Mike
Test program:
===============================================================
t = 5.8
interval = 2.0

while t < 6.1:
print "%s mod %s = %s " % (t, interval, t % interval )
t += 0.1
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
B

Brian Gough

Results:
5.9 mod 2.0 = 1.9
6.0 mod 2.0 = 2.0 !!!!!!
6.1 mod 2.0 = 0.1

I don't know much about how Python does its floating point, but it
seems like a bug to me ?

This is a common "feature" of floating-point arithmetic. 0.1 does not
have an exact machine representation in binary, so the numbers
displayed are not exact (e.g. 6.0 is actually 5.9999999... with a
difference O(10^-15)). There is an appendix in the Python tutorial
which discusses this, with more examples.
What is the best workaround so that I can get my code working as
desired ?
(ie I just want to tell whether my time "t" is an exact multiple of
the time interval, 2.0 seconds in this case).

For exact arithmetic, work with integers (e.g. in this case multiply
everything 10) or compute the difference from zero in the modulus, d,

s = t % interval
d = min (s, interval-s)

and compare it with a small tolerance, e.g. 1e-10 or something
appropriate to your problem. HTH.
 
?

=?iso-8859-1?q?Gonzalo_Sainz-Tr=E1paga_=28GomoX=29

AFAIK modulo is mathematically only properly defined on
integers - so you should stick to them.

Modulo is fine for any number (even irrational ones). For example,
acos(sqrt(3)/2) is congruent to pi/6 mod 2*pi. Technically, two numbers
are congruent mod n when the rest of the division by n is the same, it
doesn't matter whether n is an integer or not.
 
D

Diez B. Roggisch

Gonzalo said:
Modulo is fine for any number (even irrational ones). For example,
acos(sqrt(3)/2) is congruent to pi/6 mod 2*pi. Technically, two numbers
are congruent mod n when the rest of the division by n is the same, it
doesn't matter whether n is an integer or not.

Hum - usually, division in fields like Q or R doesn't yield a rest, doesn't
it? Thats what I meant. Of course if you define division as

a / b := subtract b from a until a is < b

and count the iterations, you can have modulo. So I stand corrected that the
modulo op in python then makes sense (even if numerical problems arise, but
thats a different topic).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top