issue with GMP library, a calculation issue

iulian mira iani_3d at yahoo.co.uk
Fri Nov 14 14:18:31 UTC 2014


Hi

my main purpose to use this library is to make calculations with very big numbers (precision like 4096 bits), and at the end to convert decimal number to fraction form, using my own function. This is just a piece from an app I'm working. I have tried to work with mpq_t (rational numbers) to get fractions, but no success, so I decided to use only mpf_t type. 
In my own function that converts a decimal to fraction, I use strings.

BUT I found an issue which makes me drop this library, for example I set a variable of mpf_t type with double value 0.1224 , mpf_set_d(mpf_value, 0.1224);
and when I get a string from the mpf_value with the function gmp_sprintf(text, "%.*Ff",1240, value); (the text is and array[2048])  I don't get the 0.1224 but 0.122399999999999994915000000(0)....
I specially setted 1240 the number of characters I need for string text, because sometimes I have different numbers that fills the entire string and and after that calculate the fractional form correctly.
So I'm unable to get the correct fraction form from decimal 0.1224 because of the other INCORRECT digits, most of them are trailing nines and then some others, and that happen every time I have such a "narrow" not large number like 0.1224.

Is there a way to disable this kind of rounding? or to make the mpf_value to be exact as I am setting it, in this case 0.1224 ?
i also tried to make some calculation with mpf_t types, like 153/1250 to get 0.1224, but no luck, I got those incorrect digits (that kind of rounding)
i also tried to get string with function mpf_get_str(), and also no luck, I got the same inccorect digits.
and i also tried gmp_sprintf(text, "%.Ff", value) without specified precision digits, but that cuts my string, so it's useless.

I need big precision like 4096, having a number like below with large set of repeating digits I can extract the fraction of 1/391 with that precision otherwise I can not 0.0025575447570332480818414322250639386189258312020460358056265984654731457800511508951406649616368286445012787723785166240409207161125319693094629156010230179028132992327365728900255754475703324808184143222506393861892583120204603580562659846547314578005115089514066496163682864450127877237851662404092071611253196930946291560102301790281329923273657289
0025575447570332480818414322250639386189258312020460358056265984654731457800511508951406649616368286445012787723785166240409207161125319693094629156010230179028132992327365728900255754475703324808184143222506393861892583120204603580562659846547314578005115089514066496163682864450127877237851662404092071611253196930946291560102301790281329923273657289
0025575447570332480818414322250639386189258312020460358056265984654731457800511508951406649616368286445012787723785166240409207161125319693094629156010230179028132992327365728900255754475703324808184143222506393861892583120204603580562659846547314578005115089514066496163682864450127877237851662404092071611253196930946291560102301790281329923273657289
0025575447570332480818414322250639386189258312020460358056265984654731457800511508951406649616368286445012787723785166240409207161125319693094629156010230179028132992327365728900255754475703324808184143222506393861892583120204603580562659846547314578005115089514066496163682864450127877237851662404092071611253196930946291560102301790281329923273657289

Please let me know if is there a way to get strings as exactly as I am setting them, to get 0.1224 but not 0.122399999999999994915000000(0)....
Thanks
Iulian


More information about the gmp-discuss mailing list