Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Modulus of a negative number

Reply
Thread Tools

Modulus of a negative number

 
 
Jim Hunter
Guest
Posts: n/a
 
      02-23-2005
Hello all,

I have been reading K&R2, and it says that "the sign of the result for % [is]
machine-dependent for negative operands." Does this mean that the absolute
value of the result is guaranteed, but the sign isn't? Or does it mean that
(for example) -100 % 3 == -1 on some machines and 2 on others? The latter
behavior seems illogical to me, but I've been unable to find a definitive
statement about this.

Thanks in advance for satisfying my curiosity and for taking mercy on a
newsgroup newbie.

Jim
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-24-2005
In article <(E-Mail Removed)>,
Jim Hunter <(E-Mail Removed)> wrote:
:I have been reading K&R2, and it says that "the sign of the result for % [is]
:machine-dependent for negative operands." Does this mean that the absolute
:value of the result is guaranteed, but the sign isn't?

No.

: Or does it mean that
for example) -100 % 3 == -1 on some machines and 2 on others?

Yes.

: The latter
:behavior seems illogical to me, but I've been unable to find a definitive
:statement about this.

How were you thinking it could work with the absolute value being right but
the sign possibly being wrong? A positive value for % means that many
from the beginning; a negative value for % means that many from the end.
When the result isn't exactly half-way inbetween, changing the sign gives
a dfiferent meaning.
--
*We* are now the times. -- Wim Wenders (WoD)
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      02-24-2005
Jim Hunter wrote:
>
> I have been reading K&R2, and it says that "the sign of the result

for %
> [is] machine-dependent for negative operands." Does this mean that

the
> absolute value of the result is guaranteed, but the sign isn't?


No. C89 gives an implementation two options if either operand to / (or
%)
are negative: round up, or round down (this includes round to zero
which
is _required_ by C99.) In either case, if the result is representable,
the following must hold...

a == (a/b)*b + (a%b)

Examples...

5 / -3 == -2 and 5 % -3 == -1
or 5 / -3 == -1 and 5 % -3 == 2

> Or does it mean that (for example) -100 % 3 == -1 on some machines
> and 2 on others?


Yes.

> The latter behavior seems illogical to me,


It's sometimes useful mathematically for the modulus to always be
positive.
This, and round towards zero, are the only mechanisms that you are
likely
to encounter. C89 just happens to give an implementation 8 possible
variations.

--
Peter

 
Reply With Quote
 
ruang ruang is offline
Junior Member
Join Date: May 2010
Posts: 1
 
      05-03-2010
One simple example in which the positive number is more useful, is calculating time. For instance, if the current time is 1pm, and I want to figure out the time 2 hours ago, I would do the following:
(1-2)%12 = -1%12 = 11
which is the correct answer. In this case the answer of -1 does no good at all. The reason we want the positive answer in this case is because the time scale is cyclic from 0 to 12, no negative values. I would imagine many real-life examples in which some cyclic scale goes from 0 to some positive number, in which case would prefer to use the positive answer over the negative.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Verification of bank number using modulus 11 Morten Engvoldsen Python 0 02-19-2013 10:59 PM
Re: Verification of bank number using modulus 11 MRAB Python 0 02-19-2013 09:12 PM
Re: Verification of bank number using modulus 11 Dave Angel Python 0 02-19-2013 09:10 PM
Re: Verification of bank number using modulus 11 Ian Kelly Python 0 02-19-2013 09:10 PM
Modulus and negative operands August Karlstrom C Programming 4 01-12-2006 07:56 PM



Advertisments