Converting a decimal string to mpq_t
Anders Andersson
pipatron at gmail.com
Tue Apr 29 07:30:22 CEST 2025
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).
// Anders
On Mon, Apr 28, 2025 at 11:34 PM Brett Kuntz <kuntz at shaw.ca> wrote:
>
> Use mpf_set_str() to convert the string to an mpf, then use mpq_set_f() to convert the mpf to an mpq. 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. Try out both methods!
>
> https://gmplib.org/manual/index
>
> -Brett
>
>
>
>
> ________________________________
> From: "Anders Andersson" <pipatron at gmail.com>
> To: "gmp-discuss" <gmp-discuss at gmplib.org>
> Sent: Monday, April 28, 2025 2:54:34 PM
> Subject: Converting a decimal string to mpq_t
>
> Good evening! I wanted to convert a simple decimal string such as
> "0.1" to an mpq_t but I can't find a way to do it even though an mpq_t
> should be able to represent every such number exactly in every base,
> except possibly NaN and Inf.
>
> Do I have to write my own parser or do you have any ideas? Have I
> missed something obvious?
>
> 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?
>
>
> // Anders
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
More information about the gmp-discuss
mailing list