473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Modulus of a negative number

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
Nov 14 '05 #1
2 25981
In article <87************@paradigmshift.homelinux.net>,
Jim Hunter <jh*****@paradigmshift.homelinux.net> 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)
Nov 14 '05 #2
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

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
16
by: JKop | last post by:
Take a class like the following: class Finger { public: double length; }
7
by: hasanainf | last post by:
Hi all, I have two querys QueryPurchased ProductID Location TotPcs Prod1 Loc1 100 Prod2 Loc1 50 Prod3 Loc1 150 Prod3 Loc3 150
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
1
by: Wayne | last post by:
There is probably a simple solution to this but: I have a textbox that is bound to a number field in a table. The field type is "double". If I enter a negative value directly into the table it...
3
by: Frederick Gotham | last post by:
(I'm not sure if there's already something in the Standard Library for doing this... ?) Is the following macro okay for getting a compile-time constant that indicates which negative number...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
5
by: wilboy | last post by:
Hi everyone I have a small problem here. I only want return positive number or zero when - . Eg. - = 48.50 - 52.60 = 0 78.20 ...
3
by: Peng Yu | last post by:
Hi, I don't understand why rbegin() -rend() gives a negative number. Since rbegin() + 1 gives the one before the last element, I think rbegin() - rend() should give a positive number. ...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.