public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/5308] New: Integer division and Modulo for -ve values
@ 2007-11-12 10:35 dgaur at cdotd dot ernet dot in
  2007-11-12 15:14 ` [Bug libc/5308] " drepper at redhat dot com
  0 siblings, 1 reply; 2+ messages in thread
From: dgaur at cdotd dot ernet dot in @ 2007-11-12 10:35 UTC (permalink / raw)
  To: glibc-bugs

The Modulo operation as specified in
http://xenia.media.mit.edu/~bdenckla/thesis/texts/htthe/node13.html says that
for a fraction like n/k which can be expressed as n/k = i + j/k the C division
and mod operation should yeild
n div k = i (integer part)
n mod k = j (remainder part)
For n +ve above is true 
For n -ve
-n/k = -i + j/k
-n div k = -i
-n mod k = j (+ve remainder)

But running a sample program on Redhat enterprise Linux EL4
with libc-2.3.4 gcc version 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,k,j;
n=-3;
k=8;
j=(n/k);
printf("\n n div k %d", j);
j=(n%k);
printf("\n n mod k %d", j);
}
gives following output for n = -3 k = 8
n div k 0
n mod k  -3
though it should have been as per hypothesis proposed in
http://xenia.media.mit.edu/~bdenckla/thesis/texts/htthe/node13.html
n div k -1
n mod k  5

Which is correct?

-- 
           Summary: Integer division and Modulo for -ve values
           Product: glibc
           Version: 2.4
            Status: NEW
          Severity: minor
          Priority: P3
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: dgaur at cdotd dot ernet dot in
                CC: dgaur at cdotd dot ernet dot in,glibc-bugs at sources
                    dot redhat dot com


http://sourceware.org/bugzilla/show_bug.cgi?id=5308

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug libc/5308] Integer division and Modulo for -ve values
  2007-11-12 10:35 [Bug libc/5308] New: Integer division and Modulo for -ve values dgaur at cdotd dot ernet dot in
@ 2007-11-12 15:14 ` drepper at redhat dot com
  0 siblings, 0 replies; 2+ messages in thread
From: drepper at redhat dot com @ 2007-11-12 15:14 UTC (permalink / raw)
  To: glibc-bugs


------- Additional Comments From drepper at redhat dot com  2007-11-12 15:14 -------
This has nothing to do with the C library, it is entirely the compiler which
implements these operations.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


http://sourceware.org/bugzilla/show_bug.cgi?id=5308

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-11-12 15:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-12 10:35 [Bug libc/5308] New: Integer division and Modulo for -ve values dgaur at cdotd dot ernet dot in
2007-11-12 15:14 ` [Bug libc/5308] " drepper at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).