Converting a decimal string to mpq_t

Marc Glisse marc.glisse at inria.fr
Tue Apr 29 23:27:39 CEST 2025


On Mon, 28 Apr 2025, Brett Kuntz wrote:

> Use mpf_set_str() to convert the string to an mpf, then use mpq_set_f() to convert the mpf to an mpq.

I don't think that answers the request: "0.1" is exactly 1/10, but cannot 
be represented exactly as a mpf.

> There is also an mpq_get_str() function but I've never used it before so 
> I am not sure if it can handle decimal numbers in base 10.

(probably mpq_set_str if we want to go from string to mpq)
It doesn't.

---

  From: Anders Andersson

> Do I have to write my own parser or do you have any ideas? Have I
> missed something obvious?

CGAL has its own read_float_or_quotient, so you wouldn't be the first one 
with such a need (although CGAL uses the same function for non-GMP types).

> If I manage to summon the strength to write such a function, would it
> be a good idea to extend mpq_set_str to handle floating point input?

I don't have a strong opinion about the best way to add that 
functionality: extending mpq_set_str, extending gmp_scanf (only with %Qf 
or similar but not with %Qi?), adding a new function...
And then, should it also accept hex-floats (what printf outputs with %a) 
and more general input?

Other opinions are welcome.

-- 
Marc Glisse


More information about the gmp-discuss mailing list