• Post Reply Bookmark Topic Watch Topic
  • New Topic

Incorrect value of long calculation?  RSS feed

 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you ever just "known" there was a simple explanation for some strange behavior? I cannot figure out why lines 1 and 3 of the following main() produce different results.
(Maybe my brain has frozen, it is a Monday!)



My results running this on 1.5 and 1.6:
The big number is: 1073741824
The bigger number is: 0
The bigger bigNumber is: 4294967296

Multiply the first bigNumber by 4 and the result is OK. What gives? Is this a bug? The value is WAY less than Long.MAX_VAL!

Darrin
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, should of updated that output! I get the following:

The big number is: 1073741824
The bigger number is what?: 0
The bigger bigNumber is: 4294967296

Sorry about that!

Darrin
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darrin Cartwright:
Have you ever just "known" there was a simple explanation for some strange behavior? I cannot figure out why lines 1 and 3 of the following main() produce different results.
(Maybe my brain has frozen, it is a Monday!)



My results running this on 1.5 and 1.6:
The big number is: 1073741824
The bigger number is: 0
The bigger bigNumber is: 4294967296

Multiply the first bigNumber by 4 and the result is OK. What gives? Is this a bug? The value is WAY less than Long.MAX_VAL!

Darrin


No, it isn't a bug.

The constants 4 and 1024 are ints, and when you multiply an int by an int, the result is an int.

If the result is larger than the capacity of an int, then bits will be lost.

The assignment to a long takes place after the calculation.

In the last multiplication, the type of bigNumber is long, and so the int 4 is promoted to a long before the multiplication.
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DOH!

I knew that! It IS a Monday!

Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!