Velocity Reviews > Java > How to find the "next" number (precision and java)

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

kkyzir@gmail.com
Guest
Posts: n/a

 10-05-2006
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.500000000000000000000000000000000000000011111111 11100000000000000000000000000000000000000000000000 0000
and the precision is
0.000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0001
then the next number would be
1.500000000000000000000000000000000000000011111111 11100000000000000000000000000000000000000000000000 0001

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

Boris Stumm
Guest
Posts: n/a

 10-05-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.500000000000000000000000000000000000000011111111 11100000000000000000000000000000000000000000000000 0000
> and the precision is
>

0.000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0001
> then the next number would be
>

1.500000000000000000000000000000000000000011111111 11100000000000000000000000000000000000000000000000 0001

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.

kkyzir@gmail.com
Guest
Posts: n/a

 10-05-2006
Thanks Boris! I'm on to it

Patricia Shanahan
Guest
Posts: n/a

 10-05-2006
(E-Mail Removed) wrote:
> 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.500000000000000000000000000000000000000011111111 11100000000000000000000000000000000000000000000000 0000
> and the precision is
> 0.000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 0001
> then the next number would be
> 1.500000000000000000000000000000000000000011111111 11100000000000000000000000000000000000000000000000 0001
>
> 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

kkyzir@gmail.com
Guest
Posts: n/a

 10-07-2006
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!

Andrew Thompson
Guest
Posts: n/a

 10-07-2006
(E-Mail Removed) wrote:
> 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.

Patricia Shanahan
Guest
Posts: n/a

 10-07-2006
Andrew Thompson wrote:
> (E-Mail Removed) wrote:
>> 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.

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

Patricia Shanahan
Guest
Posts: n/a

 10-07-2006
(E-Mail Removed) wrote:
> 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

Andrew Thompson
Guest
Posts: n/a

 10-07-2006

Patricia Shanahan wrote:
> (E-Mail Removed) wrote:
> > 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.

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

Andrew T.