|rational to floating point firstname.lastname@example.org (Thant Tessman) (2003-03-09)|
|Re: rational to floating point email@example.com (David Chase) (2003-03-14)|
|Re: rational to floating point firstname.lastname@example.org (2003-03-14)|
|Re: rational to floating point email@example.com (Thant Tessman) (2003-03-14)|
|Re: rational to floating point firstname.lastname@example.org (Joachim Durchholz) (2003-03-14)|
|Re: rational to floating point email@example.com (Arthur J. O'Dwyer) (2003-03-14)|
|Re: rational to floating point firstname.lastname@example.org (Glen Herrmannsfeldt) (2003-03-14)|
|[3 later articles]|
|From:||Thant Tessman <email@example.com>|
|Date:||9 Mar 2003 17:34:36 -0500|
|Posted-Date:||09 Mar 2003 17:34:36 EST|
This is a math question, but one I hope is relevant to this newsgroup.
I've been writing an interpreter for a functional programming language
in C++. One of the datatypes it supports is an arbitrary-precision
rational number type. 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? What I've come up with
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. 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.
Is my reasoning sound? Is there a simpler test?
For the curious, a description and source for my interpreter can be
My gratitude goes to folks in this newsgroup who have helped improve my
understanding of concepts I put to use building this.
Return to the
Search the comp.compilers archives again.