Converting a decimal string to mpq_t

sisyphus sisyphus359 at gmail.com
Thu May 1 12:47:58 CEST 2025


On Thu, May 1, 2025 at 8:01 PM Anders Andersson <pipatron at gmail.com> wrote:

> [snip]
> The short version of my 141 line README:
>  - New function mpq_strtoq() is most flexible, won't change old code
>  - Inspired by mpfr_strtofr() already compatible with strtod()
>  - Can replace all GMP input if requested (scanf,set_str,...)
>
> I hope to publish it online in a few days, maybe it can be useful for
> others but my hope is to get some version of it into GMP.
>

I look forward to seeing it.
I take it that the "0.1" could be 1/2 or 1/3 or 1/4 or ... or 1/61 or 1/62,
depending upon the base ?
And you'll also be accepting scientific notation like "1e-1" ?
(The "e" is a bit of a problemc if the base is greater than 14, so I guess
you would then resort to a symbol that is never numeric, irrespective of
base.)

I've already implemented this behaviour in the perl module Math::GMPq, but
only as base 10 strings:

$ perl -MMath::GMPq -le 'print Math::GMPq->new("2.014e-2");' # wraps
mpq_set_str()
1007/50000

$ perl -MMath::GMPq -le 'print Math::GMPq->new(2.014e-2);' # wraps
mpq_set_d()
5804959775695475/288230376151711744

I'm now tempted to extend the handling of strings to cover the full base
range of 2 to 62 ;-)

Cheers,
Rob


More information about the gmp-discuss mailing list