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

Discussion in 'Java' started by kkyzir@gmail.com, Oct 5, 2006.

1. ### Guest

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?

, Oct 5, 2006

2. ### Boris StummGuest

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

Boris Stumm, Oct 5, 2006

3. ### Guest

Thanks Boris! I'm on to it

, Oct 5, 2006
4. ### Patricia ShanahanGuest

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

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

Patricia Shanahan, Oct 5, 2006
5. ### Guest

to fully understand what you had already explained to me! It was
exactly what I was looking for!

, Oct 7, 2006
6. ### Andrew ThompsonGuest

wrote:
> 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.

Andrew Thompson, Oct 7, 2006
7. ### Patricia ShanahanGuest

Andrew Thompson wrote:
> wrote:
>> 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, Oct 7, 2006
8. ### Patricia ShanahanGuest

wrote:
> 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

Patricia Shanahan, Oct 7, 2006
9. ### Andrew ThompsonGuest

Patricia Shanahan wrote:
> wrote:
> > 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.

Andrew Thompson, Oct 7, 2006