This is gmp.info, produced by makeinfo version 4.0 from gmp.texi. INFO-DIR-SECTION GNU libraries START-INFO-DIR-ENTRY * gmp: (gmp). GNU Multiple Precision Arithmetic Library. END-INFO-DIR-ENTRY This file documents GNU MP, a library for arbitrary-precision arithmetic. Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.  File: gmp.info, Node: Integer Exponentiation, Next: Integer Roots, Prev: Integer Division, Up: Integer Functions Exponentiation Functions ======================== - Function: void mpz_powm (mpz_t ROP, mpz_t BASE, mpz_t EXP, mpz_t MOD) - Function: void mpz_powm_ui (mpz_t ROP, mpz_t BASE, unsigned long int EXP, mpz_t MOD) Set ROP to (BASE raised to EXP) `mod' MOD. If EXP is negative, the result is undefined. - Function: void mpz_pow_ui (mpz_t ROP, mpz_t BASE, unsigned long int EXP) - Function: void mpz_ui_pow_ui (mpz_t ROP, unsigned long int BASE, unsigned long int EXP) Set ROP to BASE raised to EXP. The case of 0^0 yields 1.  File: gmp.info, Node: Integer Roots, Next: Number Theoretic Functions, Prev: Integer Exponentiation, Up: Integer Functions Root Extraction Functions ========================= - Function: int mpz_root (mpz_t ROP, mpz_t OP, unsigned long int N) Set ROP to the truncated integer part of the Nth root of OP. Return non-zero if the computation was exact, i.e., if OP is ROP to the Nth power. - Function: void mpz_sqrt (mpz_t ROP, mpz_t OP) Set ROP to the truncated integer part of the square root of OP. - Function: void mpz_sqrtrem (mpz_t ROP1, mpz_t ROP2, mpz_t OP) Set ROP1 to the truncated integer part of the square root of OP, like `mpz_sqrt'. Set ROP2 to OP-ROP1*ROP1, (i.e., zero if OP is a perfect square). If ROP1 and ROP2 are the same variable, the results are undefined. - Function: int mpz_perfect_power_p (mpz_t OP) Return non-zero if OP is a perfect power, i.e., if there exist integers A and B, with B > 1, such that OP equals a raised to b. Return zero otherwise. - Function: int mpz_perfect_square_p (mpz_t OP) Return non-zero if OP is a perfect square, i.e., if the square root of OP is an integer. Return zero otherwise.  File: gmp.info, Node: Number Theoretic Functions, Next: Integer Comparisons, Prev: Integer Roots, Up: Integer Functions Number Theoretic Functions ========================== - Function: int mpz_probab_prime_p (mpz_t N, int REPS) If this function returns 0, N is definitely not prime. If it returns 1, then N is `probably' prime. If it returns 2, then N is surely prime. Reasonable values of reps vary from 5 to 10; a higher value lowers the probability for a non-prime to pass as a `probable' prime. The function uses Miller-Rabin's probabilistic test. - Function: int mpz_nextprime (mpz_t ROP, mpz_t OP) Set ROP to the next prime greater than OP. This function uses a probabilistic algorithm to identify primes, but for for practical purposes it's adequate, since the chance of a composite passing will be extremely small. - Function: void mpz_gcd (mpz_t ROP, mpz_t OP1, mpz_t OP2) Set ROP to the greatest common divisor of OP1 and OP2. The result is always positive even if either of or both input operands are negative. - Function: unsigned long int mpz_gcd_ui (mpz_t ROP, mpz_t OP1, unsigned long int OP2) Compute the greatest common divisor of OP1 and OP2. If ROP is not `NULL', store the result there. If the result is small enough to fit in an `unsigned long int', it is returned. If the result does not fit, 0 is returned, and the result is equal to the argument OP1. Note that the result will always fit if OP2 is non-zero. - Function: void mpz_gcdext (mpz_t G, mpz_t S, mpz_t T, mpz_t A, mpz_t B) Compute G, S, and T, such that AS + BT = G = `gcd'(A, B). If T is `NULL', that argument is not computed. - Function: void mpz_lcm (mpz_t ROP, mpz_t OP1, mpz_t OP2) Set ROP to the least common multiple of OP1 and OP2. - Function: int mpz_invert (mpz_t ROP, mpz_t OP1, mpz_t OP2) Compute the inverse of OP1 modulo OP2 and put the result in ROP. Return non-zero if an inverse exists, zero otherwise. When the function returns zero, ROP is undefined. - Function: int mpz_jacobi (mpz_t OP1, mpz_t OP2) - Function: int mpz_legendre (mpz_t OP1, mpz_t OP2) Compute the Jacobi and Legendre symbols, respectively. OP2 should be odd and must be positive. - Function: int mpz_si_kronecker (long A, mpz_t B); - Function: int mpz_ui_kronecker (unsigned long A, mpz_t B); - Function: int mpz_kronecker_si (mpz_t A, long B); - Function: int mpz_kronecker_ui (mpz_t A, unsigned long B); Calculate the value of the Kronecker/Jacobi symbol (A/B), with the Kronecker extension (a/2)=(2/a) when a odd, or (a/2)=0 when a even. All values of A and B give a well-defined result. See Henri Cohen, section 1.4.2, for more information (*note References::). See also the example program `demos/qcn.c' which uses `mpz_kronecker_ui'. - Function: unsigned long int mpz_remove (mpz_t ROP, mpz_t OP, mpz_t F) Remove all occurrences of the factor F from OP and store the result in ROP. Return the multiplicity of F in OP. - Function: void mpz_fac_ui (mpz_t ROP, unsigned long int OP) Set ROP to OP!, the factorial of OP. - Function: void mpz_bin_ui (mpz_t ROP, mpz_t N, unsigned long int K) - Function: void mpz_bin_uiui (mpz_t ROP, unsigned long int N, unsigned long int K) Compute the binomial coefficient N over K and store the result in ROP. Negative values of N are supported by `mpz_bin_ui', using the identity bin(-n,k) = (-1)^k * bin(n+k-1,k) (see Knuth volume 1 section 1.2.6 part G). - Function: void mpz_fib_ui (mpz_t ROP, unsigned long int N) Compute the Nth Fibonacci number and store the result in ROP.  File: gmp.info, Node: Integer Comparisons, Next: Integer Logic and Bit Fiddling, Prev: Number Theoretic Functions, Up: Integer Functions Comparison Functions ==================== - Function: int mpz_cmp (mpz_t OP1, mpz_t OP2) Compare OP1 and OP2. Return a positive value if OP1 > OP2, zero if OP1 = OP2, and a negative value if OP1 < OP2. - Macro: int mpz_cmp_ui (mpz_t OP1, unsigned long int OP2) - Macro: int mpz_cmp_si (mpz_t OP1, signed long int OP2) Compare OP1 and OP2. Return a positive value if OP1 > OP2, zero if OP1 = OP2, and a negative value if OP1 < OP2. These functions are actually implemented as macros. They evaluate their arguments multiple times. - Function: int mpz_cmpabs (mpz_t OP1, mpz_t OP2) - Function: int mpz_cmpabs_ui (mpz_t OP1, unsigned long int OP2) Compare the absolute values of OP1 and OP2. Return a positive value if OP1 > OP2, zero if OP1 = OP2, and a negative value if OP1 < OP2. - Macro: int mpz_sgn (mpz_t OP) Return +1 if OP > 0, 0 if OP = 0, and -1 if OP < 0. This function is actually implemented as a macro. It evaluates its arguments multiple times.  File: gmp.info, Node: Integer Logic and Bit Fiddling, Next: I/O of Integers, Prev: Integer Comparisons, Up: Integer Functions Logical and Bit Manipulation Functions ====================================== These functions behave as if two's complement arithmetic were used (although sign-magnitude is used by the actual implementation). - Function: void mpz_and (mpz_t ROP, mpz_t OP1, mpz_t OP2) Set ROP to OP1 logical-and OP2. - Function: void mpz_ior (mpz_t ROP, mpz_t OP1, mpz_t OP2) Set ROP to OP1 inclusive-or OP2. - Function: void mpz_xor (mpz_t ROP, mpz_t OP1, mpz_t OP2) Set ROP to OP1 exclusive-or OP2. - Function: void mpz_com (mpz_t ROP, mpz_t OP) Set ROP to the one's complement of OP. - Function: unsigned long int mpz_popcount (mpz_t OP) For non-negative numbers, return the population count of OP. For negative numbers, return the largest possible value (MAX_ULONG). - Function: unsigned long int mpz_hamdist (mpz_t OP1, mpz_t OP2) If OP1 and OP2 are both non-negative, return the hamming distance between the two operands. Otherwise, return the largest possible value (MAX_ULONG). It is possible to extend this function to return a useful value when the operands are both negative, but the current implementation returns MAX_ULONG in this case. *Do not depend on this behavior, since it will change in a future release.* - Function: unsigned long int mpz_scan0 (mpz_t OP, unsigned long int STARTING_BIT) Scan OP, starting with bit STARTING_BIT, towards more significant bits, until the first clear bit is found. Return the index of the found bit. - Function: unsigned long int mpz_scan1 (mpz_t OP, unsigned long int STARTING_BIT) Scan OP, starting with bit STARTING_BIT, towards more significant bits, until the first set bit is found. Return the index of the found bit. - Function: void mpz_setbit (mpz_t ROP, unsigned long int BIT_INDEX) Set bit BIT_INDEX in ROP. - Function: void mpz_clrbit (mpz_t ROP, unsigned long int BIT_INDEX) Clear bit BIT_INDEX in ROP. - Function: int mpz_tstbit (mpz_t OP, unsigned long int BIT_INDEX) Check bit BIT_INDEX in OP and return 0 or 1 accordingly.  File: gmp.info, Node: I/O of Integers, Next: Integer Random Numbers, Prev: Integer Logic and Bit Fiddling, Up: Integer Functions Input and Output Functions ========================== Functions that perform input from a stdio stream, and functions that output to a stdio stream. Passing a `NULL' pointer for a STREAM argument to any of these functions will make them read from `stdin' and write to `stdout', respectively. When using any of these functions, it is a good idea to include `stdio.h' before `gmp.h', since that will allow `gmp.h' to define prototypes for these functions. - Function: size_t mpz_out_str (FILE *STREAM, int BASE, mpz_t OP) Output OP on stdio stream STREAM, as a string of digits in base BASE. The base may vary from 2 to 36. Return the number of bytes written, or if an error occurred, return 0. - Function: size_t mpz_inp_str (mpz_t ROP, FILE *STREAM, int BASE) Input a possibly white-space preceded string in base BASE from stdio stream STREAM, and put the read integer in ROP. The base may vary from 2 to 36. If BASE is 0, the actual base is determined from the leading characters: if the first two characters are `0x' or `0X', hexadecimal is assumed, otherwise if the first character is `0', octal is assumed, otherwise decimal is assumed. Return the number of bytes read, or if an error occurred, return 0. - Function: size_t mpz_out_raw (FILE *STREAM, mpz_t OP) Output OP on stdio stream STREAM, in raw binary format. The integer is written in a portable format, with 4 bytes of size information, and that many bytes of limbs. Both the size and the limbs are written in decreasing significance order (i.e., in big-endian). The output can be read with `mpz_inp_raw'. Return the number of bytes written, or if an error occurred, return 0. The output of this can not be read by `mpz_inp_raw' from GMP 1, because of changes necessary for compatibility between 32-bit and 64-bit machines. - Function: size_t mpz_inp_raw (mpz_t ROP, FILE *STREAM) Input from stdio stream STREAM in the format written by `mpz_out_raw', and put the result in ROP. Return the number of bytes read, or if an error occurred, return 0. This routine can read the output from `mpz_out_raw' also from GMP 1, in spite of changes necessary for compatibility between 32-bit and 64-bit machines.  File: gmp.info, Node: Integer Random Numbers, Next: Miscellaneous Integer Functions, Prev: I/O of Integers, Up: Integer Functions Random Number Functions ======================= The random number functions of GMP come in two groups; older function that rely on a global state, and newer functions that accept a state parameter that is read and modified. Please see the *Note Random Number Functions:: for more information on how to use and not to use random number functions. - Function: void mpz_urandomb (mpz_t ROP, gmp_randstate_t STATE, unsigned long int N) Generate a uniformly distributed random integer in the range 0 to 2^N - 1, inclusive. The variable STATE must be initialized by calling one of the `gmp_randinit' functions (*Note Random State Initialization::) before invoking this function. - Function: void mpz_urandomm (mpz_t ROP, gmp_randstate_t STATE, mpz_t N) Generate a uniform random integer in the range 0 to N - 1, inclusive. The variable STATE must be initialized by calling one of the `gmp_randinit' functions (*Note Random State Initialization::) before invoking this function. - Function: void mpz_rrandomb (mpz_t ROP, gmp_randstate_t STATE, unsigned long int N) Generate a random integer with long strings of zeros and ones in the binary representation. Useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger corner-case bugs. The random number will be in the range 0 to 2^N - 1, inclusive. The variable STATE must be initialized by calling one of the `gmp_randinit' functions (*Note Random State Initialization::) before invoking this function. - Function: void mpz_random (mpz_t ROP, mp_size_t MAX_SIZE) Generate a random integer of at most MAX_SIZE limbs. The generated random number doesn't satisfy any particular requirements of randomness. Negative random numbers are generated when MAX_SIZE is negative. This function is obsolete. Use `mpz_urandomb' or `mpz_urandomm' instead. - Function: void mpz_random2 (mpz_t ROP, mp_size_t MAX_SIZE) Generate a random integer of at most MAX_SIZE limbs, with long strings of zeros and ones in the binary representation. Useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger corner-case bugs. Negative random numbers are generated when MAX_SIZE is negative. This function is obsolete. Use `mpz_rrandomb' instead.  File: gmp.info, Node: Miscellaneous Integer Functions, Prev: Integer Random Numbers, Up: Integer Functions Miscellaneous Functions ======================= - Function: int mpz_fits_ulong_p (mpz_t OP) - Function: int mpz_fits_slong_p (mpz_t OP) - Function: int mpz_fits_uint_p (mpz_t OP) - Function: int mpz_fits_sint_p (mpz_t OP) - Function: int mpz_fits_ushort_p (mpz_t OP) - Function: int mpz_fits_sshort_p (mpz_t OP) Return non-zero iff the value of OP fits in an `unsigned long int', `signed long int', `unsigned int', `signed int', `unsigned short int', or `signed short int', respectively. Otherwise, return zero. - Macro: int mpz_odd_p (mpz_t OP) - Macro: int mpz_even_p (mpz_t OP) Determine whether OP is odd or even, respectively. Return non-zero if yes, zero if no. These macros evaluate their arguments more than once. - Function: size_t mpz_size (mpz_t OP) Return the size of OP measured in number of limbs. If OP is zero, the returned value will be zero. - Function: size_t mpz_sizeinbase (mpz_t OP, int BASE) Return the size of OP measured in number of digits in base BASE. The base may vary from 2 to 36. The returned value will be exact or 1 too big. If BASE is a power of 2, the returned value will always be exact. This function is useful in order to allocate the right amount of space before converting OP to a string. The right amount of allocation is normally two more than the value returned by `mpz_sizeinbase' (one extra for a minus sign and one for the terminating '\0').  File: gmp.info, Node: Rational Number Functions, Next: Floating-point Functions, Prev: Integer Functions, Up: Top Rational Number Functions ************************* This chapter describes the GMP functions for performing arithmetic on rational numbers. These functions start with the prefix `mpq_'. Rational numbers are stored in objects of type `mpq_t'. All rational arithmetic functions assume operands have a canonical form, and canonicalize their result. The canonical from means that the denominator and the numerator have no common factors, and that the denominator is positive. Zero has the unique representation 0/1. Pure assignment functions do not canonicalize the assigned variable. It is the responsibility of the user to canonicalize the assigned variable before any arithmetic operations are performed on that variable. *Note that this is an incompatible change from version 1 of the library.* - Function: void mpq_canonicalize (mpq_t OP) Remove any factors that are common to the numerator and denominator of OP, and make the denominator positive. * Menu: * Initializing Rationals:: * Rational Arithmetic:: * Comparing Rationals:: * Applying Integer Functions:: * I/O of Rationals:: * Miscellaneous Rational Functions::  File: gmp.info, Node: Initializing Rationals, Next: Rational Arithmetic, Prev: Rational Number Functions, Up: Rational Number Functions Initialization and Assignment Functions ======================================= - Function: void mpq_init (mpq_t DEST_RATIONAL) Initialize DEST_RATIONAL and set it to 0/1. Each variable should normally only be initialized once, or at least cleared out (using the function `mpq_clear') between each initialization. - Function: void mpq_clear (mpq_t RATIONAL_NUMBER) Free the space occupied by RATIONAL_NUMBER. Make sure to call this function for all `mpq_t' variables when you are done with them. - Function: void mpq_set (mpq_t ROP, mpq_t OP) - Function: void mpq_set_z (mpq_t ROP, mpz_t OP) Assign ROP from OP. - Function: void mpq_set_ui (mpq_t ROP, unsigned long int OP1, unsigned long int OP2) - Function: void mpq_set_si (mpq_t ROP, signed long int OP1, unsigned long int OP2) Set the value of ROP to OP1/OP2. Note that if OP1 and OP2 have common factors, ROP has to be passed to `mpq_canonicalize' before any operations are performed on ROP. - Function: void mpq_swap (mpq_t ROP1, mpq_t ROP2) Swap the values ROP1 and ROP2 efficiently.  File: gmp.info, Node: Rational Arithmetic, Next: Comparing Rationals, Prev: Initializing Rationals, Up: Rational Number Functions Arithmetic Functions ==================== - Function: void mpq_add (mpq_t SUM, mpq_t ADDEND1, mpq_t ADDEND2) Set SUM to ADDEND1 + ADDEND2. - Function: void mpq_sub (mpq_t DIFFERENCE, mpq_t MINUEND, mpq_t SUBTRAHEND) Set DIFFERENCE to MINUEND - SUBTRAHEND. - Function: void mpq_mul (mpq_t PRODUCT, mpq_t MULTIPLIER, mpq_t MULTIPLICAND) Set PRODUCT to MULTIPLIER times MULTIPLICAND. - Function: void mpq_div (mpq_t QUOTIENT, mpq_t DIVIDEND, mpq_t DIVISOR) Set QUOTIENT to DIVIDEND/DIVISOR. - Function: void mpq_neg (mpq_t NEGATED_OPERAND, mpq_t OPERAND) Set NEGATED_OPERAND to -OPERAND. - Function: void mpq_inv (mpq_t INVERTED_NUMBER, mpq_t NUMBER) Set INVERTED_NUMBER to 1/NUMBER. If the new denominator is zero, this routine will divide by zero.  File: gmp.info, Node: Comparing Rationals, Next: Applying Integer Functions, Prev: Rational Arithmetic, Up: Rational Number Functions Comparison Functions ==================== - Function: int mpq_cmp (mpq_t OP1, mpq_t OP2) Compare OP1 and OP2. Return a positive value if OP1 > OP2, zero if OP1 = OP2, and a negative value if OP1 < OP2. To determine if two rationals are equal, `mpq_equal' is faster than `mpq_cmp'. - Macro: int mpq_cmp_ui (mpq_t OP1, unsigned long int NUM2, unsigned long int DEN2) Compare OP1 and NUM2/DEN2. Return a positive value if OP1 > NUM2/DEN2, zero if OP1 = NUM2/DEN2, and a negative value if OP1 < NUM2/DEN2. This routine allows that NUM2 and DEN2 have common factors. This function is actually implemented as a macro. It evaluates its arguments multiple times. - Macro: int mpq_sgn (mpq_t OP) Return +1 if OP > 0, 0 if OP = 0, and -1 if OP < 0. This function is actually implemented as a macro. It evaluates its arguments multiple times. - Function: int mpq_equal (mpq_t OP1, mpq_t OP2) Return non-zero if OP1 and OP2 are equal, zero if they are non-equal. Although `mpq_cmp' can be used for the same purpose, this function is much faster.  File: gmp.info, Node: Applying Integer Functions, Next: I/O of Rationals, Prev: Comparing Rationals, Up: Rational Number Functions Applying Integer Functions to Rationals ======================================= The set of `mpq' functions is quite small. In particular, there are few functions for either input or output. But there are two macros that allow us to apply any `mpz' function on the numerator or denominator of a rational number. If these macros are used to assign to the rational number, `mpq_canonicalize' normally need to be called afterwards. - Macro: mpz_t mpq_numref (mpq_t OP) - Macro: mpz_t mpq_denref (mpq_t OP) Return a reference to the numerator and denominator of OP, respectively. The `mpz' functions can be used on the result of these macros.  File: gmp.info, Node: I/O of Rationals, Next: Miscellaneous Rational Functions, Prev: Applying Integer Functions, Up: Rational Number Functions Input and Output Functions ========================== Functions that perform input from a stdio stream, and functions that output to a stdio stream. Passing a `NULL' pointer for a STREAM argument to any of these functions will make them read from `stdin' and write to `stdout', respectively. When using any of these functions, it is a good idea to include `stdio.h' before `gmp.h', since that will allow `gmp.h' to define prototypes for these functions. - Function: size_t mpq_out_str (FILE *STREAM, int BASE, mpq_t OP) Output OP on stdio stream STREAM, as a string of digits in base BASE. The base may vary from 2 to 36. Output is in the form `num/den' or if the denominator is 1 then just `num'. Return the number of bytes written, or if an error occurred, return 0.  File: gmp.info, Node: Miscellaneous Rational Functions, Prev: I/O of Rationals, Up: Rational Number Functions Miscellaneous Functions ======================= - Function: double mpq_get_d (mpq_t OP) Convert OP to a double. - Function: double mpq_set_d (mpq_t ROP, double D) Set ROP to the value of d, without rounding. These functions assign between either the numerator or denominator of a rational, and an integer. Instead of using these functions, it is preferable to use the more general mechanisms `mpq_numref' and `mpq_denref', together with `mpz_set'. - Function: void mpq_set_num (mpq_t RATIONAL, mpz_t NUMERATOR) Copy NUMERATOR to the numerator of RATIONAL. When this risks to make the numerator and denominator of RATIONAL have common factors, you have to pass RATIONAL to `mpq_canonicalize' before any operations are performed on RATIONAL. This function is equivalent to `mpz_set (mpq_numref (RATIONAL), NUMERATOR)'. - Function: void mpq_set_den (mpq_t RATIONAL, mpz_t DENOMINATOR) Copy DENOMINATOR to the denominator of RATIONAL. When this risks to make the numerator and denominator of RATIONAL have common factors, or if the denominator might be negative, you have to pass RATIONAL to `mpq_canonicalize' before any operations are performed on RATIONAL. *In version 1 of the library, negative denominators were handled by copying the sign to the numerator. That is no longer done.* This function is equivalent to `mpz_set (mpq_denref (RATIONAL), DENOMINATORS)'. - Function: void mpq_get_num (mpz_t NUMERATOR, mpq_t RATIONAL) Copy the numerator of RATIONAL to the integer NUMERATOR, to prepare for integer operations on the numerator. This function is equivalent to `mpz_set (NUMERATOR, mpq_numref (RATIONAL))'. - Function: void mpq_get_den (mpz_t DENOMINATOR, mpq_t RATIONAL) Copy the denominator of RATIONAL to the integer DENOMINATOR, to prepare for integer operations on the denominator. This function is equivalent to `mpz_set (DENOMINATOR, mpq_denref (RATIONAL))'.  File: gmp.info, Node: Floating-point Functions, Next: Low-level Functions, Prev: Rational Number Functions, Up: Top Floating-point Functions ************************ This chapter describes the GMP functions for performing floating point arithmetic. These functions start with the prefix `mpf_'. GMP floating point numbers are stored in objects of type `mpf_t'. The GMP floating-point functions have an interface that is similar to the GMP integer functions. The function prefix for floating-point operations is `mpf_'. There is one significant characteristic of floating-point numbers that has motivated a difference between this function class and other GMP function classes: the inherent inexactness of floating point arithmetic. The user has to specify the precision of each variable. A computation that assigns a variable will take place with the precision of the assigned variable; the precision of variables used as input is ignored. The precision of a calculation is defined as follows: Compute the requested operation exactly (with "infinite precision"), and truncate the result to the destination variable precision. Even if the user has asked for a very high precision, GMP will not calculate with superfluous digits. For example, if two low-precision numbers of nearly equal magnitude are added, the precision of the result will be limited to what is required to represent the result accurately. The GMP floating-point functions are _not_ intended as a smooth extension to the IEEE P754 arithmetic. Specifically, the results obtained on one computer often differs from the results obtained on a computer with a different word size. * Menu: * Initializing Floats:: * Assigning Floats:: * Simultaneous Float Init & Assign:: * Converting Floats:: * Float Arithmetic:: * Float Comparison:: * I/O of Floats:: * Miscellaneous Float Functions::  File: gmp.info, Node: Initializing Floats, Next: Assigning Floats, Prev: Floating-point Functions, Up: Floating-point Functions Initialization Functions ======================== - Function: void mpf_set_default_prec (unsigned long int PREC) Set the default precision to be *at least* PREC bits. All subsequent calls to `mpf_init' will use this precision, but previously initialized variables are unaffected. An `mpf_t' object must be initialized before storing the first value in it. The functions `mpf_init' and `mpf_init2' are used for that purpose. - Function: void mpf_init (mpf_t X) Initialize X to 0. Normally, a variable should be initialized once only or at least be cleared, using `mpf_clear', between initializations. The precision of X is undefined unless a default precision has already been established by a call to `mpf_set_default_prec'. - Function: void mpf_init2 (mpf_t X, unsigned long int PREC) Initialize X to 0 and set its precision to be *at least* PREC bits. Normally, a variable should be initialized once only or at least be cleared, using `mpf_clear', between initializations. - Function: void mpf_clear (mpf_t X) Free the space occupied by X. Make sure to call this function for all `mpf_t' variables when you are done with them. Here is an example on how to initialize floating-point variables: { mpf_t x, y; mpf_init (x); /* use default precision */ mpf_init2 (y, 256); /* precision _at least_ 256 bits */ ... /* Unless the program is about to exit, do ... */ mpf_clear (x); mpf_clear (y); } The following three functions are useful for changing the precision during a calculation. A typical use would be for adjusting the precision gradually in iterative algorithms like Newton-Raphson, making the computation precision closely match the actual accurate part of the numbers. - Function: void mpf_set_prec (mpf_t ROP, unsigned long int PREC) Set the precision of ROP to be *at least* PREC bits. Since changing the precision involves calls to `realloc', this routine should not be called in a tight loop. - Function: unsigned long int mpf_get_prec (mpf_t OP) Return the precision actually used for assignments of OP. - Function: void mpf_set_prec_raw (mpf_t ROP, unsigned long int PREC) Set the precision of ROP to be *at least* PREC bits. This is a low-level function that does not change the allocation. The PREC argument must not be larger that the precision previously returned by `mpf_get_prec'. It is crucial that the precision of ROP is ultimately reset to exactly the value returned by `mpf_get_prec' before the first call to `mpf_set_prec_raw'.  File: gmp.info, Node: Assigning Floats, Next: Simultaneous Float Init & Assign, Prev: Initializing Floats, Up: Floating-point Functions Assignment Functions ==================== These functions assign new values to already initialized floats (*note Initializing Floats::). - Function: void mpf_set (mpf_t ROP, mpf_t OP) - Function: void mpf_set_ui (mpf_t ROP, unsigned long int OP) - Function: void mpf_set_si (mpf_t ROP, signed long int OP) - Function: void mpf_set_d (mpf_t ROP, double OP) - Function: void mpf_set_z (mpf_t ROP, mpz_t OP) - Function: void mpf_set_q (mpf_t ROP, mpq_t OP) Set the value of ROP from OP. - Function: int mpf_set_str (mpf_t ROP, char *STR, int BASE) Set the value of ROP from the string in STR. The string is of the form `M@N' or, if the base is 10 or less, alternatively `MeN'. `M' is the mantissa and `N' is the exponent. The mantissa is always in the specified base. The exponent is either in the specified base or, if BASE is negative, in decimal. The argument BASE may be in the ranges 2 to 36, or -36 to -2. Negative values are used to specify that the exponent is in decimal. Unlike the corresponding `mpz' function, the base will not be determined from the leading characters of the string if BASE is 0. This is so that numbers like `0.23' are not interpreted as octal. White space is allowed in the string, and is simply ignored. [This is not really true; white-space is ignored in the beginning of the string and within the mantissa, but not in other places, such as after a minus sign or in the exponent. We are considering changing the definition of this function, making it fail when there is any white-space in the input, since that makes a lot of sense. Please tell us your opinion about this change. Do you really want it to accept "3 14" as meaning 314 as it does now?] This function returns 0 if the entire string up to the '\0' is a valid number in base BASE. Otherwise it returns -1. - Function: void mpf_swap (mpf_t ROP1, mpf_t ROP2) Swap the values ROP1 and ROP2 efficiently.  File: gmp.info, Node: Simultaneous Float Init & Assign, Next: Converting Floats, Prev: Assigning Floats, Up: Floating-point Functions Combined Initialization and Assignment Functions ================================================ For convenience, GMP provides a parallel series of initialize-and-set functions which initialize the output and then store the value there. These functions' names have the form `mpf_init_set...' Once the float has been initialized by any of the `mpf_init_set...' functions, it can be used as the source or destination operand for the ordinary float functions. Don't use an initialize-and-set function on a variable already initialized! - Function: void mpf_init_set (mpf_t ROP, mpf_t OP) - Function: void mpf_init_set_ui (mpf_t ROP, unsigned long int OP) - Function: void mpf_init_set_si (mpf_t ROP, signed long int OP) - Function: void mpf_init_set_d (mpf_t ROP, double OP) Initialize ROP and set its value from OP. The precision of ROP will be taken from the active default precision, as set by `mpf_set_default_prec'. - Function: int mpf_init_set_str (mpf_t ROP, char *STR, int BASE) Initialize ROP and set its value from the string in STR. See `mpf_set_str' above for details on the assignment operation. Note that ROP is initialized even if an error occurs. (I.e., you have to call `mpf_clear' for it.) The precision of ROP will be taken from the active default precision, as set by `mpf_set_default_prec'.  File: gmp.info, Node: Converting Floats, Next: Float Arithmetic, Prev: Simultaneous Float Init & Assign, Up: Floating-point Functions Conversion Functions ==================== - Function: double mpf_get_d (mpf_t OP) Convert OP to a double. - Function: char * mpf_get_str (char *STR, mp_exp_t *EXPPTR, int BASE, size_t N_DIGITS, mpf_t OP) Convert OP to a string of digits in base BASE. The base may vary from 2 to 36. Generate at most N_DIGITS significant digits, or if N_DIGITS is 0, the maximum number of digits accurately representable by OP. If STR is `NULL', space for the mantissa is allocated using the default allocation function. If STR is not `NULL', it should point to a block of storage enough large for the mantissa, i.e., N_DIGITS + 2. The two extra bytes are for a possible minus sign, and for the terminating null character. The exponent is written through the pointer EXPPTR. If N_DIGITS is 0, the maximum number of digits meaningfully achievable from the precision of OP will be generated. Note that the space requirements for STR in this case will be impossible for the user to predetermine. Therefore, you need to pass `NULL' for the string argument whenever N_DIGITS is 0. The generated string is a fraction, with an implicit radix point immediately to the left of the first digit. For example, the number 3.1416 would be returned as "31416" in the string and 1 written at EXPPTR. A pointer to the result string is returned. This pointer will will either equal STR, or if that is `NULL', will point to the allocated storage.  File: gmp.info, Node: Float Arithmetic, Next: Float Comparison, Prev: Converting Floats, Up: Floating-point Functions Arithmetic Functions ==================== - Function: void mpf_add (mpf_t ROP, mpf_t OP1, mpf_t OP2) - Function: void mpf_add_ui (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1 + OP2. - Function: void mpf_sub (mpf_t ROP, mpf_t OP1, mpf_t OP2) - Function: void mpf_ui_sub (mpf_t ROP, unsigned long int OP1, mpf_t OP2) - Function: void mpf_sub_ui (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1 - OP2. - Function: void mpf_mul (mpf_t ROP, mpf_t OP1, mpf_t OP2) - Function: void mpf_mul_ui (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1 times OP2. Division is undefined if the divisor is zero, and passing a zero divisor to the divide functions will make these functions intentionally divide by zero. This lets the user handle arithmetic exceptions in these functions in the same manner as other arithmetic exceptions. - Function: void mpf_div (mpf_t ROP, mpf_t OP1, mpf_t OP2) - Function: void mpf_ui_div (mpf_t ROP, unsigned long int OP1, mpf_t OP2) - Function: void mpf_div_ui (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1/OP2. - Function: void mpf_sqrt (mpf_t ROP, mpf_t OP) - Function: void mpf_sqrt_ui (mpf_t ROP, unsigned long int OP) Set ROP to the square root of OP. - Function: void mpf_pow_ui (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1 raised to the power OP2. - Function: void mpf_neg (mpf_t ROP, mpf_t OP) Set ROP to -OP. - Function: void mpf_abs (mpf_t ROP, mpf_t OP) Set ROP to the absolute value of OP. - Function: void mpf_mul_2exp (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1 times 2 raised to OP2. - Function: void mpf_div_2exp (mpf_t ROP, mpf_t OP1, unsigned long int OP2) Set ROP to OP1 divided by 2 raised to OP2.  File: gmp.info, Node: Float Comparison, Next: I/O of Floats, Prev: Float Arithmetic, Up: Floating-point Functions Comparison Functions ==================== - Function: int mpf_cmp (mpf_t OP1, mpf_t OP2) - Function: int mpf_cmp_ui (mpf_t OP1, unsigned long int OP2) - Function: int mpf_cmp_si (mpf_t OP1, signed long int OP2) Compare OP1 and OP2. Return a positive value if OP1 > OP2, zero if OP1 = OP2, and a negative value if OP1 < OP2. - Function: int mpf_eq (mpf_t OP1, mpf_t OP2, unsigned long int op3) Return non-zero if the first OP3 bits of OP1 and OP2 are equal, zero otherwise. I.e., test of OP1 and OP2 are approximately equal. - Function: void mpf_reldiff (mpf_t ROP, mpf_t OP1, mpf_t OP2) Compute the relative difference between OP1 and OP2 and store the result in ROP. - Macro: int mpf_sgn (mpf_t OP) Return +1 if OP > 0, 0 if OP = 0, and -1 if OP < 0. This function is actually implemented as a macro. It evaluates its arguments multiple times.  File: gmp.info, Node: I/O of Floats, Next: Miscellaneous Float Functions, Prev: Float Comparison, Up: Floating-point Functions Input and Output Functions ========================== Functions that perform input from a stdio stream, and functions that output to a stdio stream. Passing a `NULL' pointer for a STREAM argument to any of these functions will make them read from `stdin' and write to `stdout', respectively. When using any of these functions, it is a good idea to include `stdio.h' before `gmp.h', since that will allow `gmp.h' to define prototypes for these functions. - Function: size_t mpf_out_str (FILE *STREAM, int BASE, size_t N_DIGITS, mpf_t OP) Output OP on stdio stream STREAM, as a string of digits in base BASE. The base may vary from 2 to 36. Print at most N_DIGITS significant digits, or if N_DIGITS is 0, the maximum number of digits accurately representable by OP. In addition to the significant digits, a leading `0.' and a trailing exponent, in the form `eNNN', are printed. If BASE is greater than 10, `@' will be used instead of `e' as exponent delimiter. Return the number of bytes written, or if an error occurred, return 0. - Function: size_t mpf_inp_str (mpf_t ROP, FILE *STREAM, int BASE) Input a string in base BASE from stdio stream STREAM, and put the read float in ROP. The string is of the form `M@N' or, if the base is 10 or less, alternatively `MeN'. `M' is the mantissa and `N' is the exponent. The mantissa is always in the specified base. The exponent is either in the specified base or, if BASE is negative, in decimal. The argument BASE may be in the ranges 2 to 36, or -36 to -2. Negative values are used to specify that the exponent is in decimal. Unlike the corresponding `mpz' function, the base will not be determined from the leading characters of the string if BASE is 0. This is so that numbers like `0.23' are not interpreted as octal. Return the number of bytes read, or if an error occurred, return 0.  File: gmp.info, Node: Miscellaneous Float Functions, Prev: I/O of Floats, Up: Floating-point Functions Miscellaneous Functions ======================= - Function: void mpf_ceil (mpf_t ROP, mpf_t OP) - Function: void mpf_floor (mpf_t ROP, mpf_t OP) - Function: void mpf_trunc (mpf_t ROP, mpf_t OP) Set ROP to OP rounded to an integer. `mpf_ceil' rounds to the next higher integer, `mpf_floor' to the next lower, and `mpf_trunc' to the integer towards zero. - Function: void mpf_urandomb (mpf_t ROP, gmp_randstate_t STATE, unsigned long int NBITS) Generate a uniformly distributed random float in ROP, such that 0 <= ROP < 1, with NBITS significant bits in the mantissa. The variable STATE must be initialized by calling one of the `gmp_randinit' functions (*Note Random State Initialization::) before invoking this function. - Function: void mpf_random2 (mpf_t ROP, mp_size_t MAX_SIZE, mp_exp_t MAX_EXP) Generate a random float of at most MAX_SIZE limbs, with long strings of zeros and ones in the binary representation. The exponent of the number is in the interval -EXP to EXP. This function is useful for testing functions and algorithms, since this kind of random numbers have proven to be more likely to trigger corner-case bugs. Negative random numbers are generated when MAX_SIZE is negative.