Converting a decimal string to mpq_t

Anders Andersson pipatron at gmail.com
Thu May 1 11:52:44 CEST 2025


On Thu, May 1, 2025 at 11:15 AM Vincent Lefevre <vincent at vinc17.net> wrote:
>
> On 2025-04-29 07:30:22 +0200, Anders Andersson wrote:
> > Neither will work, 0.1 is even an example in the mpf documentation:
> > "The mantissa is stored in binary. One consequence of this is that
> > decimal fractions like 0.1 cannot be represented exactly"
> >
> > And as I hinted to in my original message, mpq_set_str does not handle
> > floating point strings (according to its manual).
>
> 0.1 is a notation that was used in math before floating point existed:
> https://en.wikipedia.org/wiki/Decimal#History
>
> IMHO, it would be worth to support it in mpq_set_str, independently
> of the notion of floating point.

Thank you, I agree.

In fact, I agree enough that I am right now writing the code. I spent
some time thinking and writing a document outlining differences
between different functions such as strtod, mpf_set_str, mpfr_strtofr
etc.

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.

// Anders


More information about the gmp-discuss mailing list