10 Replies Latest reply: Nov 14, 2007 11:14 AM by 807603 RSS

    Weird arithmetic result involving double calculation

    807603
      Hi there,

      In this calculation:

      int sizeBytes = 309375000;
      double downlinkRate = 8.0;

      double downloadTime3 = ( (double)sizeBytes * 8) / (1048576 * downlinkRate);
      double downloadTime4 = ( sizeBytes * 8) / (1048576 * downlinkRate);


      downloadTime3 ends up with the correct result, 295.0429916381836, but downloadTime4 ends up with a completely bizarre negative result: -216.9570083618164.

      Can anybody explain to me why a) the answer is different and b) it ends up with a negative answer when all positive values are used ??

      I'm using Java 5.0.

      Many thanks,
      Simon
        • 1. Re: Weird arithmetic result involving double calculation
          807603
          sizeBytes * 8 overflows but (double)sizeBytes * 8 does not since the cast to a double applies to sizeBytes and not the product.
          • 2. Re: Weird arithmetic result involving double calculation
            807603
            perfectplanet.co.uk wrote:
            double downloadTime4 = ( sizeBytes * 8) / (1048576 * downlinkRate);
            sizeBytes is an int. When you perform sizeBytes * 8, the result is greater than Integer.MAX_VALUE and so it overflows into the negative domain of int.

            By casting to a double you're telling it to perform a multiplication with double operands and a double result, and as a result you get back the correct sign.
            • 3. Re: Weird arithmetic result involving double calculation
              807603
              Hi there,

              Fast replies! Thanks a lot. Makes sense - but how come some sort of exception isn't raised? Surely Java doesn't just ignore these errors?
              • 4. Re: Weird arithmetic result involving double calculation
                807603
                Surely Java doesn't just ignore these errors?
                Uh... Did you run your own code?
                • 5. Re: Weird arithmetic result involving double calculation
                  807603
                  Of course I ran it, how do you think I came up with the values?

                  Java raises no exception in the calculation.
                  • 6. Re: Weird arithmetic result involving double calculation
                    807603
                    perfectplanet.co.uk wrote:
                    Of course I ran it, how do you think I came up with the values?

                    Java raises no exception in the calculation.
                    And neither would C or C++ or most (if not all) of the major languages.
                    • 7. Re: Weird arithmetic result involving double calculation
                      807603
                      perfectplanet.co.uk wrote:
                      Surely Java doesn't just ignore these errors?
                      They're only errors from your point of view. Should Java check every mathematical operation you do to safe guard from overflow? Some would argue yes. I personally like the fact that Integer.MAX_VALUE + 1 == Integer.MIN_VALUE. It might make more sense to you if you understood how two's-complement number representation works.

                      Say we had a 3-bit integer type. This is how the numbers are represented:
                       3 = 011
                       2 = 010
                       1 = 001
                       0 = 000
                      -1 = 111
                      -2 = 110
                      -3 = 101
                      -4 = 100
                      If you look at it closely, you can see how simple this makes certain arithmetic operations. Take adding by 1. No matter what happens, the next number can be represented by adding a 1-bit to the lowest significant bit. When you add 1 to 011 (3), what's the result? 100 (-4). When you add 1 to 111 (-1), what happens? You get 1000, which in 3 bits is 000 (0), which makes sense as -1 + 1 = 0.

                      So it helps to have an idea of binary representations of numbers.
                      • 8. Re: Weird arithmetic result involving double calculation
                        807603
                        perfectplanet.co.uk wrote:
                        Of course I ran it, how do you think I came up with the values?

                        Java raises no exception in the calculation.
                        So you saw that Java raises no exception and then asked "Surely Java doesn't just ignore these errors?" Websters defines "ignore" as

                        1. To be ignorant of or not acquainted with. [Archaic]

                        Philosophy would solidly be established, if men would more carefully distinguish those things that they know from those that they ignore. Boyle.

                        2. (Law) To throw out or reject as false or ungrounded; -- said of a bill rejected by a grand jury for want of evidence. See Ignoramus.

                        3. Hence: To refuse to take notice of; to shut the eyes to; not to recognize; to disregard willfully and causelessly; as, to ignore certain facts; to ignore the presence of an objectionable person.
                        • 9. Re: Weird arithmetic result involving double calculation
                          807603
                          Interesting! Thanks for the explanation. I certainly wouldn't expect C/C++ to do this kind of checking, but I would have expected Java to (not because I necessarily want it to, but it seems like the kind of safe thing Java would do)

                          Thanks for the replies chaps - I'll be looking out for this in future.

                          S
                          • 10. Re: Weird arithmetic result involving double calculation
                            807603
                            PS - just to make clear, my heartfelt thanks are directed at endasil and sabre... and my demonstration of the meaning of the word 'ignore' is firmly directed at the other.

                            Beats me why some people find it necessary to be sarcastic in a programming forum... they obviously don't have enough work to do!