|rational to floating point? firstname.lastname@example.org (Thant Tessman) (2003-10-13)|
|Re: rational to floating point? email@example.com (Miloslav Trmac) (2003-10-13)|
|Re: rational to floating point? firstname.lastname@example.org (2003-10-13)|
|Re: rational to floating point? email@example.com (2003-10-13)|
|Re: rational to floating point? firstname.lastname@example.org (Thant Tessman) (2003-10-14)|
|Re: rational to floating point? email@example.com (Fergus Henderson) (2003-10-18)|
|Re: rational to floating point? firstname.lastname@example.org (2003-10-18)|
|Re: rational to floating point? Peter-Lawrence.Montgomery@cwi.nl (2003-10-18)|
|Re: rational to floating point? email@example.com (Thant Tessman) (2003-10-27)|
|From:||firstname.lastname@example.org (Nick Maclaren)|
|Date:||13 Oct 2003 15:24:38 -0400|
|Organization:||University of Cambridge, England|
|Posted-Date:||13 Oct 2003 15:24:37 EDT|
Thant Tessman <email@example.com> writes:
|> Can anyone recommend a source of information on converting
|> arbitrary-precision rationals to floating-point numbers? I'm sure I
|> could make something work, but doing it accurately, efficiently, and
|> portably seems a little tricky. Google turns up lots of man pages of
|> languages that support the capability, but a description of *how* they
|> do it is harder to unearth.
Find someone of appropriate age (probably over 60) and experience,
and start picking his or her brains :-(
Most of the work on that was done in the days when computer software
was being developed by people who used it, and not computer scientists.
If things were written up at all, it was typically in local technical
reports, which are hard to locate unless you already have a precise
reference and can visit the site.
The basic technique is to convert to fixed point, with the same base
as the floating-point, round to the appropriate precision and fiddle
the formats. The bulk of the time goes in the first step, and it is
probably written up in some computer science books - look for "base
Anyway, you start by choosing a power of the base that will fit in
a suitable integer. When converting a fraction of less than one,
you multiply the numerator by that, until dividing by the denominator
gives a positive integer. Remove that and repeat.
Converting a fraction of greater than that power can be done by
multiplying up the denominator by that power until the dividend is
within range, but there are better methods. I can't remember them
offhand - sorry.
Note that you don't need to do the operations in any more precision
than is necessary to get the result and the correct rounding. This
property can be used to make the procedure quite fast, but it is
definitely tricky to code if you want it fast and accurate.
Return to the
Search the comp.compilers archives again.