13

why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java?

cherouvim
  • 31,725
  • 15
  • 104
  • 153
Satish
  • 6,457
  • 8
  • 43
  • 63
  • 25
    Because Java stretches in east-west direction, so the day is a bit longer than 86400000000 microseconds :-) – balpha Aug 19 '09 at 17:19
  • 8
    I think for this question to make sense it has to specify that it means int's and not long's. without displaying actual code or specifying, the question is ambiguous. this is the kind of question I hated on tests in school - am i supposed to answer "it *does* equal 1000" (correct, if assuming longs) or am i supposed to guess that the prof meant to imply that everything was being done as ints, and therefore mention overflow? – Peter Recore Aug 19 '09 at 22:32

3 Answers3

60

Because the multiplication overflows 32 bit integers. In 64 bits it's okay:

public class Test
{
    public static void main(String[] args)
    {
        int intProduct = 24 * 60 * 60 * 1000 * 1000;
        long longProduct = 24L * 60 * 60 * 1000 * 1000;
        System.out.println(intProduct); // Prints 500654080
        System.out.println(longProduct); // Prints 86400000000
   }
}

Obviously after the multiplication has overflowed, the division isn't going to "undo" that overflow.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    What does 1000 days * 1000 mean anyway? – Joel Coehoorn Aug 19 '09 at 17:18
  • 25
    Those must be them new "metric years" they're always talkin' about. – Don Branson Aug 19 '09 at 17:22
  • 7
    1000 days * 1000 would be 1,000,000 days. There's nothing mysterious about that. Now, 1000 inches x 1000 inches is 1,000,000 square inches, so presumably 1000 days x 1000 days would be 1,000,000 square days. But I'm not exactly sure what a "square day" is. Perhaps that's a unit of measure that time travellers use when dealing in pan-dimensional space. – Jay Aug 19 '09 at 18:22
  • 13
    Days ? 24*60'60*1000*1000 is the nr of microseconds in a day. :) – nos Aug 19 '09 at 18:49
  • i think it would depend on units, whether microseconds in a day or days in metric year? – understack Jan 29 '10 at 19:17
22

You need to start with 24L * 60 * ... because the int overflows.

Your question BTW is a copy/paste of Puzzle 3: Long Division from Java Puzzlers ;)

cherouvim
  • 31,725
  • 15
  • 104
  • 153
3

If you want to perform that calculation, then you must either re-order the operations (to avoid overflow) or use a larger datatype. The real problem is that arithmetic on fixed-size integers in Java is not associative; it's a pain, but there's the trade-off.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
  • re-ordering the numbers will not avoid the overflow – cherouvim Aug 19 '09 at 17:28
  • @cherouvim, but reordering the operations could. – Nosredna Aug 19 '09 at 17:33
  • 1
    I believe Thom is talking about reordering the multiplication so you do something like this instead: ((24 * 60)/(24 * 60)) * ((1000 * 1000)/(1000*1000)) – DeadHead Aug 19 '09 at 17:34
  • Fixed size integers are integers modulo a number (a residue class ring), so operations other than division have all the usual good properties (associative, commutative, distributive, ...). This doesn't hold for floating point, though. – starblue Aug 19 '09 at 20:22