Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problems using modulo

Reply
Thread Tools

Problems using modulo

 
 
Griff
Guest
Posts: n/a
 
      04-19-2004
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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-19-2004
Griff wrote:

>
> 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.

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      04-19-2004
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

Griff wrote:

>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/



 
Reply With Quote
 
Brian Gough
Guest
Posts: n/a
 
      04-19-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Griff) writes:

> 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.

--
Brian Gough

Network Theory Ltd,
Publishing Python Manuals --- http://www.network-theory.co.uk/
 
Reply With Quote
 
=?iso-8859-1?q?Gonzalo_Sainz-Tr=E1paga_=28GomoX=29?=
Guest
Posts: n/a
 
      04-19-2004
On Mon, 19 Apr 2004 17:51:53 +0200, Diez B. Roggisch wrote:
> 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.


 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-20-2004
Gonzalo Sainz-Trápaga (GomoX) wrote:

> On Mon, 19 Apr 2004 17:51:53 +0200, Diez B. Roggisch wrote:
>> 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.


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).


--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: modulo seems not to work when using in index Pascal Peyremorte VHDL 0 12-23-2008 05:21 PM
[xsl] sort & modulo Tjerk Wolterink XML 3 04-22-2005 12:49 PM
Java ME modulo method ?? 900390d@acadiau.ca Java 1 05-19-2004 11:56 PM
Problems using modulo Griff Python 0 04-19-2004 05:45 PM
General modulo-question Christian Kruggel Java 1 07-07-2003 05:44 PM



Advertisments