On Mon, Sep 30, 2013 at 5:33 AM, Naga Vijay <lists / ruby-forum.com> wrote:
> Hello Fellow Rubyists,
>
> Just wanted to introduce/disclose programming using "Prior Knowledge"
> based on domain knowledge.
>
> This note in https://github.com/nvijayap/mathematics is self-explanatory

I beg to differ.  It tells me nothing about why I should be using that
gem.  What's the advantage of using Mathematics.average over doing it
yourself - it's a one liner anyway?  (Btw. the error for empty lists
of numbers might be seen as a bit mysterious.)

> IMPORTANT NOTE:
> --------------
> This program introduces/relies on "Prior Knowledge"
> in the domain of mathematics, and avoids
> computation where unnecessary.

Why do you use #method_missing for #sin and #cos?  The implementation
of the two methods is in no way dynamic.  You could just code them as
regular methods. That way you would also get error reporting in case
someone passes more than one argument to sin and cos.  It's a mystery
to me why you call a variable which holds degrees "rad" (aka
"radiants").

Also, "case" might be seen as a bit inefficient.  You could use a Hash
for better efficiency:

irb(main):004:0> sin = Hash.new {|h,x|
Math.sin(x)}.merge!(0=>0,Math::PI/2=>1,Math::PI=>0,Math::PI*3/4=>-1)
=> {0=>0, 1.5707963267948966=>1, 3.141592653589793=>0, 2.356194490192345=>-1}
irb(main):005:0> sin[Math::PI]
=> 0

Question is whether it's worth the added overhead.  For all values not
"prior known" you get the old behavior anyway.  And if you want nice
(rounded) output, there's printf (see below).

> Also, resorting to computation sometimes doesn't yield
> perfect results as known/anticipated. Example:
> (environment: ruby 2.0.0p247 (2013-06-27 revision 41674)
> [x86_64-darwin12.4.0])
>
> $ ruby -e "puts Math.sin(Math::PI) # I expect 0"
> 1.2246467991473532e-16

Yes, but

$ ruby -e 'printf "%.3f\n", Math.sin(Math::PI)'
0.000

> You may be able to use to good effect in your programs.

Hm...

Cheers

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/