On Sat, Dec 20, 2008 at 5:29 AM, Brian Candler <b.candler / pobox.com> wrote:

> However, using BigDecimal, the answer is remarkably different:
>
>  require "bigdecimal"
>  puts f(BigDecimal.new("77167.0"),BigDecimal.new("33096.0"))
>  # => 9.15359360631475e+34
>
> That's a sign plus 35 orders of magnitude different :-)

When I use BigDecimal all the way through and keep things clean in
terms of groupings (explicit parens around *everything*), I get what
appears to be the correct answer:

require 'bigdecimal'

SCALE = 100

AAA = BigDecimal.new("333.75", SCALE)
BBB = BigDecimal.new("11.0", SCALE)
CCC = BigDecimal.new("121.0", SCALE)
DDD = BigDecimal.new("2.0", SCALE)
EEE = BigDecimal.new("5.5", SCALE)

def fun(x,y)
  puts  ( (AAA - (x**2)) * (y**6) ) +
        ( (x**2) * ((BBB * (x**2) * (y**2)) - (CCC * (y**4)) - DDD )) +
        ( EEE * (y**8) ) +
        ( x / (DDD * y) )
end

fun(BigDecimal.new("77617.0", SCALE), BigDecimal.new("33096.0", SCALE))



Gives:

-0.8273960599468213681411650954798162919990331157843848199178148416727096930142615421803239062122310853275320280396422528402224E0

Is there something I'm missing? Seems like it would be pretty safe to
use BigDecimal in this case.

 -Michael



-- 
Michael C. Libby
www.mikelibby.com