Mod operator

  • Thread starter Thread starter R Avery
  • Start date Start date
R

R Avery

How come the Mod operator can return negative values? I've always
wondered why.
 
well, what is there to wonder in that.
-3 Mod 2 will return -1
and that is perfect

- manges
 
You seem to be getting into the universal definition paradigm. VBA defines
how the MOD operator works. Excel has a MOD function and it works
differently - but its operation is defined in help. Mathematics may have
another definition of how MOD works or rather what modulus/modulo is. You
can't assume that a programming function will work the way your math teacher
defined a math operation. You have to look at the documentation.

as an illustrative point, one would think that addition would be pretty
straightforward, but then you run into limitations in the IEEE
implementation of a double . . .

Each language that has a standard defines their own implementation. Those
that don't have a recognized standard define it in their documentation.
There is no universal standard (that I am aware of) that you seem to imply.

http://support.microsoft.com/default.aspx?scid=kb;en-us;141178&Product=xlw
MOD Function and Mod Operator Return Different Values

http://support.microsoft.com/default.aspx?scid=kb;en-us;214271&Product=xlw
XL2000: MOD Function and Mod Operator Return Different Values
 
Some implementations of MOD work differently when the arguments have different
signs. The worksheet function uses the definition that the modulus is always
positive, i.e. =MOD(-10,3) yields -4 with a remainder of 2. OTOH, in VBA,
definition that's used is probably what you division in grade school: -10
divided by +3 is -3 with a remainder of -1.
 

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

Similar Threads


Back
Top