Re: mpf_get_str
adammaj1
adammaj1 at o2.pl
Sun Oct 6 15:53:30 CEST 2013
Hi,
> In what way are you discontent with the result?
I would like to fix exponent to 0 ( zero )
then from 1/6 I would get : 0.0010101010101010101010101010101010101
Improved version of my program ( all is below) :
gmp_printf (" mantissa of binary floating ( string ) : %s \n", sf); //
gmp_printf (" exponent : %ld \n", exponent); //
printf ("binary floating number in exponential form =0.%s*%d^%ld\n", sf,b, exponent);
What do you think about adding some expanded examples to documentation of mpf_get_str :
The output of the GMP programme is in the form mantissa,'e',exponent where the value is
0.mantissa * 2^exponent
GMP represents the floating point numbers as a pair of exponent and mantissa (and sign).
The generated string is a fraction, with an implicit radix point immediately to the left of the first digit.
Trailing zeros are not returned.
First example, the number 3.1416 would be returned as string "31416" and exponent 1 so :
3.1416 = 0.31416*10^1
Another example : 1/6 as a exponential form of binary number will be :
mantissa = 101010101010101010101010101010101010101010101010101010101010101011
exponent = -2
so 1/6 ( decimal) will be binary 0.101010101010101010101010101010101010101010101010101010101010101011 * 2 ^(-2) = 0.0(01)
Adam
=====================
/*
C programme
gcc r.c -lgmp
./a.out
http://gmplib.org/manual/Rational-Number-Functions.html#Rational-Number-Functions
a rational number 1 / 6 is in a canonical form ( decimal fraction) : 1/6
numerator of rational number = 1
denominator of rational number = 6
binary rational ( string ) : 1/110
decimal floating point number : 0.166666666666666666667
binary floating ( string ) : 101010101010101010101010101010101010101010101010101010101010101011
exponent : -2
f=101010101010101010101010101010101010101010101010101010101010101011f*2^-2
should be 0.0(01)
*/
#include <stdio.h>
#include <gmp.h>
// input = num/den
unsigned int num = 1;
unsigned int den = 6;
int main ()
{
mpq_t q; // rational number;
int b =2 ; // base of numeral system
mpz_t n ;
mpz_t d ;
mpf_t f ;
char *sr;
char *sf;
mp_exp_t exponent ; // holds the exponent for the result string
// init and set variables
mpq_init (q); // Initialize r and set it to 0/1.
mpf_init (f);
mpz_init_set_ui(n,num);
mpz_init_set_ui(d,den);
mpq_set_num(q,n);
mpq_set_den(q,d);
mpq_canonicalize (q); // It is the responsibility of the user to canonicalize the assigned variable before any arithmetic operations are performed on that variable.
mpf_set_q(f,q);
// convertions
sr = mpq_get_str (NULL, b, q); // rational number = fraction : from decimal to binary
// If str is NULL, the result string is allocated using the current allocation function
sf = mpf_get_str (NULL, &exponent, b, 0, f); // floating point number : from decimal to binary
// If n_digits is 0 then that accurate maximum number of digits are generated.
// print
gmp_printf ("a rational number %Zd / %Zd is in a canonical form ( decimal fraction) : %Qd\n",n,d, q); //
gmp_printf(" numerator of rational number = %Zd \n", mpq_numref(q)); //
gmp_printf(" denominator of rational number = %Zd \n", mpq_denref(q)); //
gmp_printf (" binary rational ( string ) : %s \n", sr); //
gmp_printf (" decimal floating point number : %.Ff \n", f); //
gmp_printf (" mantissa of binary floating ( string ) : %s \n", sf); //
gmp_printf (" exponent : %ld \n", exponent); //
printf ("binary floating number in exponential form =0.%s*%d^%ld\n", sf,b, exponent);
// clear memory
mpq_clear (q);
mpz_clear (n);
mpz_clear (d);
mpf_clear (f);
return 0;
}
More information about the gmp-discuss
mailing list