Re: rational to floating point

tmk@netvision.net.il (Michael Tiomkin)
14 Mar 2003 11:51:23 -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: tmk@netvision.net.il (Michael Tiomkin)
Newsgroups: comp.compilers
Date: 14 Mar 2003 11:51:23 -0500
Organization: http://groups.google.com/
References: 03-03-035
Keywords: arithmetic
Posted-Date: 14 Mar 2003 11:51:23 EST

Thant Tessman <thant@acm.org> wrote in message news:03-03-035...
> ... Currently the interpreter displays rational
> numbers as the ratio of two integers. 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? ...


> Is my reasoning sound? Is there a simpler test?


    I think it's wrong, while your base equation, b^n = c * d, is right.
Let a rational number be normalized, i.e. gcd(n,d) is 1 and d > 0
(n is numerator and d is denominator). When the base b is equal
to p_1^n_1 *...* p_k^n_k, p_i are prime numbers and n_i>0,
the number can be represented as a finite fp number with base b
if and only if d is a multiplication of powers of p_1,...,p_k.
  In other words, the stream of digits will be infinite if d is divisible
by a prime number that does not divide b.


    In the terms of prime factors, the stream of digits is finite if
and only if the set of prime factors of d is a subset of the set
of prime factors of b.


    By the way, why can't you print your number with repeating stream
of digits, like 1/3 in decimal notaion as 0.(3)^* ?
In this case you'll always be able to print a rational number
as a floating point number.


    Michael


Post a followup to this message

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