Boost logo

Boost :

From: Jason Hise (0xchaos_at_[hidden])
Date: 2006-10-29 15:23:31


On 10/29/06, Daryle Walker <darylew_at_[hidden]> wrote:
> Couldn't there be better approximate fractions to use?
>

I'm not sure if there were other questions implied by the ommited
portions of your post, but my best guess is that you are looking for
an algorithm which builds a good fit rational number from a float
without resorting to a simple and inefficient decimal truncation. If
this is what you are looking for then what might work is a function
which builds a rational number as a continued fraction.

Essentially you strip off the integer part of your rational number,
take the inverse of the rest, strip off the integer part, take the
inverse, and keep going until you hit a depth you like (3 or 4 is
usually plenty). When you hit the maximum depth use zero as an
approximation for the fraction built so far and pass it back up. Each
level then considers the rational returned to it to be the inverse of
part of the fraction it was building, so it flips the numerator and
denominator and adds the whole part it stripped off.

A greater depth will give you greater accuracy, but you also need to
be careful of hitting a depth where extremely small numbers (usually
the result of floating point error) will result in overflowing your
integer type.

Hope this helps

-Jason


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk