problem when converting from double

Thomas Abraham thomaskabraham at gmail.com
Fri Jan 28 16:16:57 CET 2005


hi, 

I am a beginner with GMP so this may sound stupid : 

I am trying to convert a double type to rational and then find the
numerator and denominator integers. so i do
*******************************
mpq_t val;
double g;
mpz_t num,den;
signed long int valnum, valden;

mpq_init(val);mpz_init(num);mpz_init(den);
g=0.333;
mpq_set_d(val,g);

mpq_get_num(num,val);
mpq_get_den(den,val);

valnum=mpz_get_si(num);
valden=mpz_get_si(den);

*******************************************

now if I set g=0.5 , I get valnum=1,valden=2 . 
if i set g=0.333 , i get junk. 
if i use mpz_fits_slong_p(num) , it gives me zero in the second case. 

Is there a problem with my compiler? Is my code totally wrong? Is
there an easier way to do this? I looked at the manual about 10 times
but could not find out how to get the numerator and denominator for
certain double values as mentioned above.

any suggestions would be greatly appreciated

thanks

Tom

thomaskabraham at gmail.com


More information about the gmp-discuss mailing list