Problems using modulo

Mike C. Fletcher mcfletch at rogers.com
Mon Apr 19 11:59:09 EDT 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/






More information about the Python-list mailing list