Converting a decimal string to mpq_t

Brett Kuntz kuntz at shaw.ca
Mon Apr 28 23:34:53 CEST 2025


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