From: Roy Pardee Date: 2009-03-17T11:57:35+09:00 Subject: Re: upping numerical precision On Mar 16, 2:06 pm, Siep Korteling wrote: > rpar...@gmail.com wrote: > > Hey All, > (...) > >   chi = -2.0 * Math.log(p) > > > I'm finding that p is often going to 0.0 b/c the numbers returned by > > calc_prob are sometimes outlandishly small > (...) > > That works, but makes me wonder if there's a smarter thing to do w/ > > those rational and mathn libs to really get the effect I hoped for > > just from including them in my script. > > > Is there? > > > Many thanks! > > > -Roy > > Are you sure you required 'mathn' before defining your calc_prob method? > > big = 10**100 > > small = 1/big > p small.zero? # true > > require 'mathn' > small = 1/big > p small.zero? # false > p small.class # Rational > > p -2.0*Math.log(small) > > hth, > > Siep Thanks for the response! I think the issue may be that I'm not doing any division--just multiplication. Check it out: irb(main):001:0> require 'mathn' => true irb(main):002:0> x = 0.5 => 0.5 irb(main):003:0> 1000.times do irb(main):004:1* x *= x irb(main):005:1> end => 1000 irb(main):006:0> x => 0.0 irb(main):007:0> x.class => Float irb(main):008:0> But the more I think about it, the more I think I'm fussing over nothing (ha ha!). I think if my p var goes to zero, I should just set it = Float::MIN & break out of that loop. My calc_prob method will only ever return values <= 1, so there's no sense in letting it continue to spin down the value of p (if you can tell what I'm trying to say). Thanks! -Roy