Problems using modulo

Discussion in 'Python' started by Griff, Apr 19, 2004.

  1. Griff

    Griff Guest

    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
    Griff, Apr 19, 2004
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Apr 19, 2004
    #2
    1. Advertising

  3. 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/
    Mike C. Fletcher, Apr 19, 2004
    #3
  4. Griff

    Brian Gough Guest

    (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/
    Brian Gough, Apr 19, 2004
    #4
  5. 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.
    =?iso-8859-1?q?Gonzalo_Sainz-Tr=E1paga_=28GomoX=29, Apr 19, 2004
    #5
  6. 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
    Diez B. Roggisch, Apr 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Kruggel

    General modulo-question

    Christian Kruggel, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    438
    Brad BARCLAY
    Jul 7, 2003
  2. Replies:
    1
    Views:
    652
    Darryl L. Pierce
    May 20, 2004
  3. Tjerk Wolterink

    [xsl] sort & modulo

    Tjerk Wolterink, Apr 21, 2005, in forum: XML
    Replies:
    3
    Views:
    556
    Dimitre Novatchev
    Apr 22, 2005
  4. Griff

    Problems using modulo

    Griff, Apr 19, 2004, in forum: Python
    Replies:
    0
    Views:
    284
    Griff
    Apr 19, 2004
  5. Pascal Peyremorte
    Replies:
    0
    Views:
    433
    Pascal Peyremorte
    Dec 23, 2008
Loading...

Share This Page