LRM description of modulus

From: Paul Floyd <Paul_Floyd_at_.....>
Date: Thu Apr 05 2007 - 09:29:17 PDT
Hi

Yesterday I came accross what looks to me line an error in the LRM.

On Page 57, it states that

"The modulus operator, for example y % z, gives the remainder when the 
first operand is
divided by the second, and thus is zero (0) when z divides y exactly. 
The result of a
modulus operation takes the sign of the first operand.

For the case of the modulus operator where either argument is real, the 
operation
performed is

a % b = a - floor(a/b)*b;"

This is contradictory in the case where a and b don't have the same 
sign. The problem is that floor rounds down, not toward zero (which is 
rounding up for negative numbers). For example, if a is -10.0 and b is 
3.75, then we have

-10.0 - floor(-10.0/3.75)*3.75
= -10.0 - floor(-2.67)*3.75
= -10.0 - -3*3.75
= -10.0 + 11.25
= +1.25

which does not have the same sign as a.

If the description is changed to

a % b = a - trunc(a/b)*b;

where trunc rounds to zero, not down, then the result is correct:

-10.0 - trunc(-10.0/3.75)*3.75
= -10.0 - trunc(-2.67)*3.75
= -10.0 - -2*3.75
= -10.0 + 7.5
= -2.5

Regards
Paul Floyd
-- 
Dr. Paul Floyd
Mentor Graphics Corporation


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Thu Apr 5 09:52:10 2007

This archive was generated by hypermail 2.1.8 : Thu Apr 05 2007 - 09:52:19 PDT