initialize a rational from a decimal string
Torbjorn Granlund
tg at gmplib.org
Tue Mar 12 18:57:03 CET 2013
edA-qa mort-ora-y <eda-qa at disemia.com> writes:
I'm looking for a way to initialize a rational from a decimal string. I
thought it might be a common task but I don't see any function which
would do this. Is there such a function?
Examples:
0.97 => 97/100
1.5 => 3/2
3.89e100 => 389/1
Hm, are you sure about that last calculation? :-)
What I do now is parse the three parts of the decimal (XXX.YYYeZZZ) and
then manually assemble the bits. This involves a lot of calculations
however, in particular that fractional part as I need to loop over the
numbers multiplying by 10. Is there a better way?
We don't provide a function for this. Perhaps we should.
Instead of repeatedly multiplying by 10, I'd suggest that you multiply
by a suitable power of 10, which you'll need to figure out first.
It does not matter too much if the power is minimal, as long as it is
not very much too large.
--
Torbjörn
More information about the gmp-discuss
mailing list