flt gem for arbitrary precision

Discussion in 'Ruby' started by Giampiero Zanchi, Apr 28, 2010.

  1. ciao a tutti (hi to hall)

    I just discoverd a gem to have arbitrary precision numbers and want to
    share with the forum.

    Install the gem with

    gem install flt

    an example script follows; it computes the square root of 2

    require 'flt'
    include Flt

    DecNum.context.precision = 200

    p DecNum(2).sqrt

    produces:

    DecNum('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605715')

    documentation here
    http://flt.rubyforge.org/

    hope this helps
    --
    Posted via http://www.ruby-forum.com/.
     
    Giampiero Zanchi, Apr 28, 2010
    #1
    1. Advertising

  2. Giampiero Zanchi

    Roger Pack Guest

    > DecNum.context.precision = 200
    >
    > p DecNum(2).sqrt
    >
    > produces:
    >
    > DecNum('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605715')


    Cool.
    I think you can do something similar with the builtin BigDecimal class
    require 'bigdecimal'
    >> BigDecimal.new("2").sqrt(200)

    => #<BigDecimal:3076900,'0.1414213562 3730950488 0168872420 9698078569
    6718753769 4807317667 9737990732 4784621070 3885038753 4327641572
    7350138462 3091229702 4924836055 8507372126 4412149709 9935831413
    2226659275 0559275579 9950501152 7820605714 7010955997
    1605970125E1',224(224)>

    Of course, that's decimal, so flt might be more precise.

    -rp
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Apr 28, 2010
    #2
    1. Advertising

  3. On 04/28/2010 11:31 AM, Giampiero Zanchi wrote:
    > ciao a tutti (hi to hall)
    >
    > I just discoverd a gem to have arbitrary precision numbers and want to
    > share with the forum.
    >
    > Install the gem with
    >
    > gem install flt
    >
    > an example script follows; it computes the square root of 2
    >
    > require 'flt'
    > include Flt
    >
    > DecNum.context.precision = 200
    >
    > p DecNum(2).sqrt
    >
    > produces:
    >
    > DecNum('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605715')
    >
    > documentation here
    > http://flt.rubyforge.org/


    What's the difference (precision? performance?) vs. doing

    irb(main):001:0> require 'bigdecimal'
    => true
    irb(main):002:0> BigDecimal("2").sqrt(200).to_s
    =>
    "0.1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970125E1"
    irb(main):003:0> BigDecimal("2").sqrt(200)
    => #<BigDecimal:93215a4,'0.1414213562 3730950488 0168872420 9698078569
    6718753769 4807317667 9737990732 4784621070 3885038753 4327641572
    7350138462 3091229702 4924836055 8507372126 4412149709 9935831413
    2226659275 0559275579 9950501152 7820605714 7010955997
    1605970125E1',224(224)>
    irb(main):004:0>

    ?

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 28, 2010
    #3
  4. Robert Klemme wrote:

    > What's the difference (precision? performance?) vs. doing
    >
    > irb(main):001:0> require 'bigdecimal'
    > => true
    > irb(main):002:0> BigDecimal("2").sqrt(200).to_s
    > =>
    > "0.1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970125E1"
    > irb(main):003:0> BigDecimal("2").sqrt(200)
    > => #<BigDecimal:93215a4,'0.1414213562 3730950488 0168872420 9698078569
    > 6718753769 4807317667 9737990732 4784621070 3885038753 4327641572
    > 7350138462 3091229702 4924836055 8507372126 4412149709 9935831413
    > 2226659275 0559275579 9950501152 7820605714 7010955997
    > 1605970125E1',224(224)>
    > irb(main):004:0>
    >
    > ?
    >
    > Kind regards
    >
    > robert


    maybe no difference ?
    I didn't know of BigDecimal neither
    sorry for my ignorance
    --
    Posted via http://www.ruby-forum.com/.
     
    Giampiero Zanchi, Apr 29, 2010
    #4
  5. 2010/4/29 Giampiero Zanchi <>:
    > Robert Klemme wrote:
    >
    >> What's the difference (precision? performance?) vs. doing
    >>
    >> irb(main):001:0> require 'bigdecimal'
    >> => true
    >> irb(main):002:0> BigDecimal("2").sqrt(200).to_s
    >> =>
    >> "0.1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970125E1"
    >> irb(main):003:0> BigDecimal("2").sqrt(200)
    >> => #<BigDecimal:93215a4,'0.1414213562 3730950488 0168872420 9698078569
    >> 6718753769 4807317667 9737990732 4784621070 3885038753 4327641572
    >> 7350138462 3091229702 4924836055 8507372126 4412149709 9935831413
    >> 2226659275 0559275579 9950501152 7820605714 7010955997
    >> 1605970125E1',224(224)>
    >> irb(main):004:0>
    >>
    >> ?

    >
    > maybe no difference ?
    > I didn't know of BigDecimal neither
    > sorry for my ignorance


    No problem. Maybe the gem you've found is a nice wrapper around
    BigDecimal with better #inspect and #to_s. In any case I thought it
    good to know that the standard lib contains something similar. :)

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 29, 2010
    #5
  6. you can find the section

    DecNum vs BigDecimal

    at the bottom of the documentation page

    http://flt.rubyforge.org/

    DecNum solves some of the difficulties of using BigDecimal.

    One of the major problems with BigDecimal is that it‘s not easy to
    control the number of significant digits of the results. While addition,
    subtraction and multiplication are exact (unless a limit is used),
    divisions will need to be passed precision explicitly or else an
    indeterminate number of significant digits will be lost. Part of the
    problem is that numbers don‘t keep track of its precision (0.1000 is not
    distinguishable from 0.1.)

    .....
    --
    Posted via http://www.ruby-forum.com/.
     
    Giampiero Zanchi, Apr 29, 2010
    #6
  7. Robert Klemme wrote:
    > On 04/28/2010 11:31 AM, Giampiero Zanchi wrote:
    >> ciao a tutti (hi to hall)
    >>
    >> I just discoverd a gem to have arbitrary precision numbers and want to
    >> share with the forum.
    >>
    >> Install the gem with
    >>
    >> gem install flt
    >>
    >> an example script follows; it computes the square root of 2
    >>
    >> require 'flt'
    >> include Flt
    >>
    >> DecNum.context.precision = 200
    >>
    >> p DecNum(2).sqrt
    >>
    >> produces:
    >>
    >> DecNum('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605715')
    >>
    >>
    >> documentation here
    >> http://flt.rubyforge.org/

    >
    > What's the difference (precision? performance?) vs. doing
    >
    > irb(main):001:0> require 'bigdecimal'
    > => true
    > irb(main):002:0> BigDecimal("2").sqrt(200).to_s
    > =>
    > "0.1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970125E1"
    >
    > irb(main):003:0> BigDecimal("2").sqrt(200)
    > => #<BigDecimal:93215a4,'0.1414213562 3730950488 0168872420 9698078569
    > 6718753769 4807317667 9737990732 4784621070 3885038753 4327641572
    > 7350138462 3091229702 4924836055 8507372126 4412149709 9935831413
    > 2226659275 0559275579 9950501152 7820605714 7010955997
    > 1605970125E1',224(224)>
    > irb(main):004:0>
    >
    > ?
    >
    > Kind regards
    >
    > robert
    >

    If my reading of the documentation is correct the difference is how the
    intermediate results are stored. Dividing 1 by 3 with a precision of 2
    would result in 0.33 versus 0.3333333... If you print a commission rate
    of 33.33% on a form and a charge of $5,0000, you want the amount to show
    $1,666.50 (5000 * .3333), not $1,666.67 (5,0000 * 1/3) as a lot of
    people will complain that your figures are wrong.
    I use fixed precision for all my work as it deals with money and
    everything has to match to the penny.
     
    Michael W Ryder, Apr 29, 2010
    #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. Thinkit
    Replies:
    8
    Views:
    527
    Thinkit
    Dec 24, 2003
  2. Jack
    Replies:
    2
    Views:
    384
    Alf P. Steinbach
    Dec 10, 2003
  3. Honestmath
    Replies:
    5
    Views:
    559
    Honestmath
    Dec 13, 2004
  4. Replies:
    2
    Views:
    423
    Alf P. Steinbach
    Dec 13, 2004
  5. =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

    Arbitrary precision decimal numbers

    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Apr 21, 2005, in forum: C++
    Replies:
    5
    Views:
    524
    Alex Vinokur
    Apr 26, 2005
Loading...

Share This Page