Re: rational to floating point?

nmm1@cus.cam.ac.uk (Nick Maclaren)
13 Oct 2003 15:24:38 -0400

          From comp.compilers

Related articles
rational to floating point? thant@acm.org (Thant Tessman) (2003-10-13)
Re: rational to floating point? mitr@volny.cz (Miloslav Trmac) (2003-10-13)
Re: rational to floating point? nmm1@cus.cam.ac.uk (2003-10-13)
Re: rational to floating point? haberg@matematik.su.se (2003-10-13)
Re: rational to floating point? thant@acm.org (Thant Tessman) (2003-10-14)
Re: rational to floating point? fjh@cs.mu.oz.au (Fergus Henderson) (2003-10-18)
Re: rational to floating point? nmm1@cus.cam.ac.uk (2003-10-18)
Re: rational to floating point? Peter-Lawrence.Montgomery@cwi.nl (2003-10-18)
Re: rational to floating point? thant@acm.org (Thant Tessman) (2003-10-27)
| List of all articles for this month |
From: nmm1@cus.cam.ac.uk (Nick Maclaren)
Newsgroups: comp.compilers
Date: 13 Oct 2003 15:24:38 -0400
Organization: University of Cambridge, England
References: 03-10-065
Keywords: arithmetic
Posted-Date: 13 Oct 2003 15:24:37 EDT

Thant Tessman <thant@acm.org> 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
conversion".


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.


Regards,
Nick Maclaren.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.