• Post Reply Bookmark Topic Watch Topic
  • New Topic

The maximum decimal places for a float and a double: Where are they exactly defined?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Eclipse, I declared a variable called number and set it to an impossibly long decimal place. How is this still valid? Why did Eclipse allow me to do this?



It appears that when I print it out, the result is only up to 6 decimal places.

I am looking at the Java 8 specification: http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf
Yet I am unable to find where this rule is defined.

I am using Java version 1.8.0 build 131
Thank you,
-Mark
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am not an expert, but basically, java uses the IEEE standard for floating point numbers.  that means what it really does is store the number in two parts the "number" part, and the "exponent" part. think of scientific notation:

4.232 x 10^3.

if everyone always agrees that the base for the exponent is "10", we don't need to store that. we can just store the "3".  now, if we know we have 10 places/bytes/digits to use to store this number, we can decide the first 6 are for the "number" part, and the last four are for the "exponent part".  so the above becomes:

0042320003

Java does basically that...except...it uses base 2 for everything.  so in a 32-bit type, it uses something like (I've not looked it up, so don't quote me on this) 20 bits for the "number", and 12 for the "exponent".

So the answer to "how many decimal places" is in a way "it depends".  if you have a number like

10982347019237481.00298457

the answer is "none", because it'll get rounded to some integer value.  but if you have a number like:

.00000000000000000000000002

the answer is "quite a few".
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that printing and storing the value are two separate concerns.  What method did you use to print the value? System.out.println() or System.out.printf()?  The former will use String.valueOf() to format the output of a float/double value. The latter will allow you to control how many decimal places you display.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:. . . in a 32-bit type, it uses . . . 20 bits for the "number", and 12 for the "exponent". . . .
1 bit for the sign, 8 bits for the exponent (biased) and 23 bits for the mantissa except in numbers in the “normal range”, where the mantissa occupies 24 bits. Yes, that does add up to 33.
I haven't got the time it would take to try and explain the IEEE754 format; I suggest you have a look at this Wikipedia page.

If you take 24 bits for the mantissa in a 32‑bit IEEE754 number, you can work out how many decimal digits' worth of precision that corresponds to: 24 × log₁₀2. Note that is sig fig, not places after the radix point (decimal point). That reduces greatly if you stray into the subnormal range. And as Junilu has told you, the format displayed when you print the numbers in decimal can be completely different.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And as Junilu has told you...

Just to be clear, my response was @OP, not so much at Fred
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!