Mark Richardson

Ranch Hand

Posts: 108

2

posted 5 months ago

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

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

posted 5 months ago

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".

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".

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 5 months ago

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.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Campbell Ritchie

Marshal

Posts: 56546

172

posted 5 months ago

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,

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.fred rosenberger wrote:. . . in a 32-bit type, it uses . . . 20 bits for the "number", and 12 for the "exponent". . . .

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.

posted 5 months ago

Just to be clear, my response was @OP, not so much at Fred

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

Just to be clear, my response was @OP, not so much at Fred

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]