Increase significant digits in Float

Discussion in 'Ruby' started by Jason Lillywhite, Mar 2, 2010.

  1. If I want to increase my significant digits beyond 15 in a result of a
    math expression involving floats, how do I do it? So, for example, I
    would like more than 15 sig digits in the result of:

    irb(main):001:0> 4.005 / 7
    => 0.572142857142857

    Thank you!
    --
    Posted via http://www.ruby-forum.com/.
     
    Jason Lillywhite, Mar 2, 2010
    #1
    1. Advertising

  2. On Tue, Mar 2, 2010 at 4:26 PM, Jason Lillywhite
    <> wrote:
    > If I want to increase my significant digits beyond 15 in a result of a
    > math expression involving floats, how do I do it? So, for example, I
    > would like more than 15 sig digits in the result of:
    >
    > irb(main):001:0> 4.005 / 7
    > => 0.572142857142857


    irb is calling inspect on the value of the expression. It's the
    inspect method the one that truncates to 15 digits when it creates the
    string. You can create your own strings with greater precision like
    this:

    irb(main):001:0> result = 4.005 / 7
    => 0.572142857142857
    irb(main):002:0> "%.20f" % result
    => "0.57214285714285717521"
    irb(main):003:0> "%.30f" % result
    => "0.572142857142857175212213860505"

    Check the String#% method:

    http://ruby-doc.org/core/classes/String.html#M000770

    and the Kernel#sprintf method:

    http://ruby-doc.org/core/classes/Kernel.html#M005962

    Jesus.
     
    Jesús Gabriel y Galán, Mar 2, 2010
    #2
    1. Advertising

  3. Jesús Gabriel y Galán wrote:

    > irb is calling inspect on the value of the expression. It's the
    > inspect method the one that truncates to 15 digits when it creates the
    > string.


    Thank you! I forgot that IRB is returning a truncated string to the
    screen.

    --
    Posted via http://www.ruby-forum.com/.
     
    Jason Lillywhite, Mar 2, 2010
    #3
  4. Jason Lillywhite

    Xavier Noria Guest

    On Tue, Mar 2, 2010 at 5:00 PM, Jason Lillywhite
    <> wrote:

    > Thank you! I forgot that IRB is returning a truncated string to the
    > screen.


    That 15 is hard-coded in the definition of Float#to_s.
     
    Xavier Noria, Mar 2, 2010
    #4
  5. Xavier Noria wrote:
    > On Tue, Mar 2, 2010 at 5:00 PM, Jason Lillywhite
    > <> wrote:
    >
    >> Thank you! I forgot that IRB is returning a truncated string to the
    >> screen.

    >
    > That 15 is hard-coded in the definition of Float#to_s.


    Yes, and exposing more digits is not accurate:

    puts "%.70f"%(4.005 / 7)
    #=>0.5721428571428571752122138605045620352029800415039062500000000000000000
    require 'bigdecimal'
    a = BigDecimal.new("4.005")
    b = a/7
    puts b
    #=>0.572142857142857142857143E0
    p b.precs
    #=>[24, 32]


    #first number is the number of significant digits in b; the second
    number is the maximum precision my system can handle (not sure about
    this). The float is bogus after the 16th digit.

    hth,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Mar 2, 2010
    #5
  6. Siep Korteling wrote:
    > #first number is the number of significant digits in b; the second
    > number is the maximum precision my system can handle (not sure about
    > this). The float is bogus after the 16th digit.


    Thank you Siep!

    This is interesting - and good to know.

    --
    Posted via http://www.ruby-forum.com/.
     
    Jason Lillywhite, Mar 2, 2010
    #6
  7. Jason Lillywhite

    Neve Le Guest

    Siep Korteling wrote:
    >> Xavier Noria wrote:
    >> That 15 is hard-coded in the definition of Float#to_s.

    >
    > Yes, and exposing more digits is not accurate:
    > The float is bogus after the 16th digit.



    Working with big numbers here, it seems that precision is only
    guaranteed to 15 digits on either side of the decimal. When a whole
    number >15 digits is divided the decimal is approximated, and anything
    >17 digits prints a float of n.0 no matter how it's "%n.nf" formatted.



    Is the hard-coded 15 just a "safe" limit? Would I be able to recompile
    ruby with a higher definition? If possible, how high could I go?
    --
    Posted via http://www.ruby-forum.com/.
     
    Neve Le, Jun 30, 2010
    #7
  8. Neve Le wrote:
    > Is the hard-coded 15 just a "safe" limit? Would I be able to recompile
    > ruby with a higher definition? If possible, how high could I go?


    No, it's a limit of IEEE double-precision floating-point representation.

    If you want more digits of precision, use BigDecimal.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jun 30, 2010
    #8
  9. Giampiero Zanchi, Jun 30, 2010
    #9
  10. Jason Lillywhite

    Guest

    On Wed, Jun 30, 2010 at 8:06 AM, Neve Le <> wrote:
    > Siep Korteling wrote:
    >>> Xavier Noria wrote:
    >>> That 15 is hard-coded in the definition of Float#to_s.

    >>
    >> Yes, and exposing more digits is not accurate:
    >> The float is bogus =A0after the 16th digit.

    >
    > Working with big numbers here, it seems that precision is only
    > guaranteed to 15 digits on either side of the decimal. When a whole
    > number >15 digits is divided the decimal is approximated, and anything
    >>17 digits prints a float of n.0 no matter how it's "%n.nf" formatted.


    http://ruby-doc.org/core/classes/Float.html
    "Float objects represent real numbers using the native architecture=91s
    double-precision floating point representation."

    http://en.wikipedia.org/wiki/Double_precision_floating-point_format

    You could try BigDecimal.
     
    , Jun 30, 2010
    #10
  11. You may also want to take a look at http://flt.rubyforge.org/

    It's an implementation of arbitrary precision floating point numbers
    (decimal and binary). If you don't need it to be fast it has some
    advantages over BigDecimal.
    --
    Posted via http://www.ruby-forum.com/.
     
    Javier Goizueta, Jun 30, 2010
    #11
  12. [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jun 30, 2010 at 3:56 PM, Javier Goizueta <>wrote:

    > You may also want to take a look at http://flt.rubyforge.org/
    >
    > It's an implementation of arbitrary precision floating point numbers
    > (decimal and binary). If you don't need it to be fast it has some
    > advantages over BigDecimal.
    > --
    > Posted via http://www.ruby-forum.com/.
    >

    Also: I recall a short thread from December 2008:
    Proposing an arbitrary precision class building on BigDecimal and being
    derived from Numeric
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/322100
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/322174 and
    subsequent
    or as a user friendly all in one place:
    http://groups.google.com/group/comp...d/thread/66f505b9460464cc/e9a0142b118d66a8?q=
    with links to:
    https://rubyforge.org/projects/appmath/
    http://appmath.rubyforge.org/
    http://www.ulrichmutze.de/index.html

    (I should observe that I haven't tried this code: I simply made a mental
    note at the time of the thread that it might be useful.)
     
    Colin Bartlett, Jul 1, 2010
    #12
    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. David Corby

    Round float to X significant digits

    David Corby, May 1, 2004, in forum: C++
    Replies:
    8
    Views:
    30,000
    Siemel Naran
    May 2, 2004
  2. bd
    Replies:
    0
    Views:
    634
  3. SMH
    Replies:
    0
    Views:
    233
  4. Roy Smith

    Significant digits in a float?

    Roy Smith, Apr 28, 2014, in forum: Python
    Replies:
    65
    Views:
    269
    Adam Funk
    May 8, 2014
  5. Ned Batchelder

    Re: Significant digits in a float?

    Ned Batchelder, Apr 28, 2014, in forum: Python
    Replies:
    2
    Views:
    59
    Ned Batchelder
    Apr 28, 2014
Loading...

Share This Page