Thread: Arithmetic overflows

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    Arithmetic overflows

    Hi again, simple question:
    If I have some calculation like:
    Code:
    int val = (int)exp*3600000/(uint)time
    And say exp is 4000, time is nearly 3600000, will the calculation cause an overflow because it calculates 4000*3600000 = 14400000000 first? Or is this compiler specific, or can I control it via:
    Code:
    val = exp*(3600000/time)
    Thanks in advance.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    I'd guess that the first is compiler specific, and I'd do the second one first, yeah.. Parenthesis precedence should keep it from overflowing.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, i'd divide first as long as you are in no risk of losing precision. Your second example makes no difference however as divide has precedence over multiplication. Alternatively you could always use a larger datatype, say Uint64. Or you could test for an overflow.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your second example makes no difference however as divide has precedence over multiplication.
    Actually, they have the same precedence. Consequently, I think that the grouping is necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Really, I thought it worked using BODMAS?

    Edit: Yeah, you are right. So what would evaluate first? would it be left to right?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I think so. I think most arithmetic operators are from left to right except the '=', '+=', '-=', etc.
    I might not be a pro, but I'm usually right

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Equal precedence operators are evaluated left to right.

    As to the original question: You will need to know the range of the respective numbers to decide which order is best for the calculation - in this example it seems that the 3600000/time is a good way to go, but if time is a larger number than 3600000, it would result in zero, and you would end up with bad result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Thanks for you answers.

    As exp and time tend to vary between minus several million and plus several million, I choose to use float variables instead of integers. Grants me an extra bonus to precision too. I multiply first now by the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on buffer overflows
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2004, 03:48 PM
  2. Avoiding Buffer Overflows
    By Aidman in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2004, 12:21 PM
  3. Replies: 3
    Last Post: 05-01-2003, 12:47 PM
  4. mod arithmetic in C++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2002, 12:59 PM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM