Re: rational to floating point?

Fergus Henderson <fjh@cs.mu.oz.au>
18 Oct 2003 15:27:32 -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: Fergus Henderson <fjh@cs.mu.oz.au>
Newsgroups: comp.compilers
Date: 18 Oct 2003 15:27:32 -0400
Organization: Compilers Central
References: 03-10-065 03-10-080
Keywords: arithmetic
Posted-Date: 18 Oct 2003 15:27:32 EDT

Thant Tessman <thant@acm.org> writes:


>I think I have a handle on how to produce the significand and
>exponent of the floating point number (as arbitrary-precision integers
>(conveniently in base 256 in my implementation)). What's missing is
>the conversion to the actual float. The Clinger paper makes use of a
>mysteriously unexplained "make-float" function, for which there seems
>to be no portable manifestation.


The C standard provides a function ldexp() which should be helpful for
this sort of thing. Assuming you are doing this in C, here's a possible
algorithm:


1. Convert the significand from your arbitrary precision integer type
      to a fixed precision integer type with at least as many bits as
      DBL_MANT_DIG. "long long" will have enough for IEC/IEEE double
      precision floating point. (The 1999 C standard requires that
      "long long" be supported and have at least 64 bits. If your C compiler
      doesn't support "long long" yet, complain to your C compiler vendor,
      or use GNU C.)


2. Cast the significand from the fixed precision integer type to double.
      Your C compiler ought to be able to do this without losing precision
      (if it doesn't, complain to your C compiler vendor).


3. Convert the exponent from your arbitrary precision integer type to "int".


4. Call ldexp() with the values obtained in steps 2 and 3.


I haven't actually tried this, so I don't know whether it really works
in practice. It's possible that many C compilers might have problems
with losing precision in step 2.


--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.


Post a followup to this message

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