Re: did the multiplication rules change!?
- From: "shakah" <shakahshakah@xxxxxxxxx>
- Date: 10 Jan 2006 08:54:53 -0800
mark.olive...@xxxxxxxxx wrote:
> Hi all,
>
> I am dumbfounded... I have the following code:
>
> long product = 1000;
> product = product*60;
> product = product*60;
> product = product*24;
> product = product*30;
> product = product*3;
>
> long product2 = (1000*60*60*24*30*3);
>
> System.out.println("product: " + product);
> System.out.println("product2: " + product2);
>
> which produces the following result:
>
> product: 7776000000
> product2: -813934592
>
> Can anyone please explain this behavior?? Am I wrong to expect the same
> value for both product and product2? If so I would really like to know
> why. Any insight will be very much appreciated.
>
> Thanks,
>
> Mark
You're just seeing int overflow in the product2 line, try:
long product2 = (1000L*60L*60L*24L*30L*3L);
.
- Follow-Ups:
- Re: did the multiplication rules change!?
- From: mark . oliveira
- Re: did the multiplication rules change!?
- References:
- did the multiplication rules change!?
- From: mark . oliveira
- did the multiplication rules change!?
- Prev by Date: did the multiplication rules change!?
- Next by Date: Re: did the multiplication rules change!?
- Previous by thread: did the multiplication rules change!?
- Next by thread: Re: did the multiplication rules change!?
- Index(es):
Relevant Pages
|