bad maths

Discussion in 'Ruby' started by Martin larsson, May 19, 2004.

  1. Hi

    i = 0
    100.times {
    p i += 0.6
    }

    results in:
    5.6
    5.7
    5.8
    5.9
    5.99999999999999
    6.09999999999999
    6.19999999999999

    on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install it
    if that matters

    anyone more have this problem? anyway to fix it?
     
    Martin larsson, May 19, 2004
    #1
    1. Advertising

  2. Martin larsson wrote:

    > Hi
    >
    > i = 0
    > 100.times {
    > p i += 0.6
    > }
    >
    > results in:
    > 5.6
    > 5.7
    > 5.8
    > 5.9
    > 5.99999999999999
    > 6.09999999999999
    > 6.19999999999999
    >
    > on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install
    > it if that matters
    >
    > anyone more have this problem? anyway to fix it?
    >
    >

    sry, i was testing around with different numbers so i copied wrong
    result for that litte script, just change 0.6 to 0.1 and the the result
    is right
     
    Martin larsson, May 19, 2004
    #2
    1. Advertising

  3. Martin larsson

    Hal Fulton Guest

    Martin larsson wrote:
    > Hi
    >
    > i = 0
    > 100.times {
    > p i += 0.6
    > }
    >
    > results in:
    > 5.6
    > 5.7
    > 5.8
    > 5.9
    > 5.99999999999999
    > 6.09999999999999
    > 6.19999999999999
    >
    > on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install it
    > if that matters
    >
    > anyone more have this problem? anyway to fix it?


    Look for a thread about a week or so ago. :)

    I suggest you round off the numbers for output and/or for
    calculations. And use integer arithmetic wherever you can.


    Hal
     
    Hal Fulton, May 20, 2004
    #3
  4. Martin larsson

    Lloyd Zusman Guest

    Martin larsson <> writes:

    > Martin larsson wrote:
    >
    >> Hi
    >>
    >> i = 0
    >> 100.times {
    >> p i += 0.6
    >> }
    >>
    >> results in:
    >> 5.6
    >> 5.7
    >> 5.8
    >> 5.9
    >> 5.99999999999999
    >> 6.09999999999999
    >> 6.19999999999999
    >>
    >> on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install
    >> it if that matters
    >>
    >> anyone more have this problem? anyway to fix it?
    >>
    >>

    > sry, i was testing around with different numbers so i copied wrong
    > result for that litte script, just change 0.6 to 0.1 and the the result
    > is right


    First, look up "floating point arithmetic" on Google (include the
    double quotes in your search).

    This link is also quite useful:

    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    Then, read the recent thread in this list called "Major addition bug?".
    This thread starts with the following message ID:

    <>


    --
    Lloyd Zusman

    God bless you.
     
    Lloyd Zusman, May 20, 2004
    #4
  5. Martin larsson wrote:

    > Hi


    Moin!

    > i = 0
    > 100.times {
    > p i += 0.6
    > }


    Try this:

    100.times { |i| p (i + 1) * 0.6 }

    Regards,
    Florian Gross
     
    Florian Gross, May 20, 2004
    #5
  6. Martin larsson

    Bret Jolly Guest

    Martin larsson <> wrote in message news:<>...
    > Martin larsson wrote:

    [repeated adding of 0.1 not coming out exact]

    The problem is that the computer thinks in binary and
    0.1 has an infinitely long binary representation. (You
    won't see this "problem" if you instead add 0.5, which
    has a finitely long representation) This is not a ruby
    problem, but a limitation of the computer's floating point.
    In general, you should regard floating point numbers as
    being slightly inexact. If you need exactness, require
    the mathn library and use rationals. Imitating your example,
    you'd get something like:

    require 'mathn'
    i = 5
    11.times {puts i += 1/10} # => exact rational results

    As an alternative, you could limit the precision of your
    output, which effectively rounds the numbers to output
    precision:

    i = 5.5
    5.times { puts "%5.3f" % (i += 0.1)}
    #output follows:
    5.600
    5.700
    5.800
    5.900
    6.000

    Regards, Bret
    Bret Jolly (Jou Lii Bair)
     
    Bret Jolly, May 20, 2004
    #6
  7. Martin larsson

    Bret Jolly Guest

    A small addendum to my previous answer. If you need exact decimal
    arithmetic (as you do if the decimals represent dollars and cents)
    you should use the BigDecimal class. That works in base 10 rather
    than base 2. (Actually, it works in base 10000, which is mathematically
    equivalent to base 10.) Imitating your example:
    require 'bigdecimal'
    a = BigDecimal("5.5", 20) # the 20 indicates the precision
    5.times {puts (a += BigDecimal("0.1")).to_s}
    # output follows:
    0.56E1
    0.57E1
    0.58E1
    0.59E1
    0.6E1

    No nonsense about 5.99999... here.

    The BigDecimal class is useful when you want the arithmetic to
    be decimal rather than binary, and especially when you need extra
    precision beyond what floating-point can achieve. It is, however,
    slower than floating point, a bit trickier to use (since you have
    to set precision or make sure the implicit settings are acceptable)
    and its method library is less rich than what the Math module
    provides for Float.

    Regards, Bret
     
    Bret Jolly, May 20, 2004
    #7
    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. Ross Clement (Email address invalid - do not use)

    Maths expression drawing library

    Ross Clement (Email address invalid - do not use), Jan 11, 2006, in forum: Java
    Replies:
    4
    Views:
    604
    Vyacheslav Levitsky
    Oct 2, 2006
  2. Calum Maciver

    Maths Involved

    Calum Maciver, Feb 18, 2006, in forum: Java
    Replies:
    5
    Views:
    461
    Luke Webber
    Feb 19, 2006
  3. Andrew Bullock

    bit of a maths quesiton

    Andrew Bullock, Feb 5, 2005, in forum: C++
    Replies:
    2
    Views:
    310
    Victor Bazarov
    Feb 5, 2005
  4. Geoff

    Maths in HTML

    Geoff, Jan 4, 2007, in forum: HTML
    Replies:
    10
    Views:
    722
    Adrienne Boswell
    Jan 6, 2007
  5. rantingrick
    Replies:
    44
    Views:
    1,230
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page