Converting a decimal string to mpq_t

James Cloos cloos at jhcloos.com
Wed Apr 30 22:09:29 CEST 2025


>>>>> "AC" == American Citizen <website.reader3 at gmail.com> writes:

AC> initial Q = 0
AC> S = 0.1
AC> F = 0.1e0
AC> Q =
AC> 627710173538668076383578942320766641610235544446403451289/6277101735386680763835789423207666416102355444464034512896

when going the route earlier suggested in this thread, it would
be useful to look at the algorithm the various lisp and scheme
implementations use for the fuction 'rationalize'.

the result you got is instead consistent with lisp's similar function 'rational'.

this page has a good explanation of scheme's take on rationalize:

 https://edoras.sdsu.edu/doc/mit-scheme-9.2/mit-scheme-ref/Numerical-operations.html

common lisp's version only takes a single argument.

alternatively, one can do something like: convert the part of the string
before the point to an (integral) mpq, convert the part after the point
to another mpq, divid the latter by the power of ten matching the length
of that string part, and taking the sum of the two mpq values.

if the string was in exponential format, one'd the need to mutiply the
previous mpq sum by the specified power of ten.

-JimC
-- 
James Cloos <cloos at jhcloos.com>
            OpenPGP: https://jhcloos.com/0x997A9F17ED7DAEA6.asc


More information about the gmp-discuss mailing list