Arithmetic overflows

This is a discussion on Arithmetic overflows within the C Programming forums, part of the General Programming Boards category; Hi again, simple question: If I have some calculation like: Code: int val = (int)exp*3600000/(uint)time And say exp is 4000, ...

  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
    478
    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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:48 PM
  2. Avoiding Buffer Overflows
    By Aidman in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2004, 11:21 AM
  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, 11:59 AM
  5. Replies: 1
    Last Post: 11-19-2001, 03:45 PM

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