From: "Deepak Gaur" <dgaur@cdotd.ernet.in>
To: linuxppc-embedded@ozlabs.org
Subject: Modulo operation in C for -ve values
Date: Mon, 12 Nov 2007 17:25:33 +0530 [thread overview]
Message-ID: <20071112115350.M72594@cdotd.ernet.in> (raw)
In-Reply-To: <200711121901.39163.rusty@rustcorp.com.au>
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(0,-3) or (-1,5)?
Thanks
Deepak Gaur
next prev parent reply other threads:[~2007-11-12 11:44 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-08 23:36 [PATCH] [POWERPC] Optimize counting distinct entries in the relocation sections Emil Medve
2007-11-12 6:00 ` Paul Mackerras
2007-11-12 8:01 ` Rusty Russell
2007-11-12 11:55 ` Deepak Gaur [this message]
2007-11-12 16:50 ` Medve Emilian
2007-11-12 22:31 ` [PATCH] [POWERPC] Optimize counting distinct entries in therelocation sections Medve Emilian
2007-11-13 2:49 ` Paul Mackerras
2007-11-13 4:27 ` Rusty Russell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071112115350.M72594@cdotd.ernet.in \
--to=dgaur@cdotd.ernet.in \
--cc=linuxppc-embedded@ozlabs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 NNTP newsgroup(s).