Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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".
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:And as Junilu has told you...


Just to be clear, my response was @OP, not so much at Fred
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1583 times.
Similar Threads
Optional labels
Why does this " System.out.println(2 + 1.0f) " print 3.0?
Error with variable access while handling exception
defining interfaces errata item? (Java OCA 8 Programmer I Study Guide)
Are Enthuware mocks good?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:08:55.