On Thu, May 12, 2011 at 2:54 PM, serialhex <serialhex / gmail.com> wrote:
> ...
> instead of using floats you can try to use rational numbers in ruby:
> ruby-1.9.2-p180 :001 > require 'mathn'
which also changes this:
  (-1) ** (1.0 / 3) #=> NaN
to:
  (-1) ** (1.0 / 3) #=> (0.5+0.866025403784439i)
so we can also get complex roots if we want them!

> ...
> one last thing: if your inputs are floats i'm not sure there is a nice way
> to convert them to rationals:
>   (1.0/3).to_r  #=> (6004799503160661/18014398509481984)
I think that is a nice way! It's telling us (I think) what is the
exact rational represented by the float. After all, a float is just a
special type of rational, or rather floats are a subset of the subset
of the rationals defined by them have powers of two as denominators in
their representation using the smallest positive denominator. (I hope
that's correct.)

Whether it's a *good* idea to be able to convert floats to "ordinary"
rationals is another question, and I'm sceptical about that: I think
one should probably only want to do it if one fully understands
floating point representation, and if one fully understands floating
point representation, I suspect one wouldn't want to do it!