DIV and MOD 
Author Message
 DIV and MOD

I need to use some function like DIV (returns the integer part of a
division) and MOD (returns the reminder of a division) in python, but I
don't know how to do that. % does not work as expected (in this context)
and in the MATH module, I didn't find any kind of function to do such thing.

Can anyone help me ?



Fri, 14 Jan 2005 22:21:47 GMT  
 DIV and MOD
  Math module isn't necessary. Just use the '/' and '%' operators
as necessarily. Why does % not work properly in this content?

Python 2.1.3 (#1, Apr  9 2002, 20:09:43)
[GCC 2.95.3 20010315 (release)] on linux2
Type "copyright", "credits" or "license" for more information.

Quote:
>>> 4 / 3
>>> 1
>>> 5 % 3
>>> 2

                    -- Mike

Quote:

> I need to use some function like DIV (returns the integer part of a
> division) and MOD (returns the reminder of a division) in python, but I
> don't know how to do that. % does not work as expected (in this context)
> and in the MATH module, I didn't find any kind of function to do such thing.

> Can anyone help me ?

> --
> http://mail.python.org/mailman/listinfo/python-list

`-> (m.laranjeira)

--
Michael Gilfix

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html



Fri, 14 Jan 2005 22:27:28 GMT  
 DIV and MOD

    Marcus> I need to use some function like DIV (returns the integer
    Marcus> part of a division) and MOD (returns the reminder of a
    Marcus> division) in python, but I don't know how to do that. %
    Marcus> does not work as expected (in this context) and in the
    Marcus> MATH module, I didn't find any kind of function to do such
    Marcus> thing.

When you say it did not work as expected, what did you want, what did
you try, and what did you get?

Assuming you are working with integers/longs,  then

Quote:
>>> i = 13
>>> j = 5
>>> i/j     # DIV
2
>>> i%j     # MOD

3

Note that in future pythons, The / operator will no longer be integer
(floor) division.  See http://www.python.org/peps/pep-0238.html.

John Hunter



Fri, 14 Jan 2005 22:32:20 GMT  
 DIV and MOD
  Yes, but no need to worry until 3.x. And with 3.x, many changes will
be expected anyway :)

                 -- Mike

Quote:

> Note that in future pythons, The / operator will no longer be integer
> (floor) division.  See http://www.python.org/peps/pep-0238.html.

--
Michael Gilfix

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html



Fri, 14 Jan 2005 22:48:16 GMT  
 DIV and MOD

Quote:
> I need to use some function like DIV (returns the integer part of a
> division) and MOD (returns the reminder of a division) in python, but I
> don't know how to do that. % does not work as expected (in this context)
> and in the MATH module, I didn't find any kind of function to do such
thing.

Perhaps you are looking for divmod()? No need for math modules in that case.

Quote:
>>> divmod(334, 5)
(66, 4)

regards
----------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------


Fri, 14 Jan 2005 23:31:37 GMT  
 DIV and MOD

Quote:
> don't know how to do that. % does not work as expected (in this context)

Why do you say that?   / and % give exactly the results I'd expect:

Quote:
>>> for i in range(10): print '%d / %d is %d; %d %% %d is %d' %

(i,3,i/3,i,3,i%3)

0 / 3 is 0; 0 % 3 is 0
1 / 3 is 0; 1 % 3 is 1
2 / 3 is 0; 2 % 3 is 2
3 / 3 is 1; 3 % 3 is 0
4 / 3 is 1; 4 % 3 is 1
5 / 3 is 1; 5 % 3 is 2
6 / 3 is 2; 6 % 3 is 0
7 / 3 is 2; 7 % 3 is 1
8 / 3 is 2; 8 % 3 is 2
9 / 3 is 3; 9 % 3 is 0



Fri, 14 Jan 2005 23:54:05 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. DIV and MOD

2. Query: numeric_std: div,rem,mod

3. MOD/DIV in standard Pascal

4. DIV, MOD, /, REM (M2,O2)

5. WEBrick, Tofu, Div::Div

6. MOD and /MOD with Pygmy Forth

7. SM/REM FM/MOD and UM/MOD on a 32bit cell ix86 Forth

8. Utils.Mod, ET.Mod

9. Mod-2 or Mod-3 for Mac

10. Mod-2 and Mod-3 compiler

11. Mod-2 and Mod-3 compiler for the IBM PC and compatibles

12. search algorithm for a math mod function (5 mod[2]=1)

 

 
Powered by phpBB® Forum Software