Re: rational to floating point

"Glen Herrmannsfeldt" <gah@ugcs.caltech.edu>
14 Mar 2003 11:37:46 -0500

          From comp.compilers

Related articles
rational to floating point thant@acm.org (Thant Tessman) (2003-03-09)
Re: rational to floating point chase@theworld.com (David Chase) (2003-03-14)
Re: rational to floating point nmm1@cus.cam.ac.uk (2003-03-14)
Re: rational to floating point thant@acm.org (Thant Tessman) (2003-03-14)
Re: rational to floating point joachim_d@gmx.de (Joachim Durchholz) (2003-03-14)
Re: rational to floating point ajo@andrew.cmu.edu (Arthur J. O'Dwyer) (2003-03-14)
Re: rational to floating point gah@ugcs.caltech.edu (Glen Herrmannsfeldt) (2003-03-14)
Re: rational to floating point tmk@netvision.net.il (2003-03-14)
Re: rational to floating point Peter-Lawrence.Montgomery@cwi.nl (2003-03-14)
Re: rational to floating point francis@thibault.org (John Stracke) (2003-03-14)
| List of all articles for this month |

From: "Glen Herrmannsfeldt" <gah@ugcs.caltech.edu>
Newsgroups: comp.compilers
Date: 14 Mar 2003 11:37:46 -0500
Organization: AT&T Broadband
References: 03-03-035
Keywords: arithmetic
Posted-Date: 14 Mar 2003 11:37:46 EST

(snip)


> I'd like it to display rational
> numbers as floating-point numbers whenever the conversion to floating
> point won't produce an infinite (repeating) stream of digits.
>
> The question is: Under what conditions will a rational number produce
> an infinite stream of digits for a given base? What I've come up with
> is this:
>
> Converting a rational number to a floating point value is equivalent
> to multiplying the numerator and denominator by some number that
> converts the denominator to a whole-number power of the base. That is:
> b^n = c * d where 'b' is the base, 'd' is the denominator, and 'n' and
> 'c' are some whole numbers that satisfy the equation.
>
> I think there is no solution to the above equation if the denominator
> of the original rational number and the base contain no prime factors
> in common.


I think you understand, but wrote the wrong thing.


The prime factors of the denominator must all be prime factors of the
base. Consider the decimal fraction 1/10. The prime factors of the
denominator are 2 and 5. Its representation is terminating in any
base containing prime factors of both 2 and 5. In binary, octal, or
hexadecimal, with only prime factor of 2, it is repeating. In base
20, 30, 40, or 50, for example, it is terminating.


Note that the base can have extra prime factors.


> And I think that this in turn implies that if and only if gcd(d,b)
> is 1 and 'd' is not 1, then the original rational number can only be
> represented by an infinite stream of digits.


If, but not only if. First, note that gcd(10,2) is 2, but 1/10th can
only be represented as a repeating binary fraction. Also note that
any terminating fraction can also be represented as a repeating
fraction, with trailing (b-1) digits.


(snip)


-- glen


Post a followup to this message

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