| This is mpfr.info, produced by makeinfo version 5.2 from mpfr.texi. |
| |
| This manual documents how to install and use the Multiple Precision |
| Floating-Point Reliable Library, version 3.1.3. |
| |
| Copyright 1991, 1993-2015 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.2 or |
| any later version published by the Free Software Foundation; with no |
| Invariant Sections, with no Front-Cover Texts, and with no Back-Cover |
| Texts. A copy of the license is included in *note GNU Free |
| Documentation License::. |
| INFO-DIR-SECTION Software libraries |
| START-INFO-DIR-ENTRY |
| * mpfr: (mpfr). Multiple Precision Floating-Point Reliable Library. |
| END-INFO-DIR-ENTRY |
| |
| |
| File: mpfr.info, Node: Top, Next: Copying, Prev: (dir), Up: (dir) |
| |
| GNU MPFR |
| ******** |
| |
| This manual documents how to install and use the Multiple Precision |
| Floating-Point Reliable Library, version 3.1.3. |
| |
| Copyright 1991, 1993-2015 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.2 or |
| any later version published by the Free Software Foundation; with no |
| Invariant Sections, with no Front-Cover Texts, and with no Back-Cover |
| Texts. A copy of the license is included in *note GNU Free |
| Documentation License::. |
| |
| * Menu: |
| |
| * Copying:: MPFR Copying Conditions (LGPL). |
| * Introduction to MPFR:: Brief introduction to GNU MPFR. |
| * Installing MPFR:: How to configure and compile the MPFR library. |
| * Reporting Bugs:: How to usefully report bugs. |
| * MPFR Basics:: What every MPFR user should now. |
| * MPFR Interface:: MPFR functions and macros. |
| * API Compatibility:: API compatibility with previous MPFR versions. |
| * Contributors:: |
| * References:: |
| * GNU Free Documentation License:: |
| * Concept Index:: |
| * Function and Type Index:: |
| |
| |
| File: mpfr.info, Node: Copying, Next: Introduction to MPFR, Prev: Top, Up: Top |
| |
| MPFR Copying Conditions |
| *********************** |
| |
| The GNU MPFR library (or MPFR for short) is "free"; this means that |
| everyone is free to use it and free to redistribute it on a free basis. |
| The library is not in the public domain; it is copyrighted and there are |
| restrictions on its distribution, but these restrictions are designed to |
| permit everything that a good cooperating citizen would want to do. |
| What is not allowed is to try to prevent others from further sharing any |
| version of this library that they might get from you. |
| |
| Specifically, we want to make sure that you have the right to give |
| away copies of the library, that you receive source code or else can get |
| it if you want it, that you can change this library or use pieces of it |
| in new free programs, and that you know you can do these things. |
| |
| To make sure that everyone has such rights, we have to forbid you to |
| deprive anyone else of these rights. For example, if you distribute |
| copies of the GNU MPFR library, you must give the recipients all the |
| rights that you have. You must make sure that they, too, receive or can |
| get the source code. And you must tell them their rights. |
| |
| Also, for our own protection, we must make certain that everyone |
| finds out that there is no warranty for the GNU MPFR library. If it is |
| modified by someone else and passed on, we want their recipients to know |
| that what they have is not what we distributed, so that any problems |
| introduced by others will not reflect on our reputation. |
| |
| The precise conditions of the license for the GNU MPFR library are |
| found in the Lesser General Public License that accompanies the source |
| code. See the file COPYING.LESSER. |
| |
| |
| File: mpfr.info, Node: Introduction to MPFR, Next: Installing MPFR, Prev: Copying, Up: Top |
| |
| 1 Introduction to MPFR |
| ********************** |
| |
| MPFR is a portable library written in C for arbitrary precision |
| arithmetic on floating-point numbers. It is based on the GNU MP |
| library. It aims to provide a class of floating-point numbers with |
| precise semantics. The main characteristics of MPFR, which make it |
| differ from most arbitrary precision floating-point software tools, are: |
| |
| • the MPFR code is portable, i.e., the result of any operation does |
| not depend on the machine word size ‘mp_bits_per_limb’ (64 on most |
| current processors); |
| |
| • the precision in bits can be set _exactly_ to any valid value for |
| each variable (including very small precision); |
| |
| • MPFR provides the four rounding modes from the IEEE 754-1985 |
| standard, plus away-from-zero, as well as for basic operations as |
| for other mathematical functions. |
| |
| In particular, with a precision of 53 bits, MPFR is able to exactly |
| reproduce all computations with double-precision machine floating-point |
| numbers (e.g., ‘double’ type in C, with a C implementation that |
| rigorously follows Annex F of the ISO C99 standard and ‘FP_CONTRACT’ |
| pragma set to ‘OFF’) on the four arithmetic operations and the square |
| root, except the default exponent range is much wider and subnormal |
| numbers are not implemented (but can be emulated). |
| |
| This version of MPFR is released under the GNU Lesser General Public |
| License, version 3 or any later version. It is permitted to link MPFR |
| to most non-free programs, as long as when distributing them the MPFR |
| source code and a means to re-link with a modified MPFR library is |
| provided. |
| |
| 1.1 How to Use This Manual |
| ========================== |
| |
| Everyone should read *note MPFR Basics::. If you need to install the |
| library yourself, you need to read *note Installing MPFR::, too. To use |
| the library you will need to refer to *note MPFR Interface::. |
| |
| The rest of the manual can be used for later reference, although it |
| is probably a good idea to glance through it. |
| |
| |
| File: mpfr.info, Node: Installing MPFR, Next: Reporting Bugs, Prev: Introduction to MPFR, Up: Top |
| |
| 2 Installing MPFR |
| ***************** |
| |
| The MPFR library is already installed on some GNU/Linux distributions, |
| but the development files necessary to the compilation such as ‘mpfr.h’ |
| are not always present. To check that MPFR is fully installed on your |
| computer, you can check the presence of the file ‘mpfr.h’ in |
| ‘/usr/include’, or try to compile a small program having ‘#include |
| <mpfr.h>’ (since ‘mpfr.h’ may be installed somewhere else). For |
| instance, you can try to compile: |
| |
| #include <stdio.h> |
| #include <mpfr.h> |
| int main (void) |
| { |
| printf ("MPFR library: %-12s\nMPFR header: %s (based on %d.%d.%d)\n", |
| mpfr_get_version (), MPFR_VERSION_STRING, MPFR_VERSION_MAJOR, |
| MPFR_VERSION_MINOR, MPFR_VERSION_PATCHLEVEL); |
| return 0; |
| } |
| |
| with |
| |
| cc -o version version.c -lmpfr -lgmp |
| |
| and if you get errors whose first line looks like |
| |
| version.c:2:19: error: mpfr.h: No such file or directory |
| |
| then MPFR is probably not installed. Running this program will give you |
| the MPFR version. |
| |
| If MPFR is not installed on your computer, or if you want to install |
| a different version, please follow the steps below. |
| |
| 2.1 How to Install |
| ================== |
| |
| Here are the steps needed to install the library on Unix systems (more |
| details are provided in the ‘INSTALL’ file): |
| |
| 1. To build MPFR, you first have to install GNU MP (version 4.1 or |
| higher) on your computer. You need a C compiler, preferably GCC, |
| but any reasonable compiler should work. And you need the standard |
| Unix ‘make’ command, plus some other standard Unix utility |
| commands. |
| |
| Then, in the MPFR build directory, type the following commands. |
| |
| 2. ‘./configure’ |
| |
| This will prepare the build and setup the options according to your |
| system. You can give options to specify the install directories |
| (instead of the default ‘/usr/local’), threading support, and so |
| on. See the ‘INSTALL’ file and/or the output of ‘./configure |
| --help’ for more information, in particular if you get error |
| messages. |
| |
| 3. ‘make’ |
| |
| This will compile MPFR, and create a library archive file |
| ‘libmpfr.a’. On most platforms, a dynamic library will be produced |
| too. |
| |
| 4. ‘make check’ |
| |
| This will make sure that MPFR was built correctly. If any test |
| fails, information about this failure can be found in the |
| ‘tests/test-suite.log’ file. If you want the contents of this file |
| to be automatically output in case of failure, you can set the |
| ‘VERBOSE’ environment variable to 1 before running ‘make check’, |
| for instance by typing: |
| |
| ‘VERBOSE=1 make check’ |
| |
| In case of failure, you may want to check whether the problem is |
| already known. If not, please report this failure to the MPFR |
| mailing-list ‘mpfr@inria.fr’. For details, *Note Reporting Bugs::. |
| |
| 5. ‘make install’ |
| |
| This will copy the files ‘mpfr.h’ and ‘mpf2mpfr.h’ to the directory |
| ‘/usr/local/include’, the library files (‘libmpfr.a’ and possibly |
| others) to the directory ‘/usr/local/lib’, the file ‘mpfr.info’ to |
| the directory ‘/usr/local/share/info’, and some other documentation |
| files to the directory ‘/usr/local/share/doc/mpfr’ (or if you |
| passed the ‘--prefix’ option to ‘configure’, using the prefix |
| directory given as argument to ‘--prefix’ instead of ‘/usr/local’). |
| |
| 2.2 Other ‘make’ Targets |
| ======================== |
| |
| There are some other useful make targets: |
| |
| • ‘mpfr.info’ or ‘info’ |
| |
| Create or update an info version of the manual, in ‘mpfr.info’. |
| |
| This file is already provided in the MPFR archives. |
| |
| • ‘mpfr.pdf’ or ‘pdf’ |
| |
| Create a PDF version of the manual, in ‘mpfr.pdf’. |
| |
| • ‘mpfr.dvi’ or ‘dvi’ |
| |
| Create a DVI version of the manual, in ‘mpfr.dvi’. |
| |
| • ‘mpfr.ps’ or ‘ps’ |
| |
| Create a Postscript version of the manual, in ‘mpfr.ps’. |
| |
| • ‘mpfr.html’ or ‘html’ |
| |
| Create a HTML version of the manual, in several pages in the |
| directory ‘doc/mpfr.html’; if you want only one output HTML file, |
| then type ‘makeinfo --html --no-split mpfr.texi’ from the ‘doc’ |
| directory instead. |
| |
| • ‘clean’ |
| |
| Delete all object files and archive files, but not the |
| configuration files. |
| |
| • ‘distclean’ |
| |
| Delete all generated files not included in the distribution. |
| |
| • ‘uninstall’ |
| |
| Delete all files copied by ‘make install’. |
| |
| 2.3 Build Problems |
| ================== |
| |
| In case of problem, please read the ‘INSTALL’ file carefully before |
| reporting a bug, in particular section “In case of problem”. Some |
| problems are due to bad configuration on the user side (not specific to |
| MPFR). Problems are also mentioned in the FAQ |
| <http://www.mpfr.org/faq.html>. |
| |
| Please report problems to the MPFR mailing-list ‘mpfr@inria.fr’. |
| *Note Reporting Bugs::. Some bug fixes are available on the MPFR 3.1.3 |
| web page <http://www.mpfr.org/mpfr-3.1.3/>. |
| |
| 2.4 Getting the Latest Version of MPFR |
| ====================================== |
| |
| The latest version of MPFR is available from |
| <ftp://ftp.gnu.org/gnu/mpfr/> or <http://www.mpfr.org/>. |
| |
| |
| File: mpfr.info, Node: Reporting Bugs, Next: MPFR Basics, Prev: Installing MPFR, Up: Top |
| |
| 3 Reporting Bugs |
| **************** |
| |
| If you think you have found a bug in the MPFR library, first have a look |
| on the MPFR 3.1.3 web page <http://www.mpfr.org/mpfr-3.1.3/> and the FAQ |
| <http://www.mpfr.org/faq.html>: perhaps this bug is already known, in |
| which case you may find there a workaround for it. You might also look |
| in the archives of the MPFR mailing-list: |
| <https://sympa.inria.fr/sympa/arc/mpfr>. Otherwise, please investigate |
| and report it. We have made this library available to you, and it is |
| not to ask too much from you, to ask you to report the bugs that you |
| find. |
| |
| There are a few things you should think about when you put your bug |
| report together. |
| |
| You have to send us a test case that makes it possible for us to |
| reproduce the bug, i.e., a small self-content program, using no other |
| library than MPFR. Include instructions on how to run the test case. |
| |
| You also have to explain what is wrong; if you get a crash, or if the |
| results you get are incorrect and in that case, in what way. |
| |
| Please include compiler version information in your bug report. This |
| can be extracted using ‘cc -V’ on some machines, or, if you’re using |
| GCC, ‘gcc -v’. Also, include the output from ‘uname -a’ and the MPFR |
| version (the GMP version may be useful too). If you get a failure while |
| running ‘make’ or ‘make check’, please include the ‘config.log’ file in |
| your bug report, and in case of test failure, the ‘tests/test-suite.log’ |
| file too. |
| |
| If your bug report is good, we will do our best to help you to get a |
| corrected version of the library; if the bug report is poor, we will not |
| do anything about it (aside of chiding you to send better bug reports). |
| |
| Send your bug report to the MPFR mailing-list ‘mpfr@inria.fr’. |
| |
| If you think something in this manual is unclear, or downright |
| incorrect, or if the language needs to be improved, please send a note |
| to the same address. |
| |
| |
| File: mpfr.info, Node: MPFR Basics, Next: MPFR Interface, Prev: Reporting Bugs, Up: Top |
| |
| 4 MPFR Basics |
| ************* |
| |
| * Menu: |
| |
| * Headers and Libraries:: |
| * Nomenclature and Types:: |
| * MPFR Variable Conventions:: |
| * Rounding Modes:: |
| * Floating-Point Values on Special Numbers:: |
| * Exceptions:: |
| * Memory Handling:: |
| |
| |
| File: mpfr.info, Node: Headers and Libraries, Next: Nomenclature and Types, Prev: MPFR Basics, Up: MPFR Basics |
| |
| 4.1 Headers and Libraries |
| ========================= |
| |
| All declarations needed to use MPFR are collected in the include file |
| ‘mpfr.h’. It is designed to work with both C and C++ compilers. You |
| should include that file in any program using the MPFR library: |
| |
| #include <mpfr.h> |
| |
| Note however that prototypes for MPFR functions with ‘FILE *’ |
| parameters are provided only if ‘<stdio.h>’ is included too (before |
| ‘mpfr.h’): |
| |
| #include <stdio.h> |
| #include <mpfr.h> |
| |
| Likewise ‘<stdarg.h>’ (or ‘<varargs.h>’) is required for prototypes |
| with ‘va_list’ parameters, such as ‘mpfr_vprintf’. |
| |
| And for any functions using ‘intmax_t’, you must include ‘<stdint.h>’ |
| or ‘<inttypes.h>’ before ‘mpfr.h’, to allow ‘mpfr.h’ to define |
| prototypes for these functions. Moreover, users of C++ compilers under |
| some platforms may need to define ‘MPFR_USE_INTMAX_T’ (and should do it |
| for portability) before ‘mpfr.h’ has been included; of course, it is |
| possible to do that on the command line, e.g., with |
| ‘-DMPFR_USE_INTMAX_T’. |
| |
| Note: If ‘mpfr.h’ and/or ‘gmp.h’ (used by ‘mpfr.h’) are included |
| several times (possibly from another header file), ‘<stdio.h>’ and/or |
| ‘<stdarg.h>’ (or ‘<varargs.h>’) should be included *before the first |
| inclusion* of ‘mpfr.h’ or ‘gmp.h’. Alternatively, you can define |
| ‘MPFR_USE_FILE’ (for MPFR I/O functions) and/or ‘MPFR_USE_VA_LIST’ (for |
| MPFR functions with ‘va_list’ parameters) anywhere before the last |
| inclusion of ‘mpfr.h’. As a consequence, if your file is a public |
| header that includes ‘mpfr.h’, you need to use the latter method. |
| |
| When calling a MPFR macro, it is not allowed to have previously |
| defined a macro with the same name as some keywords (currently ‘do’, |
| ‘while’ and ‘sizeof’). |
| |
| You can avoid the use of MPFR macros encapsulating functions by |
| defining the ‘MPFR_USE_NO_MACRO’ macro before ‘mpfr.h’ is included. In |
| general this should not be necessary, but this can be useful when |
| debugging user code: with some macros, the compiler may emit spurious |
| warnings with some warning options, and macros can prevent some |
| prototype checking. |
| |
| All programs using MPFR must link against both ‘libmpfr’ and ‘libgmp’ |
| libraries. On a typical Unix-like system this can be done with ‘-lmpfr |
| -lgmp’ (in that order), for example: |
| |
| gcc myprogram.c -lmpfr -lgmp |
| |
| MPFR is built using Libtool and an application can use that to link |
| if desired, *note GNU Libtool: (libtool.info)Top. |
| |
| If MPFR has been installed to a non-standard location, then it may be |
| necessary to set up environment variables such as ‘C_INCLUDE_PATH’ and |
| ‘LIBRARY_PATH’, or use ‘-I’ and ‘-L’ compiler options, in order to point |
| to the right directories. For a shared library, it may also be |
| necessary to set up some sort of run-time library path (e.g., |
| ‘LD_LIBRARY_PATH’) on some systems. Please read the ‘INSTALL’ file for |
| additional information. |
| |
| |
| File: mpfr.info, Node: Nomenclature and Types, Next: MPFR Variable Conventions, Prev: Headers and Libraries, Up: MPFR Basics |
| |
| 4.2 Nomenclature and Types |
| ========================== |
| |
| A "floating-point number", or "float" for short, is an arbitrary |
| precision significand (also called mantissa) with a limited precision |
| exponent. The C data type for such objects is ‘mpfr_t’ (internally |
| defined as a one-element array of a structure, and ‘mpfr_ptr’ is the C |
| data type representing a pointer to this structure). A floating-point |
| number can have three special values: Not-a-Number (NaN) or plus or |
| minus Infinity. NaN represents an uninitialized object, the result of |
| an invalid operation (like 0 divided by 0), or a value that cannot be |
| determined (like +Infinity minus +Infinity). Moreover, like in the IEEE |
| 754 standard, zero is signed, i.e., there are both +0 and −0; the |
| behavior is the same as in the IEEE 754 standard and it is generalized |
| to the other functions supported by MPFR. Unless documented otherwise, |
| the sign bit of a NaN is unspecified. |
| |
| The "precision" is the number of bits used to represent the significand |
| of a floating-point number; the corresponding C data type is |
| ‘mpfr_prec_t’. The precision can be any integer between ‘MPFR_PREC_MIN’ |
| and ‘MPFR_PREC_MAX’. In the current implementation, ‘MPFR_PREC_MIN’ is |
| equal to 2. |
| |
| Warning! MPFR needs to increase the precision internally, in order |
| to provide accurate results (and in particular, correct rounding). Do |
| not attempt to set the precision to any value near ‘MPFR_PREC_MAX’, |
| otherwise MPFR will abort due to an assertion failure. Moreover, you |
| may reach some memory limit on your platform, in which case the program |
| may abort, crash or have undefined behavior (depending on your C |
| implementation). |
| |
| The "rounding mode" specifies the way to round the result of a |
| floating-point operation, in case the exact result can not be |
| represented exactly in the destination significand; the corresponding C |
| data type is ‘mpfr_rnd_t’. |
| |
| |
| File: mpfr.info, Node: MPFR Variable Conventions, Next: Rounding Modes, Prev: Nomenclature and Types, Up: MPFR Basics |
| |
| 4.3 MPFR Variable Conventions |
| ============================= |
| |
| Before you can assign to an MPFR variable, you need to initialize it by |
| calling one of the special initialization functions. When you’re done |
| with a variable, you need to clear it out, using one of the functions |
| for that purpose. A variable should only be initialized once, or at |
| least cleared out between each initialization. After a variable has |
| been initialized, it may be assigned to any number of times. For |
| efficiency reasons, avoid to initialize and clear out a variable in |
| loops. Instead, initialize it before entering the loop, and clear it |
| out after the loop has exited. You do not need to be concerned about |
| allocating additional space for MPFR variables, since any variable has a |
| significand of fixed size. Hence unless you change its precision, or |
| clear and reinitialize it, a floating-point variable will have the same |
| allocated space during all its life. |
| |
| As a general rule, all MPFR functions expect output arguments before |
| input arguments. This notation is based on an analogy with the |
| assignment operator. MPFR allows you to use the same variable for both |
| input and output in the same expression. For example, the main function |
| for floating-point multiplication, ‘mpfr_mul’, can be used like this: |
| ‘mpfr_mul (x, x, x, rnd)’. This computes the square of X with rounding |
| mode ‘rnd’ and puts the result back in X. |
| |
| |
| File: mpfr.info, Node: Rounding Modes, Next: Floating-Point Values on Special Numbers, Prev: MPFR Variable Conventions, Up: MPFR Basics |
| |
| 4.4 Rounding Modes |
| ================== |
| |
| The following five rounding modes are supported: |
| • ‘MPFR_RNDN’: round to nearest (roundTiesToEven in IEEE 754-2008), |
| • ‘MPFR_RNDZ’: round toward zero (roundTowardZero in IEEE 754-2008), |
| • ‘MPFR_RNDU’: round toward plus infinity (roundTowardPositive in |
| IEEE 754-2008), |
| • ‘MPFR_RNDD’: round toward minus infinity (roundTowardNegative in |
| IEEE 754-2008), |
| • ‘MPFR_RNDA’: round away from zero. |
| |
| The ‘round to nearest’ mode works as in the IEEE 754 standard: in |
| case the number to be rounded lies exactly in the middle of two |
| representable numbers, it is rounded to the one with the least |
| significant bit set to zero. For example, the number 2.5, which is |
| represented by (10.1) in binary, is rounded to (10.0)=2 with a precision |
| of two bits, and not to (11.0)=3. This rule avoids the "drift" |
| phenomenon mentioned by Knuth in volume 2 of The Art of Computer |
| Programming (Section 4.2.2). |
| |
| Most MPFR functions take as first argument the destination variable, |
| as second and following arguments the input variables, as last argument |
| a rounding mode, and have a return value of type ‘int’, called the |
| "ternary value". The value stored in the destination variable is |
| correctly rounded, i.e., MPFR behaves as if it computed the result with |
| an infinite precision, then rounded it to the precision of this |
| variable. The input variables are regarded as exact (in particular, |
| their precision does not affect the result). |
| |
| As a consequence, in case of a non-zero real rounded result, the |
| error on the result is less or equal to 1/2 ulp (unit in the last place) |
| of that result in the rounding to nearest mode, and less than 1 ulp of |
| that result in the directed rounding modes (a ulp is the weight of the |
| least significant represented bit of the result after rounding). |
| |
| Unless documented otherwise, functions returning an ‘int’ return a |
| ternary value. If the ternary value is zero, it means that the value |
| stored in the destination variable is the exact result of the |
| corresponding mathematical function. If the ternary value is positive |
| (resp. negative), it means the value stored in the destination variable |
| is greater (resp. lower) than the exact result. For example with the |
| ‘MPFR_RNDU’ rounding mode, the ternary value is usually positive, except |
| when the result is exact, in which case it is zero. In the case of an |
| infinite result, it is considered as inexact when it was obtained by |
| overflow, and exact otherwise. A NaN result (Not-a-Number) always |
| corresponds to an exact return value. The opposite of a returned |
| ternary value is guaranteed to be representable in an ‘int’. |
| |
| Unless documented otherwise, functions returning as result the value |
| ‘1’ (or any other value specified in this manual) for special cases |
| (like ‘acos(0)’) yield an overflow or an underflow if that value is not |
| representable in the current exponent range. |
| |
| |
| File: mpfr.info, Node: Floating-Point Values on Special Numbers, Next: Exceptions, Prev: Rounding Modes, Up: MPFR Basics |
| |
| 4.5 Floating-Point Values on Special Numbers |
| ============================================ |
| |
| This section specifies the floating-point values (of type ‘mpfr_t’) |
| returned by MPFR functions (where by “returned” we mean here the |
| modified value of the destination object, which should not be mixed with |
| the ternary return value of type ‘int’ of those functions). For |
| functions returning several values (like ‘mpfr_sin_cos’), the rules |
| apply to each result separately. |
| |
| Functions can have one or several input arguments. An input point is |
| a mapping from these input arguments to the set of the MPFR numbers. |
| When none of its components are NaN, an input point can also be seen as |
| a tuple in the extended real numbers (the set of the real numbers with |
| both infinities). |
| |
| When the input point is in the domain of the mathematical function, |
| the result is rounded as described in Section “Rounding Modes” (but see |
| below for the specification of the sign of an exact zero). Otherwise |
| the general rules from this section apply unless stated otherwise in the |
| description of the MPFR function (*note MPFR Interface::). |
| |
| When the input point is not in the domain of the mathematical |
| function but is in its closure in the extended real numbers and the |
| function can be extended by continuity, the result is the obtained |
| limit. Examples: ‘mpfr_hypot’ on (+Inf,0) gives +Inf. But ‘mpfr_pow’ |
| cannot be defined on (1,+Inf) using this rule, as one can find sequences |
| (X_N,Y_N) such that X_N goes to 1, Y_N goes to +Inf and X_N to the Y_N |
| goes to any positive value when N goes to the infinity. |
| |
| When the input point is in the closure of the domain of the |
| mathematical function and an input argument is +0 (resp. −0), one |
| considers the limit when the corresponding argument approaches 0 from |
| above (resp. below). If the limit is not defined (e.g., ‘mpfr_log’ on |
| −0), the behavior is specified in the description of the MPFR function. |
| |
| When the result is equal to 0, its sign is determined by considering |
| the limit as if the input point were not in the domain: If one |
| approaches 0 from above (resp. below), the result is +0 (resp. −0); for |
| example, ‘mpfr_sin’ on +0 gives +0. In the other cases, the sign is |
| specified in the description of the MPFR function; for example |
| ‘mpfr_max’ on −0 and +0 gives +0. |
| |
| When the input point is not in the closure of the domain of the |
| function, the result is NaN. Example: ‘mpfr_sqrt’ on −17 gives NaN. |
| |
| When an input argument is NaN, the result is NaN, possibly except |
| when a partial function is constant on the finite floating-point |
| numbers; such a case is always explicitly specified in *note MPFR |
| Interface::. Example: ‘mpfr_hypot’ on (NaN,0) gives NaN, but |
| ‘mpfr_hypot’ on (NaN,+Inf) gives +Inf (as specified in *note Special |
| Functions::), since for any finite input X, ‘mpfr_hypot’ on (X,+Inf) |
| gives +Inf. |
| |
| |
| File: mpfr.info, Node: Exceptions, Next: Memory Handling, Prev: Floating-Point Values on Special Numbers, Up: MPFR Basics |
| |
| 4.6 Exceptions |
| ============== |
| |
| MPFR supports 6 exception types: |
| |
| • Underflow: An underflow occurs when the exact result of a function |
| is a non-zero real number and the result obtained after the |
| rounding, assuming an unbounded exponent range (for the rounding), |
| has an exponent smaller than the minimum value of the current |
| exponent range. (In the round-to-nearest mode, the halfway case is |
| rounded toward zero.) |
| |
| Note: This is not the single possible definition of the underflow. |
| MPFR chooses to consider the underflow _after_ rounding. The |
| underflow before rounding can also be defined. For instance, |
| consider a function that has the exact result 7 multiplied by two |
| to the power E−4, where E is the smallest exponent (for a |
| significand between 1/2 and 1), with a 2-bit target precision and |
| rounding toward plus infinity. The exact result has the exponent |
| E−1. With the underflow before rounding, such a function call |
| would yield an underflow, as E−1 is outside the current exponent |
| range. However, MPFR first considers the rounded result assuming |
| an unbounded exponent range. The exact result cannot be |
| represented exactly in precision 2, and here, it is rounded to 0.5 |
| times 2 to E, which is representable in the current exponent range. |
| As a consequence, this will not yield an underflow in MPFR. |
| |
| • Overflow: An overflow occurs when the exact result of a function is |
| a non-zero real number and the result obtained after the rounding, |
| assuming an unbounded exponent range (for the rounding), has an |
| exponent larger than the maximum value of the current exponent |
| range. In the round-to-nearest mode, the result is infinite. |
| Note: unlike the underflow case, there is only one possible |
| definition of overflow here. |
| |
| • Divide-by-zero: An exact infinite result is obtained from finite |
| inputs. |
| |
| • NaN: A NaN exception occurs when the result of a function is NaN. |
| |
| • Inexact: An inexact exception occurs when the result of a function |
| cannot be represented exactly and must be rounded. |
| |
| • Range error: A range exception occurs when a function that does not |
| return a MPFR number (such as comparisons and conversions to an |
| integer) has an invalid result (e.g., an argument is NaN in |
| ‘mpfr_cmp’, or a conversion to an integer cannot be represented in |
| the target type). |
| |
| MPFR has a global flag for each exception, which can be cleared, set |
| or tested by functions described in *note Exception Related Functions::. |
| |
| Differences with the ISO C99 standard: |
| |
| • In C, only quiet NaNs are specified, and a NaN propagation does not |
| raise an invalid exception. Unless explicitly stated otherwise, |
| MPFR sets the NaN flag whenever a NaN is generated, even when a NaN |
| is propagated (e.g., in NaN + NaN), as if all NaNs were signaling. |
| |
| • An invalid exception in C corresponds to either a NaN exception or |
| a range error in MPFR. |
| |
| |
| File: mpfr.info, Node: Memory Handling, Prev: Exceptions, Up: MPFR Basics |
| |
| 4.7 Memory Handling |
| =================== |
| |
| MPFR functions may create caches, e.g., when computing constants such as |
| Pi, either because the user has called a function like ‘mpfr_const_pi’ |
| directly or because such a function was called internally by the MPFR |
| library itself to compute some other function. |
| |
| At any time, the user can free the various caches with |
| ‘mpfr_free_cache’. It is strongly advised to do that before terminating |
| a thread, or before exiting when using tools like ‘valgrind’ (to avoid |
| memory leaks being reported). |
| |
| MPFR internal data such as flags, the exponent range, the default |
| precision and rounding mode, and caches (i.e., data that are not |
| accessed via parameters) are either global (if MPFR has not been |
| compiled as thread safe) or per-thread (thread local storage, TLS). The |
| initial values of TLS data after a thread is created entirely depend on |
| the compiler and thread implementation (MPFR simply does a conventional |
| variable initialization, the variables being declared with an |
| implementation-defined TLS specifier). |
| |
| |
| File: mpfr.info, Node: MPFR Interface, Next: API Compatibility, Prev: MPFR Basics, Up: Top |
| |
| 5 MPFR Interface |
| **************** |
| |
| The floating-point functions expect arguments of type ‘mpfr_t’. |
| |
| The MPFR floating-point functions have an interface that is similar |
| to the GNU MP functions. The function prefix for floating-point |
| operations is ‘mpfr_’. |
| |
| 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 cost of that computation should not depend |
| on the precision of variables used as input (on average). |
| |
| The semantics of a calculation in MPFR is specified as follows: |
| Compute the requested operation exactly (with “infinite accuracy”), and |
| round the result to the precision of the destination variable, with the |
| given rounding mode. The MPFR floating-point functions are intended to |
| be a smooth extension of the IEEE 754 arithmetic. The results obtained |
| on a given computer are identical to those obtained on a computer with a |
| different word size, or with a different compiler or operating system. |
| |
| MPFR _does not keep track_ of the accuracy of a computation. This is |
| left to the user or to a higher layer (for example the MPFI library for |
| interval arithmetic). As a consequence, if two variables are used to |
| store only a few significant bits, and their product is stored in a |
| variable with large precision, then MPFR will still compute the result |
| with full precision. |
| |
| The value of the standard C macro ‘errno’ may be set to non-zero by |
| any MPFR function or macro, whether or not there is an error. |
| |
| * Menu: |
| |
| * Initialization Functions:: |
| * Assignment Functions:: |
| * Combined Initialization and Assignment Functions:: |
| * Conversion Functions:: |
| * Basic Arithmetic Functions:: |
| * Comparison Functions:: |
| * Special Functions:: |
| * Input and Output Functions:: |
| * Formatted Output Functions:: |
| * Integer Related Functions:: |
| * Rounding Related Functions:: |
| * Miscellaneous Functions:: |
| * Exception Related Functions:: |
| * Compatibility with MPF:: |
| * Custom Interface:: |
| * Internals:: |
| |
| |
| File: mpfr.info, Node: Initialization Functions, Next: Assignment Functions, Prev: MPFR Interface, Up: MPFR Interface |
| |
| 5.1 Initialization Functions |
| ============================ |
| |
| An ‘mpfr_t’ object must be initialized before storing the first value in |
| it. The functions ‘mpfr_init’ and ‘mpfr_init2’ are used for that |
| purpose. |
| |
| -- Function: void mpfr_init2 (mpfr_t X, mpfr_prec_t PREC) |
| Initialize X, set its precision to be *exactly* PREC bits and its |
| value to NaN. (Warning: the corresponding MPF function initializes |
| to zero instead.) |
| |
| Normally, a variable should be initialized once only or at least be |
| cleared, using ‘mpfr_clear’, between initializations. To change |
| the precision of a variable which has already been initialized, use |
| ‘mpfr_set_prec’. The precision PREC must be an integer between |
| ‘MPFR_PREC_MIN’ and ‘MPFR_PREC_MAX’ (otherwise the behavior is |
| undefined). |
| |
| -- Function: void mpfr_inits2 (mpfr_prec_t PREC, mpfr_t X, ...) |
| Initialize all the ‘mpfr_t’ variables of the given variable |
| argument ‘va_list’, set their precision to be *exactly* PREC bits |
| and their value to NaN. See ‘mpfr_init2’ for more details. The |
| ‘va_list’ is assumed to be composed only of type ‘mpfr_t’ (or |
| equivalently ‘mpfr_ptr’). It begins from X, and ends when it |
| encounters a null pointer (whose type must also be ‘mpfr_ptr’). |
| |
| -- Function: void mpfr_clear (mpfr_t X) |
| Free the space occupied by the significand of X. Make sure to call |
| this function for all ‘mpfr_t’ variables when you are done with |
| them. |
| |
| -- Function: void mpfr_clears (mpfr_t X, ...) |
| Free the space occupied by all the ‘mpfr_t’ variables of the given |
| ‘va_list’. See ‘mpfr_clear’ for more details. The ‘va_list’ is |
| assumed to be composed only of type ‘mpfr_t’ (or equivalently |
| ‘mpfr_ptr’). It begins from X, and ends when it encounters a null |
| pointer (whose type must also be ‘mpfr_ptr’). |
| |
| Here is an example of how to use multiple initialization functions |
| (since ‘NULL’ is not necessarily defined in this context, we use |
| ‘(mpfr_ptr) 0’ instead, but ‘(mpfr_ptr) NULL’ is also correct). |
| |
| { |
| mpfr_t x, y, z, t; |
| mpfr_inits2 (256, x, y, z, t, (mpfr_ptr) 0); |
| … |
| mpfr_clears (x, y, z, t, (mpfr_ptr) 0); |
| } |
| |
| -- Function: void mpfr_init (mpfr_t X) |
| Initialize X, set its precision to the default precision, and set |
| its value to NaN. The default precision can be changed by a call to |
| ‘mpfr_set_default_prec’. |
| |
| Warning! In a given program, some other libraries might change the |
| default precision and not restore it. Thus it is safer to use |
| ‘mpfr_init2’. |
| |
| -- Function: void mpfr_inits (mpfr_t X, ...) |
| Initialize all the ‘mpfr_t’ variables of the given ‘va_list’, set |
| their precision to the default precision and their value to NaN. |
| See ‘mpfr_init’ for more details. The ‘va_list’ is assumed to be |
| composed only of type ‘mpfr_t’ (or equivalently ‘mpfr_ptr’). It |
| begins from X, and ends when it encounters a null pointer (whose |
| type must also be ‘mpfr_ptr’). |
| |
| Warning! In a given program, some other libraries might change the |
| default precision and not restore it. Thus it is safer to use |
| ‘mpfr_inits2’. |
| |
| -- Macro: MPFR_DECL_INIT (NAME, PREC) |
| This macro declares NAME as an automatic variable of type ‘mpfr_t’, |
| initializes it and sets its precision to be *exactly* PREC bits and |
| its value to NaN. NAME must be a valid identifier. You must use |
| this macro in the declaration section. This macro is much faster |
| than using ‘mpfr_init2’ but has some drawbacks: |
| |
| • You *must not* call ‘mpfr_clear’ with variables created with |
| this macro (the storage is allocated at the point of |
| declaration and deallocated when the brace-level is exited). |
| |
| • You *cannot* change their precision. |
| |
| • You *should not* create variables with huge precision with |
| this macro. |
| |
| • Your compiler must support ‘Non-Constant Initializers’ |
| (standard in C++ and ISO C99) and ‘Token Pasting’ (standard in |
| ISO C89). If PREC is not a constant expression, your compiler |
| must support ‘variable-length automatic arrays’ (standard in |
| ISO C99). GCC 2.95.3 and above supports all these features. |
| If you compile your program with GCC in C89 mode and with |
| ‘-pedantic’, you may want to define the ‘MPFR_USE_EXTENSION’ |
| macro to avoid warnings due to the ‘MPFR_DECL_INIT’ |
| implementation. |
| |
| -- Function: void mpfr_set_default_prec (mpfr_prec_t PREC) |
| Set the default precision to be *exactly* PREC bits, where PREC can |
| be any integer between ‘MPFR_PREC_MIN’ and ‘MPFR_PREC_MAX’. The |
| precision of a variable means the number of bits used to store its |
| significand. All subsequent calls to ‘mpfr_init’ or ‘mpfr_inits’ |
| will use this precision, but previously initialized variables are |
| unaffected. The default precision is set to 53 bits initially. |
| |
| Note: when MPFR is built with the ‘--enable-thread-safe’ configure |
| option, the default precision is local to each thread. *Note |
| Memory Handling::, for more information. |
| |
| -- Function: mpfr_prec_t mpfr_get_default_prec (void) |
| Return the current default MPFR precision in bits. See the |
| documentation of ‘mpfr_set_default_prec’. |
| |
| Here is an example on how to initialize floating-point variables: |
| |
| { |
| mpfr_t x, y; |
| mpfr_init (x); /* use default precision */ |
| mpfr_init2 (y, 256); /* precision _exactly_ 256 bits */ |
| … |
| /* When the program is about to exit, do ... */ |
| mpfr_clear (x); |
| mpfr_clear (y); |
| mpfr_free_cache (); /* free the cache for constants like pi */ |
| } |
| |
| The following 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 mpfr_set_prec (mpfr_t X, mpfr_prec_t PREC) |
| Reset the precision of X to be *exactly* PREC bits, and set its |
| value to NaN. The previous value stored in X is lost. It is |
| equivalent to a call to ‘mpfr_clear(x)’ followed by a call to |
| ‘mpfr_init2(x, prec)’, but more efficient as no allocation is done |
| in case the current allocated space for the significand of X is |
| enough. The precision PREC can be any integer between |
| ‘MPFR_PREC_MIN’ and ‘MPFR_PREC_MAX’. In case you want to keep the |
| previous value stored in X, use ‘mpfr_prec_round’ instead. |
| |
| Warning! You must not use this function if X was initialized with |
| ‘MPFR_DECL_INIT’ or with ‘mpfr_custom_init_set’ (*note Custom |
| Interface::). |
| |
| -- Function: mpfr_prec_t mpfr_get_prec (mpfr_t X) |
| Return the precision of X, i.e., the number of bits used to store |
| its significand. |
| |
| |
| File: mpfr.info, Node: Assignment Functions, Next: Combined Initialization and Assignment Functions, Prev: Initialization Functions, Up: MPFR Interface |
| |
| 5.2 Assignment Functions |
| ======================== |
| |
| These functions assign new values to already initialized floats (*note |
| Initialization Functions::). |
| |
| -- Function: int mpfr_set (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_ui (mpfr_t ROP, unsigned long int OP, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_set_si (mpfr_t ROP, long int OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_uj (mpfr_t ROP, uintmax_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_sj (mpfr_t ROP, intmax_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_flt (mpfr_t ROP, float OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_d (mpfr_t ROP, double OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_ld (mpfr_t ROP, long double OP, mpfr_rnd_t |
| RND) |
| -- Function: int mpfr_set_decimal64 (mpfr_t ROP, _Decimal64 OP, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_set_z (mpfr_t ROP, mpz_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_q (mpfr_t ROP, mpq_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_f (mpfr_t ROP, mpf_t OP, mpfr_rnd_t RND) |
| Set the value of ROP from OP, rounded toward the given direction |
| RND. Note that the input 0 is converted to +0 by ‘mpfr_set_ui’, |
| ‘mpfr_set_si’, ‘mpfr_set_uj’, ‘mpfr_set_sj’, ‘mpfr_set_z’, |
| ‘mpfr_set_q’ and ‘mpfr_set_f’, regardless of the rounding mode. If |
| the system does not support the IEEE 754 standard, ‘mpfr_set_flt’, |
| ‘mpfr_set_d’, ‘mpfr_set_ld’ and ‘mpfr_set_decimal64’ might not |
| preserve the signed zeros. The ‘mpfr_set_decimal64’ function is |
| built only with the configure option ‘--enable-decimal-float’, |
| which also requires ‘--with-gmp-build’, and when the compiler or |
| system provides the ‘_Decimal64’ data type (recent versions of GCC |
| support this data type); to use ‘mpfr_set_decimal64’, one should |
| define the macro ‘MPFR_WANT_DECIMAL_FLOATS’ before including |
| ‘mpfr.h’. ‘mpfr_set_q’ might fail if the numerator (or the |
| denominator) can not be represented as a ‘mpfr_t’. |
| |
| Note: If you want to store a floating-point constant to a ‘mpfr_t’, |
| you should use ‘mpfr_set_str’ (or one of the MPFR constant |
| functions, such as ‘mpfr_const_pi’ for Pi) instead of |
| ‘mpfr_set_flt’, ‘mpfr_set_d’, ‘mpfr_set_ld’ or |
| ‘mpfr_set_decimal64’. Otherwise the floating-point constant will |
| be first converted into a reduced-precision (e.g., 53-bit) binary |
| (or decimal, for ‘mpfr_set_decimal64’) number before MPFR can work |
| with it. |
| |
| -- Function: int mpfr_set_ui_2exp (mpfr_t ROP, unsigned long int OP, |
| mpfr_exp_t E, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_si_2exp (mpfr_t ROP, long int OP, mpfr_exp_t |
| E, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_uj_2exp (mpfr_t ROP, uintmax_t OP, intmax_t |
| E, mpfr_rnd_t RND) |
| -- Function: int mpfr_set_sj_2exp (mpfr_t ROP, intmax_t OP, intmax_t E, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_set_z_2exp (mpfr_t ROP, mpz_t OP, mpfr_exp_t E, |
| mpfr_rnd_t RND) |
| Set the value of ROP from OP multiplied by two to the power E, |
| rounded toward the given direction RND. Note that the input 0 is |
| converted to +0. |
| |
| -- Function: int mpfr_set_str (mpfr_t ROP, const char *S, int BASE, |
| mpfr_rnd_t RND) |
| Set ROP to the value of the string S in base BASE, rounded in the |
| direction RND. See the documentation of ‘mpfr_strtofr’ for a |
| detailed description of the valid string formats. Contrary to |
| ‘mpfr_strtofr’, ‘mpfr_set_str’ requires the _whole_ string to |
| represent a valid floating-point number. |
| |
| The meaning of the return value differs from other MPFR functions: |
| it is 0 if the entire string up to the final null character is a |
| valid number in base BASE; otherwise it is −1, and ROP may have |
| changed (users interested in the *note ternary value:: should use |
| ‘mpfr_strtofr’ instead). |
| |
| Note: it is preferable to use ‘mpfr_strtofr’ if one wants to |
| distinguish between an infinite ROP value coming from an infinite S |
| or from an overflow. |
| |
| -- Function: int mpfr_strtofr (mpfr_t ROP, const char *NPTR, char |
| **ENDPTR, int BASE, mpfr_rnd_t RND) |
| Read a floating-point number from a string NPTR in base BASE, |
| rounded in the direction RND; BASE must be either 0 (to detect the |
| base, as described below) or a number from 2 to 62 (otherwise the |
| behavior is undefined). If NPTR starts with valid data, the result |
| is stored in ROP and ‘*ENDPTR’ points to the character just after |
| the valid data (if ENDPTR is not a null pointer); otherwise ROP is |
| set to zero (for consistency with ‘strtod’) and the value of NPTR |
| is stored in the location referenced by ENDPTR (if ENDPTR is not a |
| null pointer). The usual ternary value is returned. |
| |
| Parsing follows the standard C ‘strtod’ function with some |
| extensions. After optional leading whitespace, one has a subject |
| sequence consisting of an optional sign (‘+’ or ‘-’), and either |
| numeric data or special data. The subject sequence is defined as |
| the longest initial subsequence of the input string, starting with |
| the first non-whitespace character, that is of the expected form. |
| |
| The form of numeric data is a non-empty sequence of significand |
| digits with an optional decimal point, and an optional exponent |
| consisting of an exponent prefix followed by an optional sign and a |
| non-empty sequence of decimal digits. A significand digit is |
| either a decimal digit or a Latin letter (62 possible characters), |
| with ‘A’ = 10, ‘B’ = 11, …, ‘Z’ = 35; case is ignored in bases less |
| or equal to 36, in bases larger than 36, ‘a’ = 36, ‘b’ = 37, …, ‘z’ |
| = 61. The value of a significand digit must be strictly less than |
| the base. The decimal point can be either the one defined by the |
| current locale or the period (the first one is accepted for |
| consistency with the C standard and the practice, the second one is |
| accepted to allow the programmer to provide MPFR numbers from |
| strings in a way that does not depend on the current locale). The |
| exponent prefix can be ‘e’ or ‘E’ for bases up to 10, or ‘@’ in any |
| base; it indicates a multiplication by a power of the base. In |
| bases 2 and 16, the exponent prefix can also be ‘p’ or ‘P’, in |
| which case the exponent, called _binary exponent_, indicates a |
| multiplication by a power of 2 instead of the base (there is a |
| difference only for base 16); in base 16 for example ‘1p2’ |
| represents 4 whereas ‘1@2’ represents 256. The value of an |
| exponent is always written in base 10. |
| |
| If the argument BASE is 0, then the base is automatically detected |
| as follows. If the significand starts with ‘0b’ or ‘0B’, base 2 is |
| assumed. If the significand starts with ‘0x’ or ‘0X’, base 16 is |
| assumed. Otherwise base 10 is assumed. |
| |
| Note: The exponent (if present) must contain at least a digit. |
| Otherwise the possible exponent prefix and sign are not part of the |
| number (which ends with the significand). Similarly, if ‘0b’, |
| ‘0B’, ‘0x’ or ‘0X’ is not followed by a binary/hexadecimal digit, |
| then the subject sequence stops at the character ‘0’, thus 0 is |
| read. |
| |
| Special data (for infinities and NaN) can be ‘@inf@’ or |
| ‘@nan@(n-char-sequence-opt)’, and if BASE <= 16, it can also be |
| ‘infinity’, ‘inf’, ‘nan’ or ‘nan(n-char-sequence-opt)’, all case |
| insensitive. A ‘n-char-sequence-opt’ is a possibly empty string |
| containing only digits, Latin letters and the underscore (0, 1, 2, |
| …, 9, a, b, …, z, A, B, …, Z, _). Note: one has an optional sign |
| for all data, even NaN. For example, ‘-@nAn@(This_Is_Not_17)’ is a |
| valid representation for NaN in base 17. |
| |
| -- Function: void mpfr_set_nan (mpfr_t X) |
| -- Function: void mpfr_set_inf (mpfr_t X, int SIGN) |
| -- Function: void mpfr_set_zero (mpfr_t X, int SIGN) |
| Set the variable X to NaN (Not-a-Number), infinity or zero |
| respectively. In ‘mpfr_set_inf’ or ‘mpfr_set_zero’, X is set to |
| plus infinity or plus zero iff SIGN is nonnegative; in |
| ‘mpfr_set_nan’, the sign bit of the result is unspecified. |
| |
| -- Function: void mpfr_swap (mpfr_t X, mpfr_t Y) |
| Swap the structures pointed to by X and Y. In particular, the |
| values are exchanged without rounding (this may be different from |
| three ‘mpfr_set’ calls using a third auxiliary variable). |
| |
| Warning! Since the precisions are exchanged, this will affect |
| future assignments. Moreover, since the significand pointers are |
| also exchanged, you must not use this function if the allocation |
| method used for X and/or Y does not permit it. This is the case |
| when X and/or Y were declared and initialized with |
| ‘MPFR_DECL_INIT’, and possibly with ‘mpfr_custom_init_set’ (*note |
| Custom Interface::). |
| |
| |
| File: mpfr.info, Node: Combined Initialization and Assignment Functions, Next: Conversion Functions, Prev: Assignment Functions, Up: MPFR Interface |
| |
| 5.3 Combined Initialization and Assignment Functions |
| ==================================================== |
| |
| -- Macro: int mpfr_init_set (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Macro: int mpfr_init_set_ui (mpfr_t ROP, unsigned long int OP, |
| mpfr_rnd_t RND) |
| -- Macro: int mpfr_init_set_si (mpfr_t ROP, long int OP, mpfr_rnd_t |
| RND) |
| -- Macro: int mpfr_init_set_d (mpfr_t ROP, double OP, mpfr_rnd_t RND) |
| -- Macro: int mpfr_init_set_ld (mpfr_t ROP, long double OP, mpfr_rnd_t |
| RND) |
| -- Macro: int mpfr_init_set_z (mpfr_t ROP, mpz_t OP, mpfr_rnd_t RND) |
| -- Macro: int mpfr_init_set_q (mpfr_t ROP, mpq_t OP, mpfr_rnd_t RND) |
| -- Macro: int mpfr_init_set_f (mpfr_t ROP, mpf_t OP, mpfr_rnd_t RND) |
| Initialize ROP and set its value from OP, rounded in the direction |
| RND. The precision of ROP will be taken from the active default |
| precision, as set by ‘mpfr_set_default_prec’. |
| |
| -- Function: int mpfr_init_set_str (mpfr_t X, const char *S, int BASE, |
| mpfr_rnd_t RND) |
| Initialize X and set its value from the string S in base BASE, |
| rounded in the direction RND. See ‘mpfr_set_str’. |
| |
| |
| File: mpfr.info, Node: Conversion Functions, Next: Basic Arithmetic Functions, Prev: Combined Initialization and Assignment Functions, Up: MPFR Interface |
| |
| 5.4 Conversion Functions |
| ======================== |
| |
| -- Function: float mpfr_get_flt (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: double mpfr_get_d (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: long double mpfr_get_ld (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: _Decimal64 mpfr_get_decimal64 (mpfr_t OP, mpfr_rnd_t RND) |
| Convert OP to a ‘float’ (respectively ‘double’, ‘long double’ or |
| ‘_Decimal64’), using the rounding mode RND. If OP is NaN, some |
| fixed NaN (either quiet or signaling) or the result of 0.0/0.0 is |
| returned. If OP is ±Inf, an infinity of the same sign or the |
| result of ±1.0/0.0 is returned. If OP is zero, these functions |
| return a zero, trying to preserve its sign, if possible. The |
| ‘mpfr_get_decimal64’ function is built only under some conditions: |
| see the documentation of ‘mpfr_set_decimal64’. |
| |
| -- Function: long mpfr_get_si (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: unsigned long mpfr_get_ui (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: intmax_t mpfr_get_sj (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: uintmax_t mpfr_get_uj (mpfr_t OP, mpfr_rnd_t RND) |
| Convert OP to a ‘long’, an ‘unsigned long’, an ‘intmax_t’ or an |
| ‘uintmax_t’ (respectively) after rounding it with respect to RND. |
| If OP is NaN, 0 is returned and the _erange_ flag is set. If OP is |
| too big for the return type, the function returns the maximum or |
| the minimum of the corresponding C type, depending on the direction |
| of the overflow; the _erange_ flag is set too. See also |
| ‘mpfr_fits_slong_p’, ‘mpfr_fits_ulong_p’, ‘mpfr_fits_intmax_p’ and |
| ‘mpfr_fits_uintmax_p’. |
| |
| -- Function: double mpfr_get_d_2exp (long *EXP, mpfr_t OP, mpfr_rnd_t |
| RND) |
| -- Function: long double mpfr_get_ld_2exp (long *EXP, mpfr_t OP, |
| mpfr_rnd_t RND) |
| Return D and set EXP (formally, the value pointed to by EXP) such |
| that 0.5<=abs(D)<1 and D times 2 raised to EXP equals OP rounded to |
| double (resp. long double) precision, using the given rounding |
| mode. If OP is zero, then a zero of the same sign (or an unsigned |
| zero, if the implementation does not have signed zeros) is |
| returned, and EXP is set to 0. If OP is NaN or an infinity, then |
| the corresponding double precision (resp. long-double precision) |
| value is returned, and EXP is undefined. |
| |
| -- Function: int mpfr_frexp (mpfr_exp_t *EXP, mpfr_t Y, mpfr_t X, |
| mpfr_rnd_t RND) |
| Set EXP (formally, the value pointed to by EXP) and Y such that |
| 0.5<=abs(Y)<1 and Y times 2 raised to EXP equals X rounded to the |
| precision of Y, using the given rounding mode. If X is zero, then |
| Y is set to a zero of the same sign and EXP is set to 0. If X is |
| NaN or an infinity, then Y is set to the same value and EXP is |
| undefined. |
| |
| -- Function: mpfr_exp_t mpfr_get_z_2exp (mpz_t ROP, mpfr_t OP) |
| Put the scaled significand of OP (regarded as an integer, with the |
| precision of OP) into ROP, and return the exponent EXP (which may |
| be outside the current exponent range) such that OP exactly equals |
| ROP times 2 raised to the power EXP. If OP is zero, the minimal |
| exponent ‘emin’ is returned. If OP is NaN or an infinity, the |
| _erange_ flag is set, ROP is set to 0, and the the minimal exponent |
| ‘emin’ is returned. The returned exponent may be less than the |
| minimal exponent ‘emin’ of MPFR numbers in the current exponent |
| range; in case the exponent is not representable in the |
| ‘mpfr_exp_t’ type, the _erange_ flag is set and the minimal value |
| of the ‘mpfr_exp_t’ type is returned. |
| |
| -- Function: int mpfr_get_z (mpz_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Convert OP to a ‘mpz_t’, after rounding it with respect to RND. If |
| OP is NaN or an infinity, the _erange_ flag is set, ROP is set to |
| 0, and 0 is returned. |
| |
| -- Function: int mpfr_get_f (mpf_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Convert OP to a ‘mpf_t’, after rounding it with respect to RND. |
| The _erange_ flag is set if OP is NaN or an infinity, which do not |
| exist in MPF. If OP is NaN, then ROP is undefined. If OP is +Inf |
| (resp. −Inf), then ROP is set to the maximum (resp. minimum) value |
| in the precision of the MPF number; if a future MPF version |
| supports infinities, this behavior will be considered incorrect and |
| will change (portable programs should assume that ROP is set either |
| to this finite number or to an infinite number). Note that since |
| MPFR currently has the same exponent type as MPF (but not with the |
| same radix), the range of values is much larger in MPF than in |
| MPFR, so that an overflow or underflow is not possible. |
| |
| -- Function: char * mpfr_get_str (char *STR, mpfr_exp_t *EXPPTR, int B, |
| size_t N, mpfr_t OP, mpfr_rnd_t RND) |
| Convert OP to a string of digits in base B, with rounding in the |
| direction RND, where N is either zero (see below) or the number of |
| significant digits output in the string; in the latter case, N must |
| be greater or equal to 2. The base may vary from 2 to 62; |
| otherwise the function does nothing and immediately returns a null |
| pointer. If the input number is an ordinary number, the exponent |
| is written through the pointer EXPPTR (for input 0, the current |
| minimal exponent is written); the type ‘mpfr_exp_t’ is large enough |
| to hold the exponent in all cases. |
| |
| 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. If RND is to nearest, and OP is exactly in the |
| middle of two consecutive possible outputs, the one with an even |
| significand is chosen, where both significands are considered with |
| the exponent of OP. Note that for an odd base, this may not |
| correspond to an even last digit: for example with 2 digits in base |
| 7, (14) and a half is rounded to (15) which is 12 in decimal, (16) |
| and a half is rounded to (20) which is 14 in decimal, and (26) and |
| a half is rounded to (26) which is 20 in decimal. |
| |
| If N is zero, the number of digits of the significand is chosen |
| large enough so that re-reading the printed value with the same |
| precision, assuming both output and input use rounding to nearest, |
| will recover the original value of OP. More precisely, in most |
| cases, the chosen precision of STR is the minimal precision m |
| depending only on P = PREC(OP) and B that satisfies the above |
| property, i.e., m = 1 + ceil(P*log(2)/log(B)), with P replaced by |
| P−1 if B is a power of 2, but in some very rare cases, it might be |
| m+1 (the smallest case for bases up to 62 is when P equals |
| 186564318007 for bases 7 and 49). |
| |
| If STR is a null pointer, space for the significand is allocated |
| using the current allocation function and a pointer to the string |
| is returned (unless the base is invalid). To free the returned |
| string, you must use ‘mpfr_free_str’. |
| |
| If STR is not a null pointer, it should point to a block of storage |
| large enough for the significand, i.e., at least ‘max(N + 2, 7)’. |
| The extra two bytes are for a possible minus sign, and for the |
| terminating null character, and the value 7 accounts for ‘-@Inf@’ |
| plus the terminating null character. The pointer to the string STR |
| is returned (unless the base is invalid). |
| |
| Note: The NaN and inexact flags are currently not set when need be; |
| this will be fixed in future versions. Programmers should |
| currently assume that whether the flags are set by this function is |
| unspecified. |
| |
| -- Function: void mpfr_free_str (char *STR) |
| Free a string allocated by ‘mpfr_get_str’ using the current |
| unallocation function. The block is assumed to be ‘strlen(STR)+1’ |
| bytes. For more information about how it is done: *note |
| (gmp.info)Custom Allocation::. |
| |
| -- Function: int mpfr_fits_ulong_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_slong_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_uint_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_sint_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_ushort_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_sshort_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_uintmax_p (mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_fits_intmax_p (mpfr_t OP, mpfr_rnd_t RND) |
| Return non-zero if OP would fit in the respective C data type, |
| respectively ‘unsigned long’, ‘long’, ‘unsigned int’, ‘int’, |
| ‘unsigned short’, ‘short’, ‘uintmax_t’, ‘intmax_t’, when rounded to |
| an integer in the direction RND. |
| |
| |
| File: mpfr.info, Node: Basic Arithmetic Functions, Next: Comparison Functions, Prev: Conversion Functions, Up: MPFR Interface |
| |
| 5.5 Basic Arithmetic Functions |
| ============================== |
| |
| -- Function: int mpfr_add (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_add_ui (mpfr_t ROP, mpfr_t OP1, unsigned long int |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_add_si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_add_d (mpfr_t ROP, mpfr_t OP1, double OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_add_z (mpfr_t ROP, mpfr_t OP1, mpz_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_add_q (mpfr_t ROP, mpfr_t OP1, mpq_t OP2, |
| mpfr_rnd_t RND) |
| Set ROP to OP1 + OP2 rounded in the direction RND. For types |
| having no signed zero, it is considered unsigned (i.e., (+0) + 0 = |
| (+0) and (−0) + 0 = (−0)). The ‘mpfr_add_d’ function assumes that |
| the radix of the ‘double’ type is a power of 2, with a precision at |
| most that declared by the C implementation (macro |
| ‘IEEE_DBL_MANT_DIG’, and if not defined 53 bits). |
| |
| -- Function: int mpfr_sub (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_ui_sub (mpfr_t ROP, unsigned long int OP1, mpfr_t |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_sub_ui (mpfr_t ROP, mpfr_t OP1, unsigned long int |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_si_sub (mpfr_t ROP, long int OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_sub_si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_d_sub (mpfr_t ROP, double OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_sub_d (mpfr_t ROP, mpfr_t OP1, double OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_z_sub (mpfr_t ROP, mpz_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_sub_z (mpfr_t ROP, mpfr_t OP1, mpz_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_sub_q (mpfr_t ROP, mpfr_t OP1, mpq_t OP2, |
| mpfr_rnd_t RND) |
| Set ROP to OP1 - OP2 rounded in the direction RND. For types |
| having no signed zero, it is considered unsigned (i.e., (+0) − 0 = |
| (+0), (−0) − 0 = (−0), 0 − (+0) = (−0) and 0 − (−0) = (+0)). The |
| same restrictions than for ‘mpfr_add_d’ apply to ‘mpfr_d_sub’ and |
| ‘mpfr_sub_d’. |
| |
| -- Function: int mpfr_mul (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_mul_ui (mpfr_t ROP, mpfr_t OP1, unsigned long int |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_mul_si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_mul_d (mpfr_t ROP, mpfr_t OP1, double OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_mul_z (mpfr_t ROP, mpfr_t OP1, mpz_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_mul_q (mpfr_t ROP, mpfr_t OP1, mpq_t OP2, |
| mpfr_rnd_t RND) |
| Set ROP to OP1 times OP2 rounded in the direction RND. When a |
| result is zero, its sign is the product of the signs of the |
| operands (for types having no signed zero, it is considered |
| positive). The same restrictions than for ‘mpfr_add_d’ apply to |
| ‘mpfr_mul_d’. |
| |
| -- Function: int mpfr_sqr (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the square of OP rounded in the direction RND. |
| |
| -- Function: int mpfr_div (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_ui_div (mpfr_t ROP, unsigned long int OP1, mpfr_t |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_div_ui (mpfr_t ROP, mpfr_t OP1, unsigned long int |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_si_div (mpfr_t ROP, long int OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_div_si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_d_div (mpfr_t ROP, double OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_div_d (mpfr_t ROP, mpfr_t OP1, double OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_div_z (mpfr_t ROP, mpfr_t OP1, mpz_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_div_q (mpfr_t ROP, mpfr_t OP1, mpq_t OP2, |
| mpfr_rnd_t RND) |
| Set ROP to OP1/OP2 rounded in the direction RND. When a result is |
| zero, its sign is the product of the signs of the operands (for |
| types having no signed zero, it is considered positive). The same |
| restrictions than for ‘mpfr_add_d’ apply to ‘mpfr_d_div’ and |
| ‘mpfr_div_d’. |
| |
| -- Function: int mpfr_sqrt (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_sqrt_ui (mpfr_t ROP, unsigned long int OP, |
| mpfr_rnd_t RND) |
| Set ROP to the square root of OP rounded in the direction RND (set |
| ROP to −0 if OP is −0, to be consistent with the IEEE 754 |
| standard). Set ROP to NaN if OP is negative. |
| |
| -- Function: int mpfr_rec_sqrt (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the reciprocal square root of OP rounded in the |
| direction RND. Set ROP to +Inf if OP is ±0, +0 if OP is +Inf, and |
| NaN if OP is negative. |
| |
| -- Function: int mpfr_cbrt (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_root (mpfr_t ROP, mpfr_t OP, unsigned long int K, |
| mpfr_rnd_t RND) |
| Set ROP to the cubic root (resp. the Kth root) of OP rounded in the |
| direction RND. For K odd (resp. even) and OP negative (including |
| −Inf), set ROP to a negative number (resp. NaN). The Kth root of −0 |
| is defined to be −0, whatever the parity of K. |
| |
| -- Function: int mpfr_pow (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_pow_ui (mpfr_t ROP, mpfr_t OP1, unsigned long int |
| OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_pow_si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_pow_z (mpfr_t ROP, mpfr_t OP1, mpz_t OP2, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_ui_pow_ui (mpfr_t ROP, unsigned long int OP1, |
| unsigned long int OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_ui_pow (mpfr_t ROP, unsigned long int OP1, mpfr_t |
| OP2, mpfr_rnd_t RND) |
| Set ROP to OP1 raised to OP2, rounded in the direction RND. |
| Special values are handled as described in the ISO C99 and IEEE |
| 754-2008 standards for the ‘pow’ function: |
| • ‘pow(±0, Y)’ returns plus or minus infinity for Y a negative |
| odd integer. |
| • ‘pow(±0, Y)’ returns plus infinity for Y negative and not an |
| odd integer. |
| • ‘pow(±0, Y)’ returns plus or minus zero for Y a positive odd |
| integer. |
| • ‘pow(±0, Y)’ returns plus zero for Y positive and not an odd |
| integer. |
| • ‘pow(-1, ±Inf)’ returns 1. |
| • ‘pow(+1, Y)’ returns 1 for any Y, even a NaN. |
| • ‘pow(X, ±0)’ returns 1 for any X, even a NaN. |
| • ‘pow(X, Y)’ returns NaN for finite negative X and finite |
| non-integer Y. |
| • ‘pow(X, -Inf)’ returns plus infinity for 0 < abs(x) < 1, and |
| plus zero for abs(x) > 1. |
| • ‘pow(X, +Inf)’ returns plus zero for 0 < abs(x) < 1, and plus |
| infinity for abs(x) > 1. |
| • ‘pow(-Inf, Y)’ returns minus zero for Y a negative odd |
| integer. |
| • ‘pow(-Inf, Y)’ returns plus zero for Y negative and not an odd |
| integer. |
| • ‘pow(-Inf, Y)’ returns minus infinity for Y a positive odd |
| integer. |
| • ‘pow(-Inf, Y)’ returns plus infinity for Y positive and not an |
| odd integer. |
| • ‘pow(+Inf, Y)’ returns plus zero for Y negative, and plus |
| infinity for Y positive. |
| |
| -- Function: int mpfr_neg (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_abs (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to -OP and the absolute value of OP respectively, rounded |
| in the direction RND. Just changes or adjusts the sign if ROP and |
| OP are the same variable, otherwise a rounding might occur if the |
| precision of ROP is less than that of OP. |
| |
| -- Function: int mpfr_dim (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| Set ROP to the positive difference of OP1 and OP2, i.e., OP1 - OP2 |
| rounded in the direction RND if OP1 > OP2, +0 if OP1 <= OP2, and |
| NaN if OP1 or OP2 is NaN. |
| |
| -- Function: int mpfr_mul_2ui (mpfr_t ROP, mpfr_t OP1, unsigned long |
| int OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_mul_2si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| Set ROP to OP1 times 2 raised to OP2 rounded in the direction RND. |
| Just increases the exponent by OP2 when ROP and OP1 are identical. |
| |
| -- Function: int mpfr_div_2ui (mpfr_t ROP, mpfr_t OP1, unsigned long |
| int OP2, mpfr_rnd_t RND) |
| -- Function: int mpfr_div_2si (mpfr_t ROP, mpfr_t OP1, long int OP2, |
| mpfr_rnd_t RND) |
| Set ROP to OP1 divided by 2 raised to OP2 rounded in the direction |
| RND. Just decreases the exponent by OP2 when ROP and OP1 are |
| identical. |
| |
| |
| File: mpfr.info, Node: Comparison Functions, Next: Special Functions, Prev: Basic Arithmetic Functions, Up: MPFR Interface |
| |
| 5.6 Comparison Functions |
| ======================== |
| |
| -- Function: int mpfr_cmp (mpfr_t OP1, mpfr_t OP2) |
| -- Function: int mpfr_cmp_ui (mpfr_t OP1, unsigned long int OP2) |
| -- Function: int mpfr_cmp_si (mpfr_t OP1, long int OP2) |
| -- Function: int mpfr_cmp_d (mpfr_t OP1, double OP2) |
| -- Function: int mpfr_cmp_ld (mpfr_t OP1, long double OP2) |
| -- Function: int mpfr_cmp_z (mpfr_t OP1, mpz_t OP2) |
| -- Function: int mpfr_cmp_q (mpfr_t OP1, mpq_t OP2) |
| -- Function: int mpfr_cmp_f (mpfr_t OP1, mpf_t OP2) |
| Compare OP1 and OP2. Return a positive value if OP1 > OP2, zero if |
| OP1 = OP2, and a negative value if OP1 < OP2. Both OP1 and OP2 are |
| considered to their full own precision, which may differ. If one |
| of the operands is NaN, set the _erange_ flag and return zero. |
| |
| Note: These functions may be useful to distinguish the three |
| possible cases. If you need to distinguish two cases only, it is |
| recommended to use the predicate functions (e.g., ‘mpfr_equal_p’ |
| for the equality) described below; they behave like the IEEE 754 |
| comparisons, in particular when one or both arguments are NaN. But |
| only floating-point numbers can be compared (you may need to do a |
| conversion first). |
| |
| -- Function: int mpfr_cmp_ui_2exp (mpfr_t OP1, unsigned long int OP2, |
| mpfr_exp_t E) |
| -- Function: int mpfr_cmp_si_2exp (mpfr_t OP1, long int OP2, mpfr_exp_t |
| E) |
| Compare OP1 and OP2 multiplied by two to the power E. Similar as |
| above. |
| |
| -- Function: int mpfr_cmpabs (mpfr_t OP1, mpfr_t OP2) |
| Compare |OP1| and |OP2|. Return a positive value if |OP1| > |OP2|, |
| zero if |OP1| = |OP2|, and a negative value if |OP1| < |OP2|. If |
| one of the operands is NaN, set the _erange_ flag and return zero. |
| |
| -- Function: int mpfr_nan_p (mpfr_t OP) |
| -- Function: int mpfr_inf_p (mpfr_t OP) |
| -- Function: int mpfr_number_p (mpfr_t OP) |
| -- Function: int mpfr_zero_p (mpfr_t OP) |
| -- Function: int mpfr_regular_p (mpfr_t OP) |
| Return non-zero if OP is respectively NaN, an infinity, an ordinary |
| number (i.e., neither NaN nor an infinity), zero, or a regular |
| number (i.e., neither NaN, nor an infinity nor zero). Return zero |
| otherwise. |
| |
| -- Macro: int mpfr_sgn (mpfr_t OP) |
| Return a positive value if OP > 0, zero if OP = 0, and a negative |
| value if OP < 0. If the operand is NaN, set the _erange_ flag and |
| return zero. This is equivalent to ‘mpfr_cmp_ui (op, 0)’, but more |
| efficient. |
| |
| -- Function: int mpfr_greater_p (mpfr_t OP1, mpfr_t OP2) |
| -- Function: int mpfr_greaterequal_p (mpfr_t OP1, mpfr_t OP2) |
| -- Function: int mpfr_less_p (mpfr_t OP1, mpfr_t OP2) |
| -- Function: int mpfr_lessequal_p (mpfr_t OP1, mpfr_t OP2) |
| -- Function: int mpfr_equal_p (mpfr_t OP1, mpfr_t OP2) |
| Return non-zero if OP1 > OP2, OP1 >= OP2, OP1 < OP2, OP1 <= OP2, |
| OP1 = OP2 respectively, and zero otherwise. Those functions return |
| zero whenever OP1 and/or OP2 is NaN. |
| |
| -- Function: int mpfr_lessgreater_p (mpfr_t OP1, mpfr_t OP2) |
| Return non-zero if OP1 < OP2 or OP1 > OP2 (i.e., neither OP1, nor |
| OP2 is NaN, and OP1 <> OP2), zero otherwise (i.e., OP1 and/or OP2 |
| is NaN, or OP1 = OP2). |
| |
| -- Function: int mpfr_unordered_p (mpfr_t OP1, mpfr_t OP2) |
| Return non-zero if OP1 or OP2 is a NaN (i.e., they cannot be |
| compared), zero otherwise. |
| |
| |
| File: mpfr.info, Node: Special Functions, Next: Input and Output Functions, Prev: Comparison Functions, Up: MPFR Interface |
| |
| 5.7 Special Functions |
| ===================== |
| |
| All those functions, except explicitly stated (for example |
| ‘mpfr_sin_cos’), return a *note ternary value::, i.e., zero for an exact |
| return value, a positive value for a return value larger than the exact |
| result, and a negative value otherwise. |
| |
| Important note: in some domains, computing special functions (either |
| with correct or incorrect rounding) is expensive, even for small |
| precision, for example the trigonometric and Bessel functions for large |
| argument. |
| |
| -- Function: int mpfr_log (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_log2 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_log10 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the natural logarithm of OP, log2(OP) or log10(OP), |
| respectively, rounded in the direction RND. Set ROP to −Inf if OP |
| is −0 (i.e., the sign of the zero has no influence on the result). |
| |
| -- Function: int mpfr_exp (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_exp2 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_exp10 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the exponential of OP, to 2 power of OP or to 10 power |
| of OP, respectively, rounded in the direction RND. |
| |
| -- Function: int mpfr_cos (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_sin (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_tan (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the cosine of OP, sine of OP, tangent of OP, rounded in |
| the direction RND. |
| |
| -- Function: int mpfr_sin_cos (mpfr_t SOP, mpfr_t COP, mpfr_t OP, |
| mpfr_rnd_t RND) |
| Set simultaneously SOP to the sine of OP and COP to the cosine of |
| OP, rounded in the direction RND with the corresponding precisions |
| of SOP and COP, which must be different variables. Return 0 iff |
| both results are exact, more precisely it returns s+4c where s=0 if |
| SOP is exact, s=1 if SOP is larger than the sine of OP, s=2 if SOP |
| is smaller than the sine of OP, and similarly for c and the cosine |
| of OP. |
| |
| -- Function: int mpfr_sec (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_csc (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_cot (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the secant of OP, cosecant of OP, cotangent of OP, |
| rounded in the direction RND. |
| |
| -- Function: int mpfr_acos (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_asin (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_atan (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the arc-cosine, arc-sine or arc-tangent of OP, rounded |
| in the direction RND. Note that since ‘acos(-1)’ returns the |
| floating-point number closest to Pi according to the given rounding |
| mode, this number might not be in the output range 0 <= ROP < \pi |
| of the arc-cosine function; still, the result lies in the image of |
| the output range by the rounding function. The same holds for |
| ‘asin(-1)’, ‘asin(1)’, ‘atan(-Inf)’, ‘atan(+Inf)’ or for ‘atan(op)’ |
| with large OP and small precision of ROP. |
| |
| -- Function: int mpfr_atan2 (mpfr_t ROP, mpfr_t Y, mpfr_t X, mpfr_rnd_t |
| RND) |
| Set ROP to the arc-tangent2 of Y and X, rounded in the direction |
| RND: if ‘x > 0’, ‘atan2(y, x) = atan (y/x)’; if ‘x < 0’, ‘atan2(y, |
| x) = sign(y)*(Pi - atan (abs(y/x)))’, thus a number from -Pi to Pi. |
| As for ‘atan’, in case the exact mathematical result is +Pi or -Pi, |
| its rounded result might be outside the function output range. |
| |
| ‘atan2(y, 0)’ does not raise any floating-point exception. Special |
| values are handled as described in the ISO C99 and IEEE 754-2008 |
| standards for the ‘atan2’ function: |
| • ‘atan2(+0, -0)’ returns +Pi. |
| • ‘atan2(-0, -0)’ returns -Pi. |
| • ‘atan2(+0, +0)’ returns +0. |
| • ‘atan2(-0, +0)’ returns −0. |
| • ‘atan2(+0, x)’ returns +Pi for x < 0. |
| • ‘atan2(-0, x)’ returns -Pi for x < 0. |
| • ‘atan2(+0, x)’ returns +0 for x > 0. |
| • ‘atan2(-0, x)’ returns −0 for x > 0. |
| • ‘atan2(y, 0)’ returns -Pi/2 for y < 0. |
| • ‘atan2(y, 0)’ returns +Pi/2 for y > 0. |
| • ‘atan2(+Inf, -Inf)’ returns +3*Pi/4. |
| • ‘atan2(-Inf, -Inf)’ returns -3*Pi/4. |
| • ‘atan2(+Inf, +Inf)’ returns +Pi/4. |
| • ‘atan2(-Inf, +Inf)’ returns -Pi/4. |
| • ‘atan2(+Inf, x)’ returns +Pi/2 for finite x. |
| • ‘atan2(-Inf, x)’ returns -Pi/2 for finite x. |
| • ‘atan2(y, -Inf)’ returns +Pi for finite y > 0. |
| • ‘atan2(y, -Inf)’ returns -Pi for finite y < 0. |
| • ‘atan2(y, +Inf)’ returns +0 for finite y > 0. |
| • ‘atan2(y, +Inf)’ returns −0 for finite y < 0. |
| |
| -- Function: int mpfr_cosh (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_sinh (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_tanh (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the hyperbolic cosine, sine or tangent of OP, rounded in |
| the direction RND. |
| |
| -- Function: int mpfr_sinh_cosh (mpfr_t SOP, mpfr_t COP, mpfr_t OP, |
| mpfr_rnd_t RND) |
| Set simultaneously SOP to the hyperbolic sine of OP and COP to the |
| hyperbolic cosine of OP, rounded in the direction RND with the |
| corresponding precision of SOP and COP, which must be different |
| variables. Return 0 iff both results are exact (see ‘mpfr_sin_cos’ |
| for a more detailed description of the return value). |
| |
| -- Function: int mpfr_sech (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_csch (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_coth (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the hyperbolic secant of OP, cosecant of OP, cotangent |
| of OP, rounded in the direction RND. |
| |
| -- Function: int mpfr_acosh (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_asinh (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_atanh (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the inverse hyperbolic cosine, sine or tangent of OP, |
| rounded in the direction RND. |
| |
| -- Function: int mpfr_fac_ui (mpfr_t ROP, unsigned long int OP, |
| mpfr_rnd_t RND) |
| Set ROP to the factorial of OP, rounded in the direction RND. |
| |
| -- Function: int mpfr_log1p (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the logarithm of one plus OP, rounded in the direction |
| RND. |
| |
| -- Function: int mpfr_expm1 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the exponential of OP followed by a subtraction by one, |
| rounded in the direction RND. |
| |
| -- Function: int mpfr_eint (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the exponential integral of OP, rounded in the direction |
| RND. For positive OP, the exponential integral is the sum of |
| Euler’s constant, of the logarithm of OP, and of the sum for k from |
| 1 to infinity of OP to the power k, divided by k and factorial(k). |
| For negative OP, ROP is set to NaN (this definition for negative |
| argument follows formula 5.1.2 from the Handbook of Mathematical |
| Functions from Abramowitz and Stegun, a future version might use |
| another definition). |
| |
| -- Function: int mpfr_li2 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to real part of the dilogarithm of OP, rounded in the |
| direction RND. MPFR defines the dilogarithm function as the |
| integral of -log(1-t)/t from 0 to OP. |
| |
| -- Function: int mpfr_gamma (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the value of the Gamma function on OP, rounded in the |
| direction RND. When OP is a negative integer, ROP is set to NaN. |
| |
| -- Function: int mpfr_lngamma (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the value of the logarithm of the Gamma function on OP, |
| rounded in the direction RND. When −2K−1 <= OP <= −2K, K being a |
| non-negative integer, ROP is set to NaN. See also ‘mpfr_lgamma’. |
| |
| -- Function: int mpfr_lgamma (mpfr_t ROP, int *SIGNP, mpfr_t OP, |
| mpfr_rnd_t RND) |
| Set ROP to the value of the logarithm of the absolute value of the |
| Gamma function on OP, rounded in the direction RND. The sign (1 or |
| −1) of Gamma(OP) is returned in the object pointed to by SIGNP. |
| When OP is an infinity or a non-positive integer, set ROP to +Inf. |
| When OP is NaN, −Inf or a negative integer, *SIGNP is undefined, |
| and when OP is ±0, *SIGNP is the sign of the zero. |
| |
| -- Function: int mpfr_digamma (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the value of the Digamma (sometimes also called Psi) |
| function on OP, rounded in the direction RND. When OP is a |
| negative integer, set ROP to NaN. |
| |
| -- Function: int mpfr_zeta (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_zeta_ui (mpfr_t ROP, unsigned long OP, mpfr_rnd_t |
| RND) |
| Set ROP to the value of the Riemann Zeta function on OP, rounded in |
| the direction RND. |
| |
| -- Function: int mpfr_erf (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_erfc (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the value of the error function on OP (resp. the |
| complementary error function on OP) rounded in the direction RND. |
| |
| -- Function: int mpfr_j0 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_j1 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_jn (mpfr_t ROP, long N, mpfr_t OP, mpfr_rnd_t |
| RND) |
| Set ROP to the value of the first kind Bessel function of order 0, |
| (resp. 1 and N) on OP, rounded in the direction RND. When OP is |
| NaN, ROP is always set to NaN. When OP is plus or minus Infinity, |
| ROP is set to +0. When OP is zero, and N is not zero, ROP is set |
| to +0 or −0 depending on the parity and sign of N, and the sign of |
| OP. |
| |
| -- Function: int mpfr_y0 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_y1 (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_yn (mpfr_t ROP, long N, mpfr_t OP, mpfr_rnd_t |
| RND) |
| Set ROP to the value of the second kind Bessel function of order 0 |
| (resp. 1 and N) on OP, rounded in the direction RND. When OP is |
| NaN or negative, ROP is always set to NaN. When OP is +Inf, ROP is |
| set to +0. When OP is zero, ROP is set to +Inf or −Inf depending |
| on the parity and sign of N. |
| |
| -- Function: int mpfr_fma (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, mpfr_t |
| OP3, mpfr_rnd_t RND) |
| -- Function: int mpfr_fms (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, mpfr_t |
| OP3, mpfr_rnd_t RND) |
| Set ROP to (OP1 times OP2) + OP3 (resp. (OP1 times OP2) - OP3) |
| rounded in the direction RND. |
| |
| -- Function: int mpfr_agm (mpfr_t ROP, mpfr_t OP1, mpfr_t OP2, |
| mpfr_rnd_t RND) |
| Set ROP to the arithmetic-geometric mean of OP1 and OP2, rounded in |
| the direction RND. The arithmetic-geometric mean is the common |
| limit of the sequences U_N and V_N, where U_0=OP1, V_0=OP2, U_(N+1) |
| is the arithmetic mean of U_N and V_N, and V_(N+1) is the geometric |
| mean of U_N and V_N. If any operand is negative, set ROP to NaN. |
| |
| -- Function: int mpfr_hypot (mpfr_t ROP, mpfr_t X, mpfr_t Y, mpfr_rnd_t |
| RND) |
| Set ROP to the Euclidean norm of X and Y, i.e., the square root of |
| the sum of the squares of X and Y, rounded in the direction RND. |
| Special values are handled as described in Section F.9.4.3 of the |
| ISO C99 and IEEE 754-2008 standards: If X or Y is an infinity, then |
| +Inf is returned in ROP, even if the other number is NaN. |
| |
| -- Function: int mpfr_ai (mpfr_t ROP, mpfr_t X, mpfr_rnd_t RND) |
| Set ROP to the value of the Airy function Ai on X, rounded in the |
| direction RND. When X is NaN, ROP is always set to NaN. When X is |
| +Inf or −Inf, ROP is +0. The current implementation is not |
| intended to be used with large arguments. It works with abs(X) |
| typically smaller than 500. For larger arguments, other methods |
| should be used and will be implemented in a future version. |
| |
| -- Function: int mpfr_const_log2 (mpfr_t ROP, mpfr_rnd_t RND) |
| -- Function: int mpfr_const_pi (mpfr_t ROP, mpfr_rnd_t RND) |
| -- Function: int mpfr_const_euler (mpfr_t ROP, mpfr_rnd_t RND) |
| -- Function: int mpfr_const_catalan (mpfr_t ROP, mpfr_rnd_t RND) |
| Set ROP to the logarithm of 2, the value of Pi, of Euler’s constant |
| 0.577…, of Catalan’s constant 0.915…, respectively, rounded in the |
| direction RND. These functions cache the computed values to avoid |
| other calculations if a lower or equal precision is requested. To |
| free these caches, use ‘mpfr_free_cache’. |
| |
| -- Function: void mpfr_free_cache (void) |
| Free various caches used by MPFR internally, in particular the |
| caches used by the functions computing constants |
| (‘mpfr_const_log2’, ‘mpfr_const_pi’, ‘mpfr_const_euler’ and |
| ‘mpfr_const_catalan’). You should call this function before |
| terminating a thread, even if you did not call these functions |
| directly (they could have been called internally). |
| |
| -- Function: int mpfr_sum (mpfr_t ROP, mpfr_ptr const TAB[], unsigned |
| long int N, mpfr_rnd_t RND) |
| Set ROP to the sum of all elements of TAB, whose size is N, rounded |
| in the direction RND. Warning: for efficiency reasons, TAB is an |
| array of pointers to ‘mpfr_t’, not an array of ‘mpfr_t’. If the |
| returned ‘int’ value is zero, ROP is guaranteed to be the exact |
| sum; otherwise ROP might be smaller than, equal to, or larger than |
| the exact sum (in accordance to the rounding mode). However, |
| ‘mpfr_sum’ does guarantee the result is correctly rounded. |
| |
| |
| File: mpfr.info, Node: Input and Output Functions, Next: Formatted Output Functions, Prev: Special Functions, Up: MPFR Interface |
| |
| 5.8 Input and Output Functions |
| ============================== |
| |
| This section describes functions that perform input from an input/output |
| stream, and functions that output to an input/output stream. Passing a |
| null pointer for a ‘stream’ to any of these functions will make them |
| read from ‘stdin’ and write to ‘stdout’, respectively. |
| |
| When using any of these functions, you must include the ‘<stdio.h>’ |
| standard header before ‘mpfr.h’, to allow ‘mpfr.h’ to define prototypes |
| for these functions. |
| |
| -- Function: size_t mpfr_out_str (FILE *STREAM, int BASE, size_t N, |
| mpfr_t OP, mpfr_rnd_t RND) |
| Output OP on stream STREAM, as a string of digits in base BASE, |
| rounded in the direction RND. The base may vary from 2 to 62. |
| Print N significant digits exactly, or if N is 0, enough digits so |
| that OP can be read back exactly (see ‘mpfr_get_str’). |
| |
| In addition to the significant digits, a decimal point (defined by |
| the current locale) at the right of the first digit and a trailing |
| exponent in base 10, 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 characters written, or if an error occurred, |
| return 0. |
| |
| -- Function: size_t mpfr_inp_str (mpfr_t ROP, FILE *STREAM, int BASE, |
| mpfr_rnd_t RND) |
| Input a string in base BASE from stream STREAM, rounded in the |
| direction RND, and put the read float in ROP. |
| |
| This function reads a word (defined as a sequence of characters |
| between whitespace) and parses it using ‘mpfr_set_str’. See the |
| documentation of ‘mpfr_strtofr’ for a detailed description of the |
| valid string formats. |
| |
| Return the number of bytes read, or if an error occurred, return 0. |
| |
| |
| File: mpfr.info, Node: Formatted Output Functions, Next: Integer Related Functions, Prev: Input and Output Functions, Up: MPFR Interface |
| |
| 5.9 Formatted Output Functions |
| ============================== |
| |
| 5.9.1 Requirements |
| ------------------ |
| |
| The class of ‘mpfr_printf’ functions provides formatted output in a |
| similar manner as the standard C ‘printf’. These functions are defined |
| only if your system supports ISO C variadic functions and the |
| corresponding argument access macros. |
| |
| When using any of these functions, you must include the ‘<stdio.h>’ |
| standard header before ‘mpfr.h’, to allow ‘mpfr.h’ to define prototypes |
| for these functions. |
| |
| 5.9.2 Format String |
| ------------------- |
| |
| The format specification accepted by ‘mpfr_printf’ is an extension of |
| the ‘printf’ one. The conversion specification is of the form: |
| % [flags] [width] [.[precision]] [type] [rounding] conv |
| ‘flags’, ‘width’, and ‘precision’ have the same meaning as for the |
| standard ‘printf’ (in particular, notice that the ‘precision’ is related |
| to the number of digits displayed in the base chosen by ‘conv’ and not |
| related to the internal precision of the ‘mpfr_t’ variable). |
| ‘mpfr_printf’ accepts the same ‘type’ specifiers as GMP (except the |
| non-standard and deprecated ‘q’, use ‘ll’ instead), namely the length |
| modifiers defined in the C standard: |
| |
| ‘h’ ‘short’ |
| ‘hh’ ‘char’ |
| ‘j’ ‘intmax_t’ or ‘uintmax_t’ |
| ‘l’ ‘long’ or ‘wchar_t’ |
| ‘ll’ ‘long long’ |
| ‘L’ ‘long double’ |
| ‘t’ ‘ptrdiff_t’ |
| ‘z’ ‘size_t’ |
| |
| and the ‘type’ specifiers defined in GMP plus ‘R’ and ‘P’ specific to |
| MPFR (the second column in the table below shows the type of the |
| argument read in the argument list and the kind of ‘conv’ specifier to |
| use after the ‘type’ specifier): |
| |
| ‘F’ ‘mpf_t’, float conversions |
| ‘Q’ ‘mpq_t’, integer conversions |
| ‘M’ ‘mp_limb_t’, integer conversions |
| ‘N’ ‘mp_limb_t’ array, integer conversions |
| ‘Z’ ‘mpz_t’, integer conversions |
| |
| ‘P’ ‘mpfr_prec_t’, integer conversions |
| ‘R’ ‘mpfr_t’, float conversions |
| |
| The ‘type’ specifiers have the same restrictions as those mentioned |
| in the GMP documentation: *note (gmp.info)Formatted Output Strings::. |
| In particular, the ‘type’ specifiers (except ‘R’ and ‘P’) are supported |
| only if they are supported by ‘gmp_printf’ in your GMP build; this |
| implies that the standard specifiers, such as ‘t’, must _also_ be |
| supported by your C library if you want to use them. |
| |
| The ‘rounding’ field is specific to ‘mpfr_t’ arguments and should not |
| be used with other types. |
| |
| With conversion specification not involving ‘P’ and ‘R’ types, |
| ‘mpfr_printf’ behaves exactly as ‘gmp_printf’. |
| |
| The ‘P’ type specifies that a following ‘d’, ‘i’, ‘o’, ‘u’, ‘x’, or |
| ‘X’ conversion specifier applies to a ‘mpfr_prec_t’ argument. It is |
| needed because the ‘mpfr_prec_t’ type does not necessarily correspond to |
| an ‘int’ or any fixed standard type. The ‘precision’ field specifies |
| the minimum number of digits to appear. The default ‘precision’ is 1. |
| For example: |
| mpfr_t x; |
| mpfr_prec_t p; |
| mpfr_init (x); |
| … |
| p = mpfr_get_prec (x); |
| mpfr_printf ("variable x with %Pu bits", p); |
| |
| The ‘R’ type specifies that a following ‘a’, ‘A’, ‘b’, ‘e’, ‘E’, ‘f’, |
| ‘F’, ‘g’, ‘G’, or ‘n’ conversion specifier applies to a ‘mpfr_t’ |
| argument. The ‘R’ type can be followed by a ‘rounding’ specifier |
| denoted by one of the following characters: |
| |
| ‘U’ round toward plus infinity |
| ‘D’ round toward minus infinity |
| ‘Y’ round away from zero |
| ‘Z’ round toward zero |
| ‘N’ round to nearest (with ties to even) |
| ‘*’ rounding mode indicated by the |
| ‘mpfr_rnd_t’ argument just before the |
| corresponding ‘mpfr_t’ variable. |
| |
| The default rounding mode is rounding to nearest. The following |
| three examples are equivalent: |
| mpfr_t x; |
| mpfr_init (x); |
| … |
| mpfr_printf ("%.128Rf", x); |
| mpfr_printf ("%.128RNf", x); |
| mpfr_printf ("%.128R*f", MPFR_RNDN, x); |
| |
| Note that the rounding away from zero mode is specified with ‘Y’ |
| because ISO C reserves the ‘A’ specifier for hexadecimal output (see |
| below). |
| |
| The output ‘conv’ specifiers allowed with ‘mpfr_t’ parameter are: |
| |
| ‘a’ ‘A’ hex float, C99 style |
| ‘b’ binary output |
| ‘e’ ‘E’ scientific format float |
| ‘f’ ‘F’ fixed point float |
| ‘g’ ‘G’ fixed or scientific float |
| |
| The conversion specifier ‘b’ which displays the argument in binary is |
| specific to ‘mpfr_t’ arguments and should not be used with other types. |
| Other conversion specifiers have the same meaning as for a ‘double’ |
| argument. |
| |
| In case of non-decimal output, only the significand is written in the |
| specified base, the exponent is always displayed in decimal. Special |
| values are always displayed as ‘nan’, ‘-inf’, and ‘inf’ for ‘a’, ‘b’, |
| ‘e’, ‘f’, and ‘g’ specifiers and ‘NAN’, ‘-INF’, and ‘INF’ for ‘A’, ‘E’, |
| ‘F’, and ‘G’ specifiers. |
| |
| If the ‘precision’ field is not empty, the ‘mpfr_t’ number is rounded |
| to the given precision in the direction specified by the rounding mode. |
| If the precision is zero with rounding to nearest mode and one of the |
| following ‘conv’ specifiers: ‘a’, ‘A’, ‘b’, ‘e’, ‘E’, tie case is |
| rounded to even when it lies between two consecutive values at the |
| wanted precision which have the same exponent, otherwise, it is rounded |
| away from zero. For instance, 85 is displayed as "8e+1" and 95 is |
| displayed as "1e+2" with the format specification ‘"%.0RNe"’. This also |
| applies when the ‘g’ (resp. ‘G’) conversion specifier uses the ‘e’ |
| (resp. ‘E’) style. If the precision is set to a value greater than the |
| maximum value for an ‘int’, it will be silently reduced down to |
| ‘INT_MAX’. |
| |
| If the ‘precision’ field is empty (as in ‘%Re’ or ‘%.RE’) with ‘conv’ |
| specifier ‘e’ and ‘E’, the number is displayed with enough digits so |
| that it can be read back exactly, assuming that the input and output |
| variables have the same precision and that the input and output rounding |
| modes are both rounding to nearest (as for ‘mpfr_get_str’). The default |
| precision for an empty ‘precision’ field with ‘conv’ specifiers ‘f’, |
| ‘F’, ‘g’, and ‘G’ is 6. |
| |
| 5.9.3 Functions |
| --------------- |
| |
| For all the following functions, if the number of characters which ought |
| to be written appears to exceed the maximum limit for an ‘int’, nothing |
| is written in the stream (resp. to ‘stdout’, to BUF, to STR), the |
| function returns −1, sets the _erange_ flag, and (in POSIX system only) |
| ‘errno’ is set to ‘EOVERFLOW’. |
| |
| -- Function: int mpfr_fprintf (FILE *STREAM, const char *TEMPLATE, …) |
| -- Function: int mpfr_vfprintf (FILE *STREAM, const char *TEMPLATE, |
| va_list AP) |
| Print to the stream STREAM the optional arguments under the control |
| of the template string TEMPLATE. Return the number of characters |
| written or a negative value if an error occurred. |
| |
| -- Function: int mpfr_printf (const char *TEMPLATE, …) |
| -- Function: int mpfr_vprintf (const char *TEMPLATE, va_list AP) |
| Print to ‘stdout’ the optional arguments under the control of the |
| template string TEMPLATE. Return the number of characters written |
| or a negative value if an error occurred. |
| |
| -- Function: int mpfr_sprintf (char *BUF, const char *TEMPLATE, …) |
| -- Function: int mpfr_vsprintf (char *BUF, const char *TEMPLATE, |
| va_list AP) |
| Form a null-terminated string corresponding to the optional |
| arguments under the control of the template string TEMPLATE, and |
| print it in BUF. No overlap is permitted between BUF and the other |
| arguments. Return the number of characters written in the array |
| BUF _not counting_ the terminating null character or a negative |
| value if an error occurred. |
| |
| -- Function: int mpfr_snprintf (char *BUF, size_t N, const char |
| *TEMPLATE, …) |
| -- Function: int mpfr_vsnprintf (char *BUF, size_t N, const char |
| *TEMPLATE, va_list AP) |
| Form a null-terminated string corresponding to the optional |
| arguments under the control of the template string TEMPLATE, and |
| print it in BUF. If N is zero, nothing is written and BUF may be a |
| null pointer, otherwise, the N−1 first characters are written in |
| BUF and the N-th is a null character. Return the number of |
| characters that would have been written had N be sufficiently |
| large, _not counting_ the terminating null character, or a negative |
| value if an error occurred. |
| |
| -- Function: int mpfr_asprintf (char **STR, const char *TEMPLATE, …) |
| -- Function: int mpfr_vasprintf (char **STR, const char *TEMPLATE, |
| va_list AP) |
| Write their output as a null terminated string in a block of memory |
| allocated using the current allocation function. A pointer to the |
| block is stored in STR. The block of memory must be freed using |
| ‘mpfr_free_str’. The return value is the number of characters |
| written in the string, excluding the null-terminator, or a negative |
| value if an error occurred. |
| |
| |
| File: mpfr.info, Node: Integer Related Functions, Next: Rounding Related Functions, Prev: Formatted Output Functions, Up: MPFR Interface |
| |
| 5.10 Integer and Remainder Related Functions |
| ============================================ |
| |
| -- Function: int mpfr_rint (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_ceil (mpfr_t ROP, mpfr_t OP) |
| -- Function: int mpfr_floor (mpfr_t ROP, mpfr_t OP) |
| -- Function: int mpfr_round (mpfr_t ROP, mpfr_t OP) |
| -- Function: int mpfr_trunc (mpfr_t ROP, mpfr_t OP) |
| Set ROP to OP rounded to an integer. ‘mpfr_rint’ rounds to the |
| nearest representable integer in the given direction RND, |
| ‘mpfr_ceil’ rounds to the next higher or equal representable |
| integer, ‘mpfr_floor’ to the next lower or equal representable |
| integer, ‘mpfr_round’ to the nearest representable integer, |
| rounding halfway cases away from zero (as in the roundTiesToAway |
| mode of IEEE 754-2008), and ‘mpfr_trunc’ to the next representable |
| integer toward zero. |
| |
| The returned value is zero when the result is exact, positive when |
| it is greater than the original value of OP, and negative when it |
| is smaller. More precisely, the returned value is 0 when OP is an |
| integer representable in ROP, 1 or −1 when OP is an integer that is |
| not representable in ROP, 2 or −2 when OP is not an integer. |
| |
| When OP is NaN, the NaN flag is set as usual. In the other cases, |
| the inexact flag is set when ROP differs from OP, following the ISO |
| C99 rule for the ‘rint’ function. If you want the behavior to be |
| more like IEEE 754 / ISO TS 18661-1, i.e., the usual behavior where |
| the round-to-integer function is regarded as any other mathematical |
| function, you should use one the ‘mpfr_rint_*’ functions instead |
| (however it is not possible to round to nearest with the even |
| rounding rule yet). |
| |
| Note that ‘mpfr_round’ is different from ‘mpfr_rint’ called with |
| the rounding to nearest mode (where halfway cases are rounded to an |
| even integer or significand). Note also that no double rounding is |
| performed; for instance, 10.5 (1010.1 in binary) is rounded by |
| ‘mpfr_rint’ with rounding to nearest to 12 (1100 in binary) in |
| 2-bit precision, because the two enclosing numbers representable on |
| two bits are 8 and 12, and the closest is 12. (If one first |
| rounded to an integer, one would round 10.5 to 10 with even |
| rounding, and then 10 would be rounded to 8 again with even |
| rounding.) |
| |
| -- Function: int mpfr_rint_ceil (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| -- Function: int mpfr_rint_floor (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t |
| RND) |
| -- Function: int mpfr_rint_round (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t |
| RND) |
| -- Function: int mpfr_rint_trunc (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t |
| RND) |
| Set ROP to OP rounded to an integer. ‘mpfr_rint_ceil’ rounds to |
| the next higher or equal integer, ‘mpfr_rint_floor’ to the next |
| lower or equal integer, ‘mpfr_rint_round’ to the nearest integer, |
| rounding halfway cases away from zero, and ‘mpfr_rint_trunc’ to the |
| next integer toward zero. If the result is not representable, it |
| is rounded in the direction RND. The returned value is the ternary |
| value associated with the considered round-to-integer function |
| (regarded in the same way as any other mathematical function). |
| |
| Contrary to ‘mpfr_rint’, those functions do perform a double |
| rounding: first OP is rounded to the nearest integer in the |
| direction given by the function name, then this nearest integer (if |
| not representable) is rounded in the given direction RND. Thus |
| these round-to-integer functions behave more like the other |
| mathematical functions, i.e., the returned result is the correct |
| rounding of the exact result of the function in the real numbers. |
| |
| For example, ‘mpfr_rint_round’ with rounding to nearest and a |
| precision of two bits rounds 6.5 to 7 (halfway cases away from |
| zero), then 7 is rounded to 8 by the round-even rule, despite the |
| fact that 6 is also representable on two bits, and is closer to 6.5 |
| than 8. |
| |
| -- Function: int mpfr_frac (mpfr_t ROP, mpfr_t OP, mpfr_rnd_t RND) |
| Set ROP to the fractional part of OP, having the same sign as OP, |
| rounded in the direction RND (unlike in ‘mpfr_rint’, RND affects |
| only how the exact fractional part is rounded, not how the |
| fractional part is generated). |
| |
| -- Function: int mpfr_modf (mpfr_t IOP, mpfr_t FOP, mpfr_t OP, |
| mpfr_rnd_t RND) |
| Set simultaneously IOP to the integral part of OP and FOP to the |
| fractional part of OP, rounded in the direction RND with the |
| corresponding precision of IOP and FOP (equivalent to |
| ‘mpfr_trunc(IOP, OP, RND)’ and ‘mpfr_frac(FOP, OP, RND)’). The |
| variables IOP and FOP must be different. Return 0 iff both results |
| are exact (see ‘mpfr_sin_cos’ for a more detailed description of |
| the return value). |
| |
| -- Function: int mpfr_fmod (mpfr_t R, mpfr_t X, mpfr_t Y, mpfr_rnd_t |
| RND) |
| -- Function: int mpfr_remainder (mpfr_t R, mpfr_t X, mpfr_t Y, |
| mpfr_rnd_t RND) |
| -- Function: int mpfr_remquo (mpfr_t R, long* Q, mpfr_t X, mpfr_t Y, |
| mpfr_rnd_t RND) |
| Set R to the value of X - NY, rounded according to the direction |
| RND, where N is the integer quotient of X divided by Y, defined as |
| follows: N is rounded toward zero for ‘mpfr_fmod’, and to the |
| nearest integer (ties rounded to even) for ‘mpfr_remainder’ and |
| ‘mpfr_remquo’. |
| |
| Special values are handled as described in Section F.9.7.1 of the |
| ISO C99 standard: If X is infinite or Y is zero, R is NaN. If Y is |
| infinite and X is finite, R is X rounded to the precision of R. If |
| R is zero, it has the sign of X. The return value is the ternary |
| value corresponding to R. |
| |
| Additionally, ‘mpfr_remquo’ stores the low significant bits from |
| the quotient N in *Q (more precisely the number of bits in a ‘long’ |
| minus one), with the sign of X divided by Y (except if those low |
| bits are all zero, in which case zero is returned). Note that X |
| may be so large in magnitude relative to Y that an exact |
| representation of the quotient is not practical. The |
| ‘mpfr_remainder’ and ‘mpfr_remquo’ functions are useful for |
| additive argument reduction. |
| |
| -- Function: int mpfr_integer_p (mpfr_t OP) |
| Return non-zero iff OP is an integer. |
| |
| |
| File: mpfr.info, Node: Rounding Related Functions, Next: Miscellaneous Functions, Prev: Integer Related Functions, Up: MPFR Interface |
| |
| 5.11 Rounding Related Functions |
| =============================== |
| |
| -- Function: void mpfr_set_default_rounding_mode (mpfr_rnd_t RND) |
| Set the default rounding mode to RND. The default rounding mode is |
| to nearest initially. |
| |
| -- Function: mpfr_rnd_t mpfr_get_default_rounding_mode (void) |
| Get the default rounding mode. |
| |
| -- Function: int mpfr_prec_round (mpfr_t X, mpfr_prec_t PREC, |
| mpfr_rnd_t RND) |
| Round X according to RND with precision PREC, which must be an |
| integer between ‘MPFR_PREC_MIN’ and ‘MPFR_PREC_MAX’ (otherwise the |
| behavior is undefined). If PREC is greater or equal to the |
| precision of X, then new space is allocated for the significand, |
| and it is filled with zeros. Otherwise, the significand is rounded |
| to precision PREC with the given direction. In both cases, the |
| precision of X is changed to PREC. |
| |
| Here is an example of how to use ‘mpfr_prec_round’ to implement |
| Newton’s algorithm to compute the inverse of A, assuming X is |
| already an approximation to N bits: |
| mpfr_set_prec (t, 2 * n); |
| mpfr_set (t, a, MPFR_RNDN); /* round a to 2n bits */ |
| mpfr_mul (t, t, x, MPFR_RNDN); /* t is correct to 2n bits */ |
| mpfr_ui_sub (t, 1, t, MPFR_RNDN); /* high n bits cancel with 1 */ |
| mpfr_prec_round (t, n, MPFR_RNDN); /* t is correct to n bits */ |
| mpfr_mul (t, t, x, MPFR_RNDN); /* t is correct to n bits */ |
| mpfr_prec_round (x, 2 * n, MPFR_RNDN); /* exact */ |
| mpfr_add (x, x, t, MPFR_RNDN); /* x is correct to 2n bits */ |
| |
| Warning! You must not use this function if X was initialized with |
| ‘MPFR_DECL_INIT’ or with ‘mpfr_custom_init_set’ (*note Custom |
| Interface::). |
| |
| -- Function: int mpfr_can_round (mpfr_t B, mpfr_exp_t ERR, mpfr_rnd_t |
| RND1, mpfr_rnd_t RND2, mpfr_prec_t PREC) |
| Assuming B is an approximation of an unknown number X in the |
| direction RND1 with error at most two to the power E(b)-ERR where |
| E(b) is the exponent of B, return a non-zero value if one is able |
| to round correctly X to precision PREC with the direction RND2, and |
| 0 otherwise (including for NaN and Inf). This function *does not |
| modify* its arguments. |
| |
| If RND1 is ‘MPFR_RNDN’, then the sign of the error is unknown, but |
| its absolute value is the same, so that the possible range is twice |
| as large as with a directed rounding for RND1. |
| |
| Note: if one wants to also determine the correct *note ternary |
| value:: when rounding B to precision PREC with rounding mode RND, a |
| useful trick is the following: |
| if (mpfr_can_round (b, err, MPFR_RNDN, MPFR_RNDZ, |
| prec + (rnd == MPFR_RNDN))) |
| ... |
| Indeed, if RND is ‘MPFR_RNDN’, this will check if one can round to |
| PREC+1 bits with a directed rounding: if so, one can surely round |
| to nearest to PREC bits, and in addition one can determine the |
| correct ternary value, which would not be the case when B is near |
| from a value exactly representable on PREC bits. |
| |
| -- Function: mpfr_prec_t mpfr_min_prec (mpfr_t X) |
| Return the minimal number of bits required to store the significand |
| of X, and 0 for special values, including 0. (Warning: the |
| returned value can be less than ‘MPFR_PREC_MIN’.) |
| |
| The function name is subject to change. |
| |
| -- Function: const char * mpfr_print_rnd_mode (mpfr_rnd_t RND) |
| Return a string ("MPFR_RNDD", "MPFR_RNDU", "MPFR_RNDN", |
| "MPFR_RNDZ", "MPFR_RNDA") corresponding to the rounding mode RND, |
| or a null pointer if RND is an invalid rounding mode. |
| |
| |
| File: mpfr.info, Node: Miscellaneous Functions, Next: Exception Related Functions, Prev: Rounding Related Functions, Up: MPFR Interface |
| |
| 5.12 Miscellaneous Functions |
| ============================ |
| |
| -- Function: void mpfr_nexttoward (mpfr_t X, mpfr_t Y) |
| If X or Y is NaN, set X to NaN. If X and Y are equal, X is |
| unchanged. Otherwise, if X is different from Y, replace X by the |
| next floating-point number (with the precision of X and the current |
| exponent range) in the direction of Y (the infinite values are seen |
| as the smallest and largest floating-point numbers). If the result |
| is |