Problems adding floats

Discussion in 'Java' started by Klixx0r, Dec 12, 2004.

  1. Klixx0r

    Klixx0r Guest

    I have a problem when adding 2 floats together.

    I add 1.235 and 0.001 together and get 1.2360001. Substraction
    works fine.

    With the addition I need the answer 1.236 and just that. Is there
    any solution to this problem?

    Thanks

    ----------------------------------------------
    Posted with NewsLeecher v1.0 Final
    * Binary Usenet Leeching Made Easy
    * http://www.newsleecher.com/?usenet
    ----------------------------------------------
    Klixx0r, Dec 12, 2004
    #1
    1. Advertising

  2. Klixx0r

    Guest

    I think the problem lies in the fact that decimal numbers can't always
    be converted into binary decimals exactly. There might be two possible
    results when converting. You might try using BigDecimal
    , Dec 12, 2004
    #2
    1. Advertising

  3. "Klixx0r" <> wrote in message
    news:su2vd.880$...
    > I have a problem when adding 2 floats together.
    >
    > I add 1.235 and 0.001 together and get 1.2360001. Substraction
    > works fine.
    >
    > With the addition I need the answer 1.236 and just that. Is there
    > any solution to this problem?


    The problem is actually that you don't understand how floating point numbers
    work. Many simple decimal numbers (e.g. 0.001) repeat indefinately when
    represented in binary and cannot be represented precisely. If precision is
    critical for you, as it would be for financial data, try BigDecimal.
    Otherwise, accept that real numbers cannot be represented with perfect
    precision in a finite number of bits and round the number to the something
    acceptable and format the result to ignore the result. Look at Math.round /
    floor / ceil and DecimalFormat.

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Dec 12, 2004
    #3
  4. Klixx0r wrote:

    > I have a problem when adding 2 floats together.
    >
    > I add 1.235 and 0.001 together and get 1.2360001. Substraction
    > works fine.
    >
    > With the addition I need the answer 1.236 and just that. Is there
    > any solution to this problem?
    >
    > Thanks
    >
    > ----------------------------------------------
    > Posted with NewsLeecher v1.0 Final
    > * Binary Usenet Leeching Made Easy
    > * http://www.newsleecher.com/?usenet
    > ----------------------------------------------
    >


    There are other combinations of numbers for which
    subtraction will give rounding errors.

    The solution depends on the actual constraints:

    1. If you need exact arithmetic for short decimal fractions,
    for example to do financial calculations, use
    java.math.BigDecimal.

    2. If you need reasonable precision for more general cases,
    for example, 1/3.0, use double. Use java.text.DecimalFormat
    to round on output.

    I would only use float for very large quantities of low
    precision data.

    Patricia
    Patricia Shanahan, Dec 12, 2004
    #4
  5. Matt Humphrey wrote:
    > The problem is actually that you don't understand how floating point numbers
    > work. Many simple decimal numbers (e.g. 0.001) repeat indefinately when
    > represented in binary and cannot be represented precisely. If precision is
    > critical for you, as it would be for financial data, try BigDecimal.


    Precision is not the issue; double offers plenty of precision. The issue is
    conforming to expectations about decimal fractions and, more importantly,
    having stringently defined rounding rules (i.e. imprecision).
    Michael Borgwardt, Dec 13, 2004
    #5
  6. Michael Borgwardt wrote:
    > Matt Humphrey wrote:
    >
    >> The problem is actually that you don't understand how floating point
    >> numbers
    >> work. Many simple decimal numbers (e.g. 0.001) repeat indefinately when
    >> represented in binary and cannot be represented precisely. If
    >> precision is
    >> critical for you, as it would be for financial data, try BigDecimal.

    >
    >
    > Precision is not the issue; double offers plenty of precision. The issue is
    > conforming to expectations about decimal fractions and, more importantly,
    > having stringently defined rounding rules (i.e. imprecision).


    The rounding rules for float and double are stringently defined, just
    different to those defined on decimal fractions.

    Mark Thornton
    Mark Thornton, Dec 13, 2004
    #6
    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. Koen
    Replies:
    4
    Views:
    376
    Sudsy
    Oct 27, 2004
  2. J.K. Becker
    Replies:
    43
    Views:
    987
  3. joanne matthews (RRes-Roth)
    Replies:
    9
    Views:
    278
  4. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,253
    Tim Rentsch
    Sep 23, 2005
  5. Agix
    Replies:
    7
    Views:
    329
    dorayme
    May 10, 2007
Loading...

Share This Page