Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java - integer limits, multiplication and exponents  RSS feed

 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just playing around, but noticed that if I use integers only and simple multiiplication to get my exponent results (in this instance I was looking into Base 26) what is returned is odd, BUT consistent results once I surpass the integer upper limit of 2,147,483,647. I anticipated that once this limit was reached the program would blow up somehow, but no. Can anyone explain why I get these consistent results? Just curious.



***Here are the results that I consistently get for integers along with the results I would get by using the Math class:
Enter an integer exponent: 20
Using int only:26 to the 2 power = 676
Math class: 26.0 to the 2.0 power = 676.0
Using int only:26 to the 3 power = 17576
Math class: 26.0 to the 3.0 power = 17576.0
Using int only:26 to the 4 power = 456976
Math class: 26.0 to the 4.0 power = 456976.0
Using int only:26 to the 5 power = 11881376
Math class: 26.0 to the 5.0 power = 1.1881376E7
Using int only:26 to the 6 power = 308915776
Math class: 26.0 to the 6.0 power = 3.08915776E8
Using int only:26 to the 7 power = -558124416
Math class: 26.0 to the 7.0 power = 8.031810176E9
Using int only:26 to the 8 power = -1626332928
Math class: 26.0 to the 8.0 power = 2.08827064576E11
Using int only:26 to the 9 power = 665016832
Math class: 26.0 to the 9.0 power = 5.429503678976E12
Using int only:26 to the 10 power = 110568448
Math class: 26.0 to the 10.0 power = 1.41167095653376E14
Using int only:26 to the 11 power = -1420187648
Math class: 26.0 to the 11.0 power = 3.670344486987776E15
Using int only:26 to the 12 power = 1729826816
Math class: 26.0 to the 12.0 power = 9.5428956661682176E16
Using int only:26 to the 13 power = 2025824256
Math class: 26.0 to the 13.0 power = 2.4811528732037366E18
Using int only:26 to the 14 power = 1131823104
Math class: 26.0 to the 14.0 power = 6.450997470329715E19
Using int only:26 to the 15 power = -637370368
Math class: 26.0 to the 15.0 power = 1.677259342285726E21
Using int only:26 to the 16 power = 608239616
Math class: 26.0 to the 16.0 power = 4.360874289942888E22
Using int only:26 to the 17 power = -1365639168
Math class: 26.0 to the 17.0 power = 1.1338273153851507E24
Using int only:26 to the 18 power = -1146880000
Math class: 26.0 to the 18.0 power = 2.947951020001392E25
Using int only:26 to the 19 power = 245891072
Math class: 26.0 to the 19.0 power = 7.664672652003619E26
Using int only:26 to the 20 power = 2098200576
Math class: 26.0 to the 20.0 power = 1.992814889520941E28
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Willis wrote:Just playing around, but noticed that if I use integers only and simple multiiplication to get my exponent results (in this instance I was looking into Base 26) what is returned is odd, BUT consistent results once I surpass the integer upper limit of 2,147,483,647. I anticipated that once this limit was reached the program would blow up somehow, but no. Can anyone explain why I get these consistent results? Just curious.


The whole number data types, such as byte, short, int, long, simply circles around when handling overflows... meaning adding one to the maximum value will generate a result of the minimum value.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Willis wrote:Just playing around, but noticed that if I use integers only and simple multiiplication to get my exponent results (in this instance I was looking into Base 26) what is returned is odd, BUT consistent results once I surpass the integer upper limit of 2,147,483,647. I anticipated that once this limit was reached the program would blow up somehow, but no. Can anyone explain why I get these consistent results?

Because the return type (double) can handle values much larger than an integer can (it has 64 bits to store them in for a start; an int only has 32). The only thing you need to be aware of is that once you get over 2^53 (approximately 10^16), the values will NOT be exact - which is why you should never use doubles to hold precise decimal values (like money).

For more information, you might want to look at this page.

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it is 2⁵³? You get the equivalent of 53 bits, so I think it would be ±(0…2⁵⁴-1) where you get precise values for whole numbers only as doubles. Some fractions, e.g. 2.5 exactly can be stored precisely as doubles, but most can't.

If you want precision, avoid floating‑point arithmetic; it is designed for range not precision. Use this for precise decimal fractions and this for integers too big to fit into a long.
I think there are some languages which throw Exceptions if you get an arithmetic overflow, but Java® doesn't. Otherwise you could never calculate hash codes.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you sure it is 2⁵³?

Yup; pretty sure. The "mantissa" in IEEE-754 is only 52 bits long, but it has some "clever stuff" to deal with insignificant bit values (like 0) as I recall, so the actual 'integer' values correspond to 2^53 (or 15.95 significant decimal digits), possibly ± 1 or 2.

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, IEEE754 has a fiddle which means the mantissa in a value in the “normal” range is 53 bits. One bit for the sign bit, eleven for the exponent and fifty‑three for the mantissa. That adds up to 64 65.

And I am not having you on! They have a fiddle to fit 53 bits into the 52 available.
 
George Willis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!