# Re: rational to floating point

## John Stracke <francis@thibault.org>14 Mar 2003 11:54:25 -0500

From comp.compilers

Related articles
[3 earlier articles]
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: John Stracke Newsgroups: comp.compilers Date: 14 Mar 2003 11:54:25 -0500 Organization: Compilers Central References: 03-03-035 Keywords: arithmetic Posted-Date: 14 Mar 2003 11:54:25 EST

Thant Tessman wrote:
> 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.

No, that doesn't work; consider writing 5/6 in base 10 (0.8333...).
gcd(6,10)=2.

The logic you want is that it's a repeating decimal if d has any prime
factors which do not divide into b. I'm not sure offhand of an
efficient way to do this test, though.

However, you might consider that a better way to cope with repeating
decimals is to print them as repeating decimals. A useful tidbit to
remember is that l, the length of the repeating part, divides d-1 (for
example, 1/7 is 0.[142857], where the 6 digits in [] repeat). Given
that, you may be able to come up with a good algorithm for the printing.

--
/=============================================================\
|John Stracke | http://www.thibault.org |HTML OK |
|Francois Thibault |========================================|
|East Kingdom |So what's the gene for belief in genetic|
|francis@thibault.org|determinism? |
\=============================================================/

Post a followup to this message