To anyone out there who is engaged in coding these sorts of things a lot,

I would strongly recommend that they at the very least try reading a

book like Forman S. Acton’s “Numerical Methods that Work” and/or W.H.

Press (et. al.) “Numerical Recipes” before they proceed. I’ve had

these kinds of things bite me in the past, developing answers that

were totally wrong, even though the formulae in use were apparently

correct. And they would have been, had my floating point numbers been

arbitrary precision numbers instead! It’s issues such as this that

usually make it better and safer to use well-tested numerics libraries

where they exist rather than rolling your own.

I fully agree with you. I’d like to add that there is, among others,

gsl and its ruby bindings rb-gsl that can do this job for you.

Since Ruby is object-oriented, using fractions instead of floats

can be an option, also (but if you use numerical libraries, it’s mostly

because you want to crunch a huge amount of numbers fast,

and don’t care too much about exact results ;-).

Even more sophisticated things can be done, using some more

advanced algebra, if you want to represent things like the

square root of two correctly. (No float or fraction can do that,

and the first ancient Greek who noticed this was reportedly killed

for noticing this and thus rebelling against the dogma of the

Pythagorean faith he adhered to. It is said that as a consequence,

the Pythagorean community split up into “hearers” and “mathematicians”

as a result of this incident.)

You can represent 2.sqrt it as a root of the Polynomial x**2-2, i.e., a
solution of x**2-2=0.

Polynomials are implemented in Ruby here:

*http://www.math.kobe-u.ac.jp/~kodama/tips-RubyPoly.html*

(http://www.math.kobe-u.ac.jp/~kodama/tips-RubyPoly.html)

Best regards,

Axel