How to find the "next" number (precision and java)

K

kkyzir

Hello everybody! I have the following question:
Given a double number, can I calculate the next closest double that can
represented from Java's float implementation ?
e.g. If I have the number
1.500000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000000
and the precision is
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
then the next number would be
1.500000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000001

Can I do something like this or the precision is not the same for all
numbers?

Thanks in advance!
 
B

Boris Stumm

Hello everybody! I have the following question:
Given a double number, can I calculate the next closest double that can
represented from Java's float implementation ?

What? float or double?
e.g. If I have the number
1.500000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000000
and the precision is
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
then the next number would be
1.500000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000001

floats have 7 digits precision, doubles have 15 digits precision. That is
TOTAL digits, not digits after the decimal point. That means, first number
would be 1.5, second number would be 0, and the third again 1.5
Can I do something like this or the precision is not the same for all
numbers?

Depends. relative precision is the same for all doubles/floats,
absolute precision (the one you seem to refer to) is not.

Have a look at java.lang.Math.ulp(double) or java.lang.Math.ulp(float).
If thats not what you want, have a look at java.math.BigDecimal.
 
P

Patricia Shanahan

Hello everybody! I have the following question:
Given a double number, can I calculate the next closest double that can
represented from Java's float implementation ?
e.g. If I have the number
1.500000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000000
and the precision is
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
then the next number would be
1.500000000000000000000000000000000000000011111111111000000000000000000000000000000000000000000000000001

Can I do something like this or the precision is not the same for all
numbers?

Thanks in advance!

The absolute value of the gap between adjacent doubles varies with
magnitude. Floating point carries a fixed number of significant bits, so
large numbers have bigger gaps between them than small numbers.

It is sometimes called the "ulp", unit in last place, of the number, and
is a useful concept in talking and thinking about floating point precision.

If you want to know it for a specific number, use Math.ulp().

In simple cases (not infinities, NaNs, Double.MAX_VALUE), you can find
the next double up by adding one to the bit pattern:

long bits = Double.doubleToLongBits(in);
double up = Double.longBitsToDouble(bits+1);

Patricia
 
K

kkyzir

Thank you Patricia for your answer! I had to read the IEEE 754 standard
to fully understand what you had already explained to me! It was
exactly what I was looking for!
 
A

Andrew Thompson

Thank you Patricia for your answer! I had to read the IEEE 754 standard
to fully understand what you had already explained to me! It was
exactly what I was looking for!

I am still lost.*

What is the actual (/ultimate) purpose of gaining the next double?

* Not that that matters to the OP or their stated problem,
but I am interested.

Andrew T.
 
P

Patricia Shanahan

Andrew said:
I am still lost.*

What is the actual (/ultimate) purpose of gaining the next double?

* Not that that matters to the OP or their stated problem,
but I am interested.

I don't know about the OP's interest, but I am interested in the gaps
between adjacent numbers as a means to the end of understanding floating
point behavior.

The precision of many of the java.lang.Math methods is defined in terms
of the ulp for the input. The most accurate operations, including simple
arithmetic, produce an answer, if rounding is needed, that is one of the
pair of doubles that bracket the infinitely precise result.

Patricia
 
P

Patricia Shanahan

Thank you Patricia for your answer! I had to read the IEEE 754 standard
to fully understand what you had already explained to me! It was
exactly what I was looking for!

You have my sympathy. What understanding I have of IEEE floating point
came partly from reading the standard, many times.

Patricia
 
A

Andrew Thompson

Patricia said:
You have my sympathy. What understanding I have of IEEE floating point
came partly from reading the standard, many times.

(chuckle) BTW - Thanks for the response on the other (sub)thread.

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top