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?

    Thanks in advance!
     
    , Oct 5, 2006
    #1
    1. Advertising

  2. Boris Stumm Guest

    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
    #2
    1. Advertising

  3. Guest

    Thanks Boris! I'm on to it
     
    , Oct 5, 2006
    #3
  4. 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?
    >
    > 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
     
    Patricia Shanahan, Oct 5, 2006
    #4
  5. Guest

    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!
     
    , Oct 7, 2006
    #5
  6. 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.
     
    Andrew Thompson, Oct 7, 2006
    #6
  7. Andrew Thompson wrote:
    > 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, Oct 7, 2006
    #7
  8. 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
     
    Patricia Shanahan, Oct 7, 2006
    #8
  9. Patricia Shanahan wrote:
    > 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.
     
    Andrew Thompson, Oct 7, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    666
    Fredrik Lundh
    Jan 23, 2006
  2. Deniz Bahar
    Replies:
    2
    Views:
    503
    Andrey Tarasevich
    Mar 9, 2005
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    816
    Alf P. Steinbach
    Jul 10, 2007
  4. inspired
    Replies:
    4
    Views:
    822
    Roedy Green
    Nov 28, 2010
  5. oldyork90

    find it, cut it out, find next

    oldyork90, Jan 16, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    401
    John W. Krahn
    Jan 22, 2012
Loading...

Share This Page