Why so many programs return wrong remainder?

This is a discussion on Why so many programs return wrong remainder? within the Tech Board forums, part of the Community Boards category; Please before continue read what remainder is, th1.1.3. *** Why many programs including all C flavours compilers, windows calc, Excel.... ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    17

    Why so many programs return wrong remainder?

    Please before continue read what remainder is, th1.1.3. ***

    Why many programs including all C flavours compilers, windows calc, Excel....
    Code:
    #include <iostream> /* TEST, SEE WITH YOUR EYES */
    int main()<% std::cout << -3 % 2; %>
    ...return wrong result? (-1 instead of 1, in this case)

    I guess it is something about how to the machine compare less significant bits.
    But I'd like to know where is the problem exactly.

    Do someone know? Thanks.



    *** Note: actually there is a small imprecision, b CAN be negative and 0 <= r < abs(b). Probably it is wrote that wat because it won't affect remainder.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    According to the Wikipedia Entry, for the set of integers, Z, remainder is defined as follows:

    If a and d are integers, with d non-zero, then a remainder is an integer r such that a = qd + r for some integer q, and with 0 ≤ |r| < |d|.
    The way it is defined in the link you provided, it seems a if it applies more towards natural numbers, even though it mentionse all integers earlier on.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    Wiki is wrong.
    If it was correct it would mean that remainder is not unique.

    7 % 3 -> 1 or -2 ?
    13 % -4 -> 1 or -3 ?
    -4 % 3 -> 2 or -1 ?
    -6 % -4 -> 2 or -2 ?
    Remainder IS positive.

    If remainder would not unique also division result not would be...
    7 / 3 -> 2 or 3?
    Last edited by Ezzetabi; 07-26-2005 at 09:19 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why many programs including all C flavours compilers, windows calc, Excel....
    Let's stick to C++, since it's easier to prove you're wrong that way.

    >...return wrong result? (-1 instead of 1, in this case)
    What makes you think it's wrong? The result is only "wrong" if it fails to meet the requirements as defined by the standard. The standard requires (ignoring division by 0) that (a/b)*b + a%b is equal to a. In fact, it is stated very clearly that if either a or b is negative, the sign of the remainder is implementation defined, but it's easy to see why your example would have to result in -1 or be wrong for real because it would fail to meet the requirement.

    >But I'd like to know where is the problem exactly.
    The problem is in your understanding of what should happen. Just because in college you always used absolute values for finding the remainder doesn't mean that that's the only way to do it.
    My best code is written with the delete key.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Aren't there two possibilities allowed by the standard?

    Either (-3) / 2 = -1 and (-3) % 2 = -1
    Or (-3) / 2 = -2 and (-3) % 2 = 1

    Aren't both these cases allowed? Both satisfy the formula Prelude posted. The standard only suggests that the algorithm follows Fortran.
    Last edited by Sang-drax; 07-26-2005 at 11:37 AM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Aren't there two possibilities allowed by the standard?
    Yes, it serves me right for trying to do math (which I'm notoriously bad at). Though everything besides "would have to result in -1 or be wrong" still stands. The standard says that it's implementation defined, and unless the implementation fails to do what it says, there's nothing wrong with the result, even if the OP doesn't agree.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    17
    Thanks for nothing.

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I was taught in school that the remainder is always positive. So the remainer of -3/2 is indeed 1. Either my math teacher was wrong ( unlikely ), there is a more sophisticated way to calculate remainders taught later in university ( unlikely ) or the definition does not conform to popular third grade math rules.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    even in high school I was taught that there were such things as negative remainders... just like there are negative differences for subtraction...

    take this situation: say somebody wants to put something on layaway for $300. you decide that you want to have them make a deposit for the total price divided by two. now, this may get tricky (with the negatives), but anybody who's taken accounting 101 would be able to follow (from the other person's P.O.V.)

    I owe $300 (-300)
    I need to pay the remainder of what I owe divided by two (-300%2)
    I owe $100 this month (-100)

    now, as you can see, that clearly explains why -300%2=-100 this is the way I've always learned it. if that equation came out to +100, then you would end up owing this guy $100 this month.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Thanks for nothing.
    You asked a question and got the answer. If you don't like the answer then that's your problem. Or were you just trying to act smart by claiming that you're right and every program in the world is wrong?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21