Aligning world configuration with vendor-build configuration
[dragonfly.git] / contrib / mpfr / mpfr.texi
1 \input texinfo    @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename mpfr.info
4 @documentencoding ISO-8859-1
5 @set VERSION 2.4.2
6 @set UPDATED-MONTH November 2009
7 @settitle GNU MPFR @value{VERSION}
8 @synindex tp fn
9 @iftex
10 @afourpaper
11 @end iftex
12 @comment %**end of header
13
14 @copying
15 This manual documents how to install and use the Multiple Precision
16 Floating-Point Reliable Library, version @value{VERSION}.
17
18 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
19
20 Permission is granted to copy, distribute and/or modify this document under
21 the terms of the GNU Free Documentation License, Version 1.2 or any later
22 version published by the Free Software Foundation; with no Invariant Sections,
23 with no Front-Cover Texts, and with no Back-Cover Texts.  A copy of the
24 license is included in @ref{GNU Free Documentation License}.
25 @end copying
26
27
28 @c  Texinfo version 4.2 or up will be needed to process this file.
29 @c
30 @c  A suitable texinfo.tex is supplied, a newer one should work
31 @c  equally well.
32 @c
33 @c  The edition number is in the VERSION variable above and should be
34 @c  updated where appropriate.  Also, update the month and year in
35 @c  UPDATED-MONTH.
36
37
38 @dircategory Software libraries
39 @direntry
40 * mpfr: (mpfr).                 Multiple Precision Floating-Point Reliable Library.
41 @end direntry
42
43 @c  html <meta name=description content="...">
44 @documentdescription
45 How to install and use GNU MPFR, a library for reliable multiple precision
46 floating-point arithmetic, version @value{VERSION}.
47 @end documentdescription
48
49 @c smallbook
50 @finalout
51 @setchapternewpage on
52
53 @ifnottex
54 @node Top, Copying, (dir), (dir)
55 @top GNU MPFR
56 @end ifnottex
57
58 @iftex
59 @titlepage
60 @title GNU MPFR
61 @subtitle The Multiple Precision Floating-Point Reliable Library
62 @subtitle Edition @value{VERSION}
63 @subtitle @value{UPDATED-MONTH}
64
65 @author The MPFR team
66 @email{mpfr@@loria.fr}
67
68 @c Include the Distribution inside the titlepage so
69 @c that headings are turned off.
70
71 @tex
72 \global\parindent=0pt
73 \global\parskip=8pt
74 \global\baselineskip=13pt
75 @end tex
76
77 @page
78 @vskip 0pt plus 1filll
79 @end iftex
80
81 @insertcopying
82 @ifnottex
83 @sp 1
84 @end ifnottex
85
86 @iftex
87 @end titlepage
88 @headings double
89 @end iftex
90
91 @c  Don't bother with contents for html, the menus seem adequate.
92 @ifnothtml
93 @contents
94 @end ifnothtml
95
96 @menu
97 * Copying::                     MPFR Copying Conditions (LGPL).
98 * Introduction to MPFR::        Brief introduction to GNU MPFR.
99 * Installing MPFR::             How to configure and compile the MPFR library.
100 * Reporting Bugs::              How to usefully report bugs.
101 * MPFR Basics::                 What every MPFR user should now.
102 * MPFR Interface::              MPFR functions and macros.
103 * Contributors::
104 * References::
105 * GNU Free Documentation License::
106 * Concept Index::
107 * Function Index::
108 @end menu
109
110
111 @c  @m{T,N} is $T$ in tex or @math{N} otherwise.  This is an easy way to give
112 @c  different forms for math in tex and info.  Commas in N or T don't work,
113 @c  but @C{} can be used instead.  \, works in info but not in tex.
114 @iftex
115 @macro m {T,N}
116 @tex$\T\$@end tex
117 @end macro
118 @end iftex
119 @ifnottex
120 @macro m {T,N}
121 @math{\N\}
122 @end macro
123 @end ifnottex
124
125 @c  Usage: @GMPabs{x}
126 @c  Give either |x| in tex, or abs(x) in info or html.
127 @tex
128 \gdef\GMPabs#1{|#1|}
129 @end tex
130 @ifnottex
131 @macro GMPabs {X}
132 @abs{}(\X\)
133 @end macro
134 @end ifnottex
135
136 @c  Usage: @GMPtimes{}
137 @c  Give either \times or the word "times".
138 @tex
139 \gdef\GMPtimes{\times}
140 @end tex
141 @ifnottex
142 @macro GMPtimes
143 times
144 @end macro
145 @end ifnottex
146
147 @c  New math operators.
148 @c  @abs{} can be used in both tex and info, or just \abs in tex.
149 @tex
150 \gdef\abs{\mathop{\rm abs}}
151 @end tex
152 @ifnottex
153 @macro abs
154 abs
155 @end macro
156 @end ifnottex
157
158 @c  @times{} made available as a "*" in info and html (already works in tex).
159 @ifnottex
160 @macro times
161 *
162 @end macro
163 @end ifnottex
164
165 @c  Math operators already available in tex, made available in info too.
166 @c  For example @log{} can be used in both tex and info.
167 @ifnottex
168 @macro le
169 <=
170 @end macro
171 @macro ge
172 >=
173 @end macro
174 @macro ne
175 <>
176 @end macro
177 @macro log
178 log
179 @end macro
180 @end ifnottex
181
182 @c  @pom{} definition
183 @tex
184 \gdef\pom{\ifmmode\pm\else$\pm$\fi}
185 @end tex
186 @ifnottex
187 @macro pom
188 ±
189 @end macro
190 @end ifnottex
191
192 @c The following macro have been copied from gmp.texi
193 @c
194 @c  Usage: @MPFRpxreftop{info,title}
195 @c
196 @c  Like @pxref{}, but designed for a reference to the top of a document, not
197 @c  a particular section.
198 @c
199 @c  The texinfo manual recommends putting a likely section name in references
200 @c  like this, eg. "Introduction", but it seems better to just give the title.
201 @c
202 @iftex
203 @macro MPFRpxreftop{info,title}
204 see @cite{\title\}.
205 @end macro
206 @end iftex
207 @ifhtml
208 @macro MPFRpxreftop{info,title}
209 see @cite{\title\}.
210 @end macro
211 @end ifhtml
212 @ifnottex
213 @ifnothtml
214 @macro MPFRpxreftop{info,title}
215 @pxref{Top,\title\,\title\,\info\,\title\}
216 @end macro
217 @end ifnothtml
218 @end ifnottex
219
220 @node Copying, Introduction to MPFR, Top, Top
221 @comment  node-name, next, previous,  up
222 @unnumbered MPFR Copying Conditions
223 @cindex Copying conditions
224 @cindex Conditions for copying MPFR
225
226 This library is @dfn{free}; this means that everyone is free to use it and
227 free to redistribute it on a free basis.  The library is not in the public
228 domain; it is copyrighted and there are restrictions on its distribution, but
229 these restrictions are designed to permit everything that a good cooperating
230 citizen would want to do.  What is not allowed is to try to prevent others
231 from further sharing any version of this library that they might get from
232 you.@refill
233
234 Specifically, we want to make sure that you have the right to give away copies
235 of the library, that you receive source code or else can get it if you want
236 it, that you can change this library or use pieces of it in new free programs,
237 and that you know you can do these things.@refill
238
239 To make sure that everyone has such rights, we have to forbid you to deprive
240 anyone else of these rights.  For example, if you distribute copies of the
241 GNU MPFR library, you must give the recipients all the rights that you have.
242 You must make sure that they, too, receive or can get the source code.  And you
243 must tell them their rights.@refill
244
245 Also, for our own protection, we must make certain that everyone finds out
246 that there is no warranty for the GNU MPFR library.  If it is modified by
247 someone else and passed on, we want their recipients to know that what they
248 have is not what we distributed, so that any problems introduced by others
249 will not reflect on our reputation.@refill
250
251 The precise conditions of the license for the GNU MPFR library are found in the
252 Lesser General Public License that accompanies the source code.
253 See the file COPYING.LIB.@refill
254
255 @node Introduction to MPFR, Installing MPFR, Copying, Top
256 @comment  node-name,  next,  previous,  up
257 @chapter Introduction to MPFR
258
259
260 MPFR is a portable library written in C for arbitrary precision arithmetic
261 on floating-point numbers. It is based on the GNU MP library.
262 It aims to extend the class of floating-point numbers provided by the
263 GNU MP library by a precise semantics. The main differences
264 with the @code{mpf} class from GNU MP are:
265
266 @itemize @bullet
267 @item the MPFR code is portable, i.e.@: the result of any operation
268 does not depend (or should not) on the machine word size
269 @code{mp_bits_per_limb} (32 or 64 on most machines);
270 @item the precision in bits can be set exactly to any valid value
271 for each variable (including very small precision);
272 @item MPFR provides the four rounding modes from the IEEE 754-1985
273 standard.
274 @end itemize
275
276 In particular, with a precision of 53 bits, MPFR should be able to
277 exactly reproduce all computations with double-precision machine
278 floating-point numbers (e.g., @code{double} type in C, with a C
279 implementation that rigorously follows Annex F of the ISO C99 standard
280 and @code{FP_CONTRACT} pragma set to @code{OFF}) on the four arithmetic
281 operations and the square root, except the default exponent range is much
282 wider and subnormal numbers are not implemented (but can be emulated).
283
284 This version of MPFR is released under the GNU Lesser General Public
285 License, Version 2.1 or any later version.
286 It is permitted to link MPFR to most non-free programs, as long as when
287 distributing them the MPFR source code and a means to re-link with a
288 modified MPFR library is provided.
289
290 @section How to Use This Manual
291
292 Everyone should read @ref{MPFR Basics}.  If you need to install the library
293 yourself, you need to read @ref{Installing MPFR}, too.
294
295 The rest of the manual can be used for later reference, although it is
296 probably a good idea to glance through it.
297
298 @node Installing MPFR, Reporting Bugs, Introduction to MPFR, Top
299 @comment  node-name,  next,  previous,  up
300 @chapter Installing MPFR
301 @cindex Installation
302
303 @section How to Install
304
305 Here are the steps needed to install the library on Unix systems
306 (more details are provided in the @file{INSTALL} file):
307
308 @enumerate
309 @item
310 To build MPFR, you first have to install GNU MP
311 (version 4.1 or higher) on your computer.
312 You need a C compiler, preferably GCC, but any reasonable compiler should
313 work.  And you need a standard Unix @samp{make} program, plus some other
314 standard Unix utility programs.
315
316 Then, in the MPFR build directory, type the following commands.
317
318 @item
319 @samp{./configure}
320
321 This will prepare the build and setup the options according to your system.
322 You can give options to specify the install directories (instead of
323 the default @file{/usr/local}), threading support, and so on. See
324 the @file{INSTALL} file and/or the output of @samp{./configure --help}
325 for more information, in particular if you get error messages.
326
327 @item
328 @samp{make}
329
330 This will compile MPFR, and create a library archive file @file{libmpfr.a}.
331 On most platforms, a dynamic library will be produced too (see configure).
332
333 @item
334 @samp{make check}
335
336 This will make sure MPFR was built correctly.
337 If you get error messages, please
338 report this to @samp{mpfr@@loria.fr}.  (@xref{Reporting Bugs}, for
339 information on what to include in useful bug reports.)
340
341 @item
342 @samp{make install}
343
344 This will copy the files @file{mpfr.h} and @file{mpf2mpfr.h} to the directory
345 @file{/usr/local/include}, the library files (@file{libmpfr.a} and possibly
346 others) to the directory @file{/usr/local/lib}, the file @file{mpfr.info}
347 to the directory @file{/usr/local/share/info}, and some other documentation
348 files to the directory @file{/usr/local/share/doc/mpfr} (or if you passed the
349 @samp{--prefix} option to @file{configure}, using the prefix directory given
350 as argument to @samp{--prefix} instead of @file{/usr/local}).
351 @end enumerate
352
353 @section Other `make' Targets
354
355 There are some other useful make targets:
356
357 @itemize @bullet
358 @item
359 @samp{mpfr.info} or @samp{info}
360
361 Create or update an info version of the manual, in @file{mpfr.info}.
362
363 This file is already provided in the MPFR archives.
364
365 @item
366 @samp{mpfr.pdf} or @samp{pdf}
367
368 Create a PDF version of the manual, in @file{mpfr.pdf}.
369
370 @item
371 @samp{mpfr.dvi} or @samp{dvi}
372
373 Create a DVI version of the manual, in @file{mpfr.dvi}.
374
375 @item
376 @samp{mpfr.ps} or @samp{ps}
377
378 Create a Postscript version of the manual, in @file{mpfr.ps}.
379
380 @item
381 @samp{mpfr.html} or @samp{html}
382
383 Create a HTML version of the manual, in several pages in the directory
384 @file{mpfr.html}; if you want only one output HTML file, then type
385 @samp{makeinfo --html --no-split mpfr.texi} instead.
386
387 @item
388 @samp{clean}
389
390 Delete all object files and archive files, but not the configuration files.
391
392 @item
393 @samp{distclean}
394
395 Delete all generated files not included in the distribution.
396
397 @item
398 @samp{uninstall}
399
400 Delete all files copied by @samp{make install}.
401 @end itemize
402
403
404 @section Build Problems
405
406 In case of problem, please read the @file{INSTALL} file carefully
407 before reporting a bug, in particular section ``In case of problem''.
408 Some problems are due to bad configuration on the user side (not
409 specific to MPFR). Problems are also mentioned in the FAQ
410 @url{http://www.mpfr.org/faq.html}.
411
412 @comment Warning! Do not split "MPFR ... @url{...}" across several lines
413 @comment as this needs to be updated with update-version.
414 Please report problems to @samp{mpfr@@loria.fr}.
415 @xref{Reporting Bugs}.
416 Some bug fixes are available on the
417 MPFR 2.4.2 web page @url{http://www.mpfr.org/mpfr-2.4.2/}.
418
419 @section Getting the Latest Version of MPFR
420
421 The latest version of MPFR is available from
422 @url{ftp://ftp.gnu.org/gnu/mpfr/} or @url{http://www.mpfr.org/}.
423
424 @node Reporting Bugs, MPFR Basics, Installing MPFR, Top
425 @comment  node-name,  next,  previous,  up
426 @chapter Reporting Bugs
427 @cindex Reporting bugs
428
429 @comment Warning! Do not split "MPFR ... @url{...}" across several lines
430 @comment as this needs to be updated with update-version.
431 If you think you have found a bug in the MPFR library, first have a look
432 on the MPFR 2.4.2 web page @url{http://www.mpfr.org/mpfr-2.4.2/} and the
433 FAQ @url{http://www.mpfr.org/faq.html}:
434 perhaps this bug is already known, in which case you may find there
435 a workaround for it. Otherwise, please investigate and report it.
436 We have made this library available to you, and it is not to ask too
437 much from you, to ask you to report the bugs that you find.
438
439 There are a few things you should think about when you put your bug report
440 together.
441
442 You have to send us a test case that makes it possible for us to reproduce the
443 bug.  Include instructions on how to run the test case.
444
445 You also have to explain what is wrong; if you get a crash, or if the results
446 printed are incorrect and in that case, in what way.
447
448 Please include compiler version information in your bug report. This can
449 be extracted using @samp{cc -V} on some machines, or, if you're using gcc,
450 @samp{gcc -v}. Also, include the output from @samp{uname -a} and the MPFR
451 version (the GMP version may be useful too).
452
453 If your bug report is good, we will do our best to help you to get a corrected
454 version of the library; if the bug report is poor, we will not do anything
455 about it (aside of chiding you to send better bug reports).
456
457 Send your bug report to: @samp{mpfr@@loria.fr}.
458
459 If you think something in this manual is unclear, or downright incorrect, or if
460 the language needs to be improved, please send a note to the same address.
461
462 @node MPFR Basics, MPFR Interface, Reporting Bugs, Top
463 @comment  node-name,  next,  previous,  up
464 @chapter MPFR Basics
465
466 @section Headers and Libraries
467
468 @cindex @file{mpfr.h}
469 All declarations needed to use MPFR are collected in the include file
470 @file{mpfr.h}.  It is designed to work with both C and C++ compilers.
471 You should include that file in any program using the MPFR library:
472
473 @example
474 #include <mpfr.h>
475 @end example
476
477 @cindex @code{stdio.h}
478 Note however that prototypes for MPFR functions with @code{FILE *} parameters
479 are provided only if @code{<stdio.h>} is included too (before @file{mpfr.h}).
480
481 @example
482 #include <stdio.h>
483 #include <mpfr.h>
484 @end example
485
486 @cindex @code{stdarg.h}
487 Likewise @code{<stdarg.h>} (or @code{<varargs.h>}) is required for prototypes
488 with @code{va_list} parameters, such as @code{mpfr_vprintf}.
489
490 @cindex @code{stdint.h}
491 @cindex @code{inttypes.h}
492 @cindex @code{intmax_t}
493 @cindex @code{uintmax_t}
494 And for any functions using @code{intmax_t}, you must include
495 @code{<stdint.h>} or @code{<inttypes.h>} before @file{mpfr.h}, to
496 allow @file{mpfr.h} to define prototypes for these functions. Moreover,
497 users of C++ compilers under some platforms may need to define the
498 @code{__STDC_CONSTANT_MACROS} macro (before @code{<stdint.h>} or
499 @code{<inttypes.h>} has been included) or @code{MPFR_USE_INTMAX_T}
500 (before @file{mpfr.h} has been included), at least for portability;
501 of course, it is possible to do that on the command line, e.g.,
502 with @code{-DMPFR_USE_INTMAX_T}.
503
504 You can avoid the use of MPFR macros encapsulating functions by defining
505 the @samp{MPFR_USE_NO_MACRO} macro before @file{mpfr.h} is included.  In
506 general this should not be necessary, but this can be useful when debugging
507 user code: with some macros, the compiler may emit spurious warnings with
508 some warning options, and macros can prevent some prototype checking.
509
510 @cindex Libraries
511 @cindex Linking
512 @cindex @code{libmpfr}
513 All programs using MPFR must link against both @file{libmpfr} and
514 @file{libgmp} libraries.  On a typical Unix-like system this can be
515 done with @samp{-lmpfr -lgmp} (in that order), for example
516
517 @example
518 gcc myprogram.c -lmpfr -lgmp
519 @end example
520
521 @cindex Libtool
522 MPFR is built using Libtool and an application can use that to link if
523 desired, @MPFRpxreftop{libtool.info, GNU Libtool}
524 @c Note: the .info extension has been added to avoid the following bug:
525 @c   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=484740
526 @c which occurs when reading the info file from the build directory:
527 @c   info ./mpfr    or    info -f ./mpfr.info
528 @c Due to a poor design, the "info" utility will not find the correct
529 @c libtool info file if the .info extension is not provided, because of
530 @c the "libtool" script in MPFR's directory!
531
532 If MPFR has been installed to a non-standard location, then it may be
533 necessary to set up environment variables such as @samp{C_INCLUDE_PATH}
534 and @samp{LIBRARY_PATH}, or use @samp{-I} and @samp{-L} compiler options,
535 in order to point to the right directories. For a shared library, it may
536 also be necessary to set up some sort of run-time library path (e.g.,
537 @samp{LD_LIBRARY_PATH}) on some systems. Please read the @file{INSTALL}
538 file for additional information.
539
540 @section Nomenclature and Types
541
542 @cindex Floating-point number
543 @tindex @code{mpfr_t}
544 @noindent
545 A @dfn{floating-point number} or @dfn{float} for short, is an arbitrary
546 precision significand (also called mantissa) with a limited precision
547 exponent. The C data type
548 for such objects is @code{mpfr_t} (internally defined as a one-element
549 array of a structure, and @code{mpfr_ptr} is the C data type representing
550 a pointer to this structure). A floating-point number can have
551 three special values: Not-a-Number (NaN) or plus or minus Infinity. NaN
552 represents an uninitialized object, the result of an invalid operation
553 (like 0 divided by 0), or a value that cannot be determined (like
554 +Infinity minus +Infinity). Moreover, like in the IEEE 754 standard,
555 zero is signed, i.e.@: there are both +0 and @minus{}0; the behavior
556 is the same as in the IEEE 754 standard and it is generalized to
557 the other functions supported by MPFR. Unless documented otherwise,
558 the sign bit of a NaN is unspecified.
559
560
561 @cindex Precision
562 @tindex @code{mp_prec_t}
563 @noindent
564 The @dfn{precision} is the number of bits used to represent the significand
565 of a floating-point number;
566 the corresponding C data type is @code{mp_prec_t}.
567 The precision can be any integer between @code{MPFR_PREC_MIN} and
568 @code{MPFR_PREC_MAX}. In the current implementation, @code{MPFR_PREC_MIN}
569 is equal to 2.
570
571 Warning! MPFR needs to increase the precision internally, in order to
572 provide accurate results (and in particular, correct rounding). Do not
573 attempt to set the precision to any value near @code{MPFR_PREC_MAX},
574 otherwise MPFR will abort due to an assertion failure. Moreover, you
575 may reach some memory limit on your platform, in which case the program
576 may abort, crash or have undefined behavior (depending on your C
577 implementation).
578
579 @cindex Rounding Modes
580 @tindex @code{mp_rnd_t}
581 @noindent
582 The @dfn{rounding mode} specifies the way to round the result of a
583 floating-point operation, in case the exact result can not be represented
584 exactly in the destination significand;
585 the corresponding C data type is @code{mp_rnd_t}.
586
587 @cindex Limb
588 @c @tindex @code{mp_limb_t}
589 @noindent
590 A @dfn{limb} means the part of a multi-precision number that fits in a single
591 word.  (We chose this word because a limb of the human body is analogous to a
592 digit, only larger, and containing several digits.)  Normally a limb contains
593 32 or 64 bits.  The C data type for a limb is @code{mp_limb_t}.
594
595 @section Function Classes
596
597 There is only one class of functions in the MPFR library:
598
599 @enumerate
600 @item
601 Functions for floating-point arithmetic, with names beginning with
602 @code{mpfr_}.  The associated type is @code{mpfr_t}.
603 @end enumerate
604
605
606 @section MPFR Variable Conventions
607
608 As a general rule, all MPFR functions expect output arguments before input
609 arguments.  This notation is based on an analogy with the assignment operator.
610
611 MPFR allows you to use the same variable for both input and output in the same
612 expression.  For example, the main function for floating-point multiplication,
613 @code{mpfr_mul}, can be used like this: @code{mpfr_mul (x, x, x, rnd_mode)}.
614 This
615 computes the square of @var{x} with rounding mode @code{rnd_mode}
616 and puts the result back in @var{x}.
617
618 Before you can assign to an MPFR variable, you need to initialize it by calling
619 one of the special initialization functions.  When you're done with a
620 variable, you need to clear it out, using one of the functions for that
621 purpose.
622
623 A variable should only be initialized once, or at least cleared out between
624 each initialization.  After a variable has been initialized, it may be
625 assigned to any number of times.
626
627 For efficiency reasons, avoid to initialize and clear out a variable in loops.
628 Instead, initialize it before entering the loop, and clear it out after the
629 loop has exited.
630
631 You do not need to be concerned about allocating additional space for MPFR
632 variables, since any variable has a significand of fixed size.
633 Hence unless you change its precision, or clear and reinitialize it,
634 a floating-point variable will have the same allocated space during all its
635 life.
636
637 @section Rounding Modes
638
639 The following four rounding modes are supported:
640
641 @itemize @bullet
642 @item @code{GMP_RNDN}: round to nearest (roundTiesToEven in IEEE 754-2008)
643 @item @code{GMP_RNDZ}: round toward zero (roundTowardZero in IEEE 754-2008)
644 @item @code{GMP_RNDU}: round toward plus infinity (roundTowardPositive in IEEE 754-2008)
645 @item @code{GMP_RNDD}: round toward minus infinity (roundTowardNegative in IEEE 754-2008)
646 @end itemize
647
648 The @samp{round to nearest} mode works as in the IEEE 754 standard: in
649 case the number to be rounded lies exactly in the middle of two representable
650 numbers, it is rounded to the one with the least significant bit set to zero.
651 For example, the number 5/2, which is represented by (10.1) in binary, is
652 rounded to (10.0)=2 with a precision of two bits, and not to (11.0)=3.
653 This rule avoids the @dfn{drift} phenomenon mentioned by Knuth in volume 2
654 of The Art of Computer Programming (Section 4.2.2).
655
656 Most MPFR functions take as first argument the destination variable, as
657 second and following arguments the input variables, as last argument a
658 rounding mode, and have a return value of type @code{int}, called the
659 @dfn{ternary value}. The value stored in the destination variable is
660 correctly rounded, i.e.@: MPFR behaves as if it computed the result with
661 an infinite precision, then rounded it to the precision of this variable.
662 The input variables are regarded as exact (in particular, their precision
663 does not affect the result).
664
665 As a consequence, in case of a non-zero real rounded result, the error
666 on the result is less or equal to 1/2 ulp (unit in the last place) of
667 the target in the rounding to nearest mode, and less than 1 ulp of the
668 target in the directed rounding modes (a ulp is the weight of the least
669 significant represented bit of the target after rounding).
670 @c Since subnormals are not supported, we must take into account the ulp of
671 @c the rounded result, not the one of the exact result, for full generality.
672
673 Unless documented otherwise, functions returning an @code{int} return
674 a ternary value.
675 If the ternary value is zero, it means that the value stored in the
676 destination variable is the exact result of the corresponding mathematical
677 function. If the ternary value is positive (resp.@: negative), it means
678 the value stored in the destination variable is greater (resp.@: lower)
679 than the exact result. For example with the @code{GMP_RNDU} rounding mode,
680 the ternary value is usually positive, except when the result is exact, in
681 which case it is zero. In the case of an infinite result, it is considered
682 as inexact when it was obtained by overflow, and exact otherwise. A NaN
683 result (Not-a-Number) always corresponds to an exact return value.
684 The opposite of a returned ternary value is guaranteed to be representable
685 in an @code{int}.
686
687 Unless documented otherwise, functions returning a @code{1}
688 (or any other value specified in this manual)
689 for special cases (like @code{acos(0)}) should return an overflow or
690 an underflow if @code{1} is not representable in the current exponent range.
691
692 @section Floating-Point Values on Special Numbers
693
694 This section specifies the floating-point values (of type @code{mpfr_t})
695 returned by MPFR functions. For functions returning several values (like
696 @code{mpfr_sin_cos}), the rules apply to each result separately.
697
698 Functions can have one or several input arguments. An input point is
699 a mapping from these input arguments to the set of the MPFR numbers.
700 When none of its components are NaN, an input point can also be seen
701 as a tuple in the extended real numbers (the set of the real numbers
702 with both infinities).
703
704 When the input point is in the domain of the mathematical function, the
705 result is rounded as described in Section ``Rounding Modes'' (but see
706 below for the specification of the sign of an exact zero). Otherwise
707 the general rules from this section apply unless stated otherwise in
708 the description of the MPFR function (@ref{MPFR Interface}).
709
710 When the input point is not in the domain of the mathematical function
711 but is in its closure in the extended real numbers and the function can
712 be extended by continuity, the result is the obtained limit.
713 Examples: @code{mpfr_hypot} on (+Inf,0) gives +Inf. But @code{mpfr_pow}
714 cannot be defined on (1,+Inf) using this rule, as one can find
715 sequences (@m{x_n,@var{x}_@var{n}},@m{y_n,@var{y}_@var{n}}) such that
716 @m{x_n,@var{x}_@var{n}} goes to 1, @m{y_n,@var{y}_@var{n}} goes to +Inf
717 and @m{(x_n)^{y_n},@var{x}_@var{n} to the @var{y}_@var{n}} goes to any
718 positive value when @var{n} goes to the infinity.
719
720 When the input point is in the closure of the domain of the mathematical
721 function and an input argument is +0 (resp.@: @minus{}0), one considers
722 the limit when the corresponding argument approaches 0 from above
723 (resp.@: below). If the limit is not defined (e.g., @code{mpfr_log} on
724 @minus{}0), the behavior must be specified in the description of the
725 MPFR function.
726
727 When the result is equal to 0, its sign is determined by considering the
728 limit as if the input point were not in the domain: If one approaches 0
729 from above (resp.@: below), the result is +0 (resp.@: @minus{}0). In the
730 other cases, the sign must be specified in the description of the MPFR
731 function. Example: @code{mpfr_sin} on +0 gives +0.
732
733 When the input point is not in the closure of the domain of the function,
734 the result is NaN. Example: @code{mpfr_sqrt} on @minus{}17 gives NaN.
735
736 When an input argument is NaN, the result is NaN, possibly except when
737 a partial function is constant on the finite floating-point numbers;
738 such a case is always explicitly specified in @ref{MPFR Interface}.
739 @c Said otherwise, if such a case is not specified, this is a bug, thus
740 @c we may change the returned value after documenting it without having
741 @c to change the libtool interface number (this would have more drawbacks
742 @c that advantages in practice), like for any bug fix.
743 Example: @code{mpfr_hypot} on (NaN,0) gives NaN, but @code{mpfr_hypot}
744 on (NaN,+Inf) gives +Inf (as specified in @ref{Special Functions}),
745 since for any finite input @var{x}, @code{mpfr_hypot} on (@var{x},+Inf)
746 gives +Inf.
747
748 @section Exceptions
749
750 MPFR supports 5 exception types:
751
752 @itemize @bullet
753
754 @item Underflow:
755 An underflow occurs when the exact result of a function is a non-zero
756 real number and the result obtained after the rounding, assuming an
757 unbounded exponent range (for the rounding), has an exponent smaller
758 than the minimum exponent of the current range. In the round-to-nearest
759 mode, the halfway case is rounded toward zero.
760
761 Note: This is not the single definition of the underflow. MPFR chooses
762 to consider the underflow after rounding. The underflow before rounding
763 can also be defined. For instance, consider a function that has the
764 exact result @m{7 \times 2^{e-4}, 7 multiplied by two to the power
765 @var{e}@minus{}4}, where @var{e} is the smallest exponent (for a
766 significand between 1/2 and 1) in the current
767 range, with a 2-bit target precision and rounding toward plus infinity.
768 The exact result has the exponent @var{e}@minus{}1. With the underflow
769 before rounding, such a function call would yield an underflow, as
770 @var{e}@minus{}1 is outside the current exponent range. However, MPFR
771 first considers the rounded result assuming an unbounded exponent range.
772 The exact result cannot be represented exactly in precision 2, and here,
773 it is rounded to @m{0.5 @times 2^e, 0.5 times 2 to @var{e}}, which is
774 representable in the current exponent range. As a consequence, this will
775 not yield an underflow in MPFR.
776
777 @item Overflow:
778 An overflow occurs when the exact result of a function is a non-zero
779 real number and the result obtained after the rounding, assuming an
780 unbounded exponent range (for the rounding), has an exponent larger
781 than the maximum exponent of the current range. In the round-to-nearest
782 mode, the result is infinite.
783
784 @item NaN:
785 A NaN exception occurs when the result of a function is a NaN.
786 @c NaN is defined above. So, we don't say anything more.
787
788 @item Inexact:
789 An inexact exception occurs when the result of a function cannot be
790 represented exactly and must be rounded.
791
792 @item Range error:
793 A range exception occurs when a function that does not return a MPFR
794 number (such as comparisons and conversions to an integer) has an
795 invalid result (e.g. an argument is NaN in @code{mpfr_cmp} or in a
796 conversion to an integer).
797
798 @end itemize
799
800 MPFR has a global flag for each exception, which can be cleared, set
801 or tested by functions described in @ref{Exception Related Functions}.
802
803 Differences with the ISO C99 standard:
804
805 @itemize @bullet
806
807 @item In C, only quiet NaNs are specified, and a NaN propagation does not
808 raise an invalid exception. Unless explicitly stated otherwise, MPFR sets
809 the NaN flag whenever a NaN is generated, even when a NaN is propagated
810 (e.g. in NaN + NaN), as if all NaNs were signaling.
811
812 @item An invalid exception in C corresponds to either a NaN exception or
813 a range error in MPFR.
814
815 @end itemize
816
817 @section Memory Handling
818
819 MPFR functions may create caches, e.g. when computing constants such
820 as @m{\pi,Pi}, either because the user has called a function like
821 @code{mpfr_const_pi} directly or because such a function was called
822 internally by the MPFR library itself to compute some other function.
823
824 At any time, the user can free the various caches with
825 @code{mpfr_free_cache}. It is strongly advised to do that before
826 terminating a thread, or before exiting when using tools like
827 @samp{valgrind} (to avoid memory leaks being reported).
828
829 MPFR internal data such as flags, the exponent range, the default
830 precision and rounding mode, and caches (i.e., data that are not
831 accessed via parameters) are either global (if MPFR has not been
832 compiled as thread safe) or per-thread (thread local storage).
833
834 @node MPFR Interface, Contributors, MPFR Basics, Top
835 @comment  node-name,  next,  previous,  up
836 @chapter MPFR Interface
837 @cindex Floating-point functions
838 @cindex Float functions
839
840 The floating-point functions expect arguments of type @code{mpfr_t}.
841
842 The MPFR floating-point functions have an interface that is similar to the
843 GNU MP
844 integer functions.  The function prefix for floating-point operations is
845 @code{mpfr_}.
846
847 There is one significant characteristic of floating-point numbers that has
848 motivated a difference between this function class and other GNU MP function
849 classes: the inherent inexactness of floating-point arithmetic.  The user has
850 to specify the precision for each variable.  A computation that assigns a
851 variable will take place with the precision of the assigned variable; the
852 cost of that computation should not depend from the
853 precision of variables used as input (on average).
854
855 @cindex Precision
856 The semantics of a calculation in MPFR is specified as follows: Compute the
857 requested operation exactly (with ``infinite accuracy''), and round the result
858 to the precision of the destination variable, with the given rounding mode.
859 The MPFR floating-point functions are intended to be a smooth extension
860 of the IEEE 754 arithmetic. The results obtained on one computer should
861 not differ from the results obtained on a computer with a different word size.
862
863 @cindex Accuracy
864 MPFR does not keep track of the accuracy of a computation. This is left
865 to the user or to a higher layer.
866 As a consequence, if two variables are used to store
867 only a few significant bits, and their product is stored in a variable with large
868 precision, then MPFR will still compute the result with full precision.
869
870 The value of the standard C macro @code{errno} may be set to non-zero by
871 any MPFR function or macro, whether or not there is an error.
872
873 @menu
874 * Initialization Functions::
875 * Assignment Functions::
876 * Combined Initialization and Assignment Functions::
877 * Conversion Functions::
878 * Basic Arithmetic Functions::
879 * Comparison Functions::
880 * Special Functions::
881 * Input and Output Functions::
882 * Formatted Output Functions::
883 * Integer Related Functions::
884 * Rounding Related Functions::
885 * Miscellaneous Functions::
886 * Exception Related Functions::
887 * Compatibility with MPF::
888 * Custom Interface::
889 * Internals::
890 @end menu
891
892 @node Initialization Functions, Assignment Functions, MPFR Interface, MPFR Interface
893 @comment  node-name,  next,  previous,  up
894 @cindex Initialization functions
895 @section Initialization Functions
896
897 An @code{mpfr_t} object must be initialized before storing the first value in
898 it.  The functions @code{mpfr_init} and @code{mpfr_init2} are used for that
899 purpose.
900
901 @deftypefun void mpfr_init2 (mpfr_t @var{x}, mp_prec_t @var{prec})
902 Initialize @var{x}, set its precision to be @strong{exactly}
903 @var{prec} bits and its value to NaN. (Warning: the corresponding
904 @code{mpf} functions initialize to zero instead.)
905
906 Normally, a variable should be initialized once only or at
907 least be cleared, using @code{mpfr_clear}, between initializations.
908 To change the precision of a variable which has already been initialized,
909 use @code{mpfr_set_prec}.
910 The precision @var{prec} must be an integer between @code{MPFR_PREC_MIN} and
911 @code{MPFR_PREC_MAX} (otherwise the behavior is undefined).
912 @end deftypefun
913
914 @deftypefun void mpfr_inits2 (mp_prec_t @var{prec}, mpfr_t @var{x}, ...)
915 Initialize all the @code{mpfr_t} variables of the given @code{va_list},
916 set their precision to be @strong{exactly}
917 @var{prec} bits and their value to NaN.
918 See @code{mpfr_init2} for more details.
919 The @code{va_list} is assumed to be composed only of type @code{mpfr_t}
920 (or equivalently @code{mpfr_ptr}).
921 It begins from @var{x}. It ends when it encounters a null pointer (whose
922 type must also be @code{mpfr_ptr}).
923 @end deftypefun
924
925 @deftypefun void mpfr_clear (mpfr_t @var{x})
926 Free the space occupied by @var{x}.  Make sure to call this function for all
927 @code{mpfr_t} variables when you are done with them.
928 @end deftypefun
929
930 @deftypefun void mpfr_clears (mpfr_t @var{x}, ...)
931 Free the space occupied by all the @code{mpfr_t} variables of the given
932 @code{va_list}. See @code{mpfr_clear} for more details.
933 The @code{va_list} is assumed to be composed only of type @code{mpfr_t}
934 (or equivalently @code{mpfr_ptr}).
935 It begins from @var{x}. It ends when it encounters a null pointer (whose
936 type must also be @code{mpfr_ptr}).
937 @end deftypefun
938
939 Here is an example of how to use multiple initialization functions:
940
941 @example
942 @{
943   mpfr_t x, y, z, t;
944   mpfr_inits2 (256, x, y, z, t, (mpfr_ptr) 0);
945   @dots{}
946   mpfr_clears (x, y, z, t, (mpfr_ptr) 0);
947 @}
948 @end example
949
950 @deftypefun void mpfr_init (mpfr_t @var{x})
951 Initialize @var{x} and set its value to NaN.
952
953 Normally, a variable should be initialized once only
954 or at least be cleared, using @code{mpfr_clear}, between initializations.  The
955 precision of @var{x} is the default precision, which can be changed
956 by a call to @code{mpfr_set_default_prec}.
957
958 Warning! In a given program, some other libraries might change the default
959 precision and not restore it. Thus it is safer to use @code{mpfr_init2}.
960 @end deftypefun
961
962 @deftypefun void mpfr_inits (mpfr_t @var{x}, ...)
963 Initialize all the @code{mpfr_t} variables of the given @code{va_list},
964 set their precision to be the default precision and their value to NaN.
965 See @code{mpfr_init} for more details.
966 The @code{va_list} is assumed to be composed only of type @code{mpfr_t}
967 (or equivalently @code{mpfr_ptr}).
968 It begins from @var{x}. It ends when it encounters a null pointer (whose
969 type must also be @code{mpfr_ptr}).
970
971 Warning! In a given program, some other libraries might change the default
972 precision and not restore it. Thus it is safer to use @code{mpfr_inits2}.
973 @end deftypefun
974
975 @defmac MPFR_DECL_INIT (@var{name}, @var{prec})
976 This macro declares @var{name} as an automatic variable of type @code{mpfr_t},
977 initializes it and sets its precision to be @strong{exactly} @var{prec} bits
978 and its value to NaN. @var{name} must be a valid identifier.
979 You must use this macro in the declaration section.
980 This macro is much faster than using @code{mpfr_init2} but has some
981 drawbacks:
982
983 @itemize @bullet
984 @item You @strong{must not} call @code{mpfr_clear} with variables
985 created with this macro (the storage is allocated at the point of declaration
986 and deallocated when the brace-level is exited).
987 @item You @strong{cannot} change their precision.
988 @item You @strong{should not} create variables with huge precision with this
989 macro.
990 @item Your compiler must support @samp{Non-Constant Initializers} (standard
991 in C++ and ISO C99) and @samp{Token Pasting}
992 (standard in ISO C89). If @var{prec} is not a constant expression, your
993 compiler must support @samp{variable-length automatic arrays} (standard
994 in ISO C99). @samp{GCC 2.95.3} and above supports all these features.
995 If you compile your program with gcc in c89 mode and with @samp{-pedantic},
996 you may want to define the @code{MPFR_USE_EXTENSION} macro to avoid warnings
997 due to the @code{MPFR_DECL_INIT} implementation.
998 @end itemize
999 @end defmac
1000
1001 @deftypefun void mpfr_set_default_prec (mp_prec_t @var{prec})
1002 Set the default precision to be @strong{exactly} @var{prec} bits.  The
1003 precision of a variable means the number of bits used to store its significand.
1004 All
1005 subsequent calls to @code{mpfr_init} will use this precision, but previously
1006 initialized variables are unaffected.
1007 This default precision is set to 53 bits initially.
1008 The precision can be any integer between @code{MPFR_PREC_MIN} and
1009 @code{MPFR_PREC_MAX}.
1010 @end deftypefun
1011
1012 @deftypefun mp_prec_t mpfr_get_default_prec (void)
1013 Return the default MPFR precision in bits.
1014 @end deftypefun
1015
1016 @need 2000
1017 Here is an example on how to initialize floating-point variables:
1018
1019 @example
1020 @{
1021   mpfr_t x, y;
1022   mpfr_init (x);                /* use default precision */
1023   mpfr_init2 (y, 256);          /* precision @emph{exactly} 256 bits */
1024   @dots{}
1025   /* When the program is about to exit, do ... */
1026   mpfr_clear (x);
1027   mpfr_clear (y);
1028   mpfr_free_cache ();
1029 @}
1030 @end example
1031
1032 The following functions are useful for changing the precision during a
1033 calculation.  A typical use would be for adjusting the precision gradually in
1034 iterative algorithms like Newton-Raphson, making the computation precision
1035 closely match the actual accurate part of the numbers.
1036
1037 @deftypefun void mpfr_set_prec (mpfr_t @var{x}, mp_prec_t @var{prec})
1038 Reset the precision of @var{x} to be @strong{exactly} @var{prec} bits,
1039 and set its value to NaN.
1040 The previous value stored in @var{x} is lost. It is equivalent to
1041 a call to @code{mpfr_clear(x)} followed by a call to
1042 @code{mpfr_init2(x, prec)}, but more efficient as no allocation is done in
1043 case the current allocated space for the significand of @var{x} is enough.
1044 The precision @var{prec} can be any integer between @code{MPFR_PREC_MIN} and
1045 @code{MPFR_PREC_MAX}.
1046
1047 In case you want to keep the previous value stored in @var{x},
1048 use @code{mpfr_prec_round} instead.
1049 @end deftypefun
1050
1051 @deftypefun mp_prec_t mpfr_get_prec (mpfr_t @var{x})
1052 Return the precision actually used for assignments of @var{x}, i.e.@: the
1053 number of bits used to store its significand.
1054 @end deftypefun
1055
1056 @node Assignment Functions, Combined Initialization and Assignment Functions, Initialization Functions, MPFR Interface
1057 @comment  node-name,  next,  previous,  up
1058 @cindex Assignment functions
1059 @section Assignment Functions
1060
1061 These functions assign new values to already initialized floats
1062 (@pxref{Initialization Functions}).
1063
1064 @deftypefun int mpfr_set (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1065 @deftypefunx int mpfr_set_ui (mpfr_t @var{rop}, unsigned long int @var{op}, mp_rnd_t @var{rnd})
1066 @deftypefunx int mpfr_set_si (mpfr_t @var{rop}, long int @var{op}, mp_rnd_t @var{rnd})
1067 @deftypefunx int mpfr_set_uj (mpfr_t @var{rop}, uintmax_t @var{op}, mp_rnd_t @var{rnd})
1068 @deftypefunx int mpfr_set_sj (mpfr_t @var{rop}, intmax_t @var{op}, mp_rnd_t @var{rnd})
1069 @deftypefunx int mpfr_set_d (mpfr_t @var{rop}, double @var{op}, mp_rnd_t @var{rnd})
1070 @deftypefunx int mpfr_set_ld (mpfr_t @var{rop}, long double @var{op}, mp_rnd_t @var{rnd})
1071 @deftypefunx int mpfr_set_decimal64 (mpfr_t @var{rop}, _Decimal64 @var{op}, mp_rnd_t @var{rnd})
1072 @deftypefunx int mpfr_set_z (mpfr_t @var{rop}, mpz_t @var{op}, mp_rnd_t @var{rnd})
1073 @deftypefunx int mpfr_set_q (mpfr_t @var{rop}, mpq_t @var{op}, mp_rnd_t @var{rnd})
1074 @deftypefunx int mpfr_set_f (mpfr_t @var{rop}, mpf_t @var{op}, mp_rnd_t @var{rnd})
1075 Set the value of @var{rop} from @var{op}, rounded
1076 toward the given direction @var{rnd}.
1077 Note that the input 0 is converted to +0 by @code{mpfr_set_ui},
1078 @code{mpfr_set_si}, @code{mpfr_set_sj}, @code{mpfr_set_uj},
1079 @code{mpfr_set_z}, @code{mpfr_set_q} and
1080 @code{mpfr_set_f}, regardless of the rounding mode.
1081 If the system does not support the IEEE 754 standard, @code{mpfr_set_d},
1082 @code{mpfr_set_ld} and
1083 @code{mpfr_set_decimal64} might not preserve the signed zeros.
1084 The @code{mpfr_set_decimal64} function is built only with the configure
1085 option @samp{--enable-decimal-float}, which also requires
1086 @samp{--with-gmp-build}, and when the compiler or
1087 system provides the @samp{_Decimal64} data type
1088 (GCC version 4.2.0 is known to support this data type,
1089 but only when configured with @samp{--enable-decimal-float} too).
1090 @code{mpfr_set_q} might not be able to work if the numerator (or the
1091 denominator) can not be representable as a @code{mpfr_t}.
1092
1093 Note: If you want to store a floating-point constant to a @code{mpfr_t},
1094 you should use @code{mpfr_set_str} (or one of the MPFR constant functions,
1095 such as @code{mpfr_const_pi} for @m{\pi,Pi}) instead of @code{mpfr_set_d},
1096 @code{mpfr_set_ld} or @code{mpfr_set_decimal64}.
1097 Otherwise the floating-point constant will be first
1098 converted into a reduced-precision (e.g., 53-bit) binary number before
1099 MPFR can work with it.
1100 @end deftypefun
1101
1102 @deftypefun int mpfr_set_ui_2exp (mpfr_t @var{rop}, unsigned long int @var{op}, mp_exp_t @var{e}, mp_rnd_t @var{rnd})
1103 @deftypefunx int mpfr_set_si_2exp (mpfr_t @var{rop}, long int @var{op}, mp_exp_t @var{e}, mp_rnd_t @var{rnd})
1104 @deftypefunx int mpfr_set_uj_2exp (mpfr_t @var{rop}, uintmax_t @var{op}, intmax_t @var{e}, mp_rnd_t @var{rnd})
1105 @deftypefunx int mpfr_set_sj_2exp (mpfr_t @var{rop}, intmax_t @var{op}, intmax_t @var{e}, mp_rnd_t @var{rnd})
1106 Set the value of @var{rop} from @m{@var{op} \times 2^e, @var{op} multiplied by
1107 two to the power @var{e}}, rounded toward the given direction @var{rnd}.
1108 Note that the input 0 is converted to +0.
1109 @end deftypefun
1110
1111 @deftypefun int mpfr_set_str (mpfr_t @var{rop}, const char *@var{s}, int @var{base}, mp_rnd_t @var{rnd})
1112 Set @var{rop} to the value of the string @var{s} in base @var{base},
1113 rounded in the direction @var{rnd}.
1114 See the documentation of @code{mpfr_strtofr} for a detailed description
1115 of the valid string formats.
1116 Contrary to @code{mpfr_strtofr}, @code{mpfr_set_str} requires the
1117 @emph{whole} string to represent a valid floating-point number.
1118 @c Additionally, special values
1119 @c @code{@@NaN@@}, @code{@@Inf@@}, @code{+@@Inf@@} and @code{-@@Inf@@},
1120 @c all case insensitive, without leading whitespace and possibly followed by
1121 @c other characters, are accepted too (it may change).
1122 This function returns 0 if the entire string up to the final null character
1123 is a valid number in base @var{base}; otherwise it returns @minus{}1, and
1124 @var{rop} may have changed.
1125 @end deftypefun
1126
1127 @deftypefun int mpfr_strtofr (mpfr_t @var{rop}, const char *@var{nptr}, char **@var{endptr}, int @var{base}, mp_rnd_t @var{rnd})
1128
1129 Read a floating-point number from a string @var{nptr} in base @var{base},
1130 rounded in the direction @var{rnd}; @var{base} must be either 0 (to
1131 detect the base, as described below) or a number from 2 to 36 (otherwise
1132 the behavior is undefined). If @var{nptr} starts with valid data, the
1133 result is stored in @var{rop} and @code{*@var{endptr}} points to the
1134 character just after the valid data (if @var{endptr} is not a null pointer);
1135 otherwise @var{rop} is set to zero and the value of @var{nptr} is stored
1136 in the location referenced by @var{endptr} (if @var{endptr} is not a null
1137 pointer). The usual ternary value is returned.
1138
1139 Parsing follows the standard C @code{strtod} function with some extensions.
1140 Case is ignored. After optional leading whitespace, one has a subject
1141 sequence consisting of an optional sign (@code{+} or @code{-}), and either
1142 numeric data or special data. The subject sequence is defined as the
1143 longest initial subsequence of the input string, starting with the first
1144 non-whitespace character, that is of the expected form.
1145
1146 The form of numeric data is a non-empty sequence of significand digits
1147 with an optional decimal point, and an optional exponent consisting of
1148 an exponent prefix followed by an optional sign and a non-empty sequence
1149 of decimal digits. A significand digit is either a decimal digit or a
1150 Latin letter (62 possible characters), with @code{a} = 10, @code{b} = 11,
1151 @dots{}, @code{z} = 35; its value must be strictly less than the base.
1152 The decimal point can be either the one defined by the current locale or
1153 the period (the first one is accepted for consistency with the C standard
1154 and the practice, the second one is accepted to allow the programmer to
1155 provide MPFR numbers from strings in a way that does not depend on the
1156 current locale).
1157 The exponent prefix can be @code{e} or @code{E} for bases up to 10, or
1158 @code{@@} in any base; it indicates a multiplication by a power of the
1159 base. In bases 2 and 16, the exponent prefix can also be @code{p} or
1160 @code{P}, in which case it introduces a binary exponent: it indicates a
1161 multiplication by a power of 2 (there is a difference only for base 16).
1162 The value of an exponent is always written in base 10.
1163 In base 2, the significand can start with @code{0b} or @code{0B}, and
1164 in base 16, it can start with @code{0x} or @code{0X}.
1165
1166 If the argument @var{base} is 0, then the base is automatically detected
1167 as follows. If the significand starts with @code{0b} or @code{0B}, base 2
1168 is assumed. If the significand starts with @code{0x} or @code{0X}, base 16
1169 is assumed. Otherwise base 10 is assumed.
1170
1171 Note: The exponent must contain at least a digit. Otherwise the possible
1172 exponent prefix and sign are not part of the number (which ends with the
1173 significand). Similarly, if @code{0b}, @code{0B}, @code{0x} or @code{0X}
1174 is not followed by a binary/hexadecimal digit, then the subject sequence
1175 stops at the character @code{0}.
1176
1177 Special data (for infinities and NaN) can be @code{@@inf@@} or
1178 @code{@@nan@@(n-char-sequence)}, and if @math{@var{base} @le{} 16},
1179 it can also be @code{infinity}, @code{inf}, @code{nan} or
1180 @code{nan(n-char-sequence-opt)}, all case insensitive.
1181 A @code{n-char-sequence-opt} is a possibly empty string containing only digits,
1182 Latin letters and the underscore (0, 1, 2, @dots{}, 9, a, b, @dots{}, z,
1183 A, B, @dots{}, Z, _). Note: one has an optional sign for all data, even
1184 NaN.
1185
1186 @end deftypefun
1187
1188 @deftypefun void mpfr_set_inf (mpfr_t @var{x}, int @var{sign})
1189 @deftypefunx void mpfr_set_nan (mpfr_t @var{x})
1190 Set the variable @var{x} to infinity or NaN (Not-a-Number) respectively.
1191 In @code{mpfr_set_inf}, @var{x} is set to plus infinity iff @var{sign} is
1192 nonnegative.
1193 @end deftypefun
1194
1195 @deftypefun void mpfr_swap (mpfr_t @var{x}, mpfr_t @var{y})
1196 Swap the values @var{x} and @var{y} efficiently. Warning: the
1197 precisions are exchanged too; in case the precisions are different,
1198 @code{mpfr_swap} is thus not equivalent to three @code{mpfr_set} calls
1199 using a third auxiliary variable.
1200 @end deftypefun
1201
1202 @node Combined Initialization and Assignment Functions, Conversion Functions, Assignment Functions, MPFR Interface
1203 @comment  node-name,  next,  previous,  up
1204 @cindex Combined initialization and assignment functions
1205 @section Combined Initialization and Assignment Functions
1206
1207 @deftypefn Macro int mpfr_init_set (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1208 @deftypefnx Macro int mpfr_init_set_ui (mpfr_t @var{rop}, unsigned long int @var{op}, mp_rnd_t @var{rnd})
1209 @deftypefnx Macro int mpfr_init_set_si (mpfr_t @var{rop}, signed long int @var{op}, mp_rnd_t @var{rnd})
1210 @deftypefnx Macro int mpfr_init_set_d (mpfr_t @var{rop}, double @var{op}, mp_rnd_t @var{rnd})
1211 @deftypefnx Macro int mpfr_init_set_ld (mpfr_t @var{rop}, long double @var{op}, mp_rnd_t @var{rnd})
1212 @deftypefnx Macro int mpfr_init_set_z (mpfr_t @var{rop}, mpz_t @var{op}, mp_rnd_t @var{rnd})
1213 @deftypefnx Macro int mpfr_init_set_q (mpfr_t @var{rop}, mpq_t @var{op}, mp_rnd_t @var{rnd})
1214 @deftypefnx Macro int mpfr_init_set_f (mpfr_t @var{rop}, mpf_t @var{op}, mp_rnd_t @var{rnd})
1215 Initialize @var{rop} and set its value from @var{op}, rounded in the direction
1216 @var{rnd}.
1217 The precision of @var{rop} will be taken from the active default precision,
1218 as set by @code{mpfr_set_default_prec}.
1219 @end deftypefn
1220
1221 @deftypefun int mpfr_init_set_str (mpfr_t @var{x}, const char *@var{s}, int @var{base}, mp_rnd_t @var{rnd})
1222 Initialize @var{x} and set its value from
1223 the string @var{s} in base @var{base},
1224 rounded in the direction @var{rnd}.
1225 See @code{mpfr_set_str}.
1226 @end deftypefun
1227
1228 @node Conversion Functions, Basic Arithmetic Functions, Combined Initialization and Assignment Functions, MPFR Interface
1229 @comment  node-name,  next,  previous,  up
1230 @cindex Conversion functions
1231 @section Conversion Functions
1232
1233 @deftypefun double mpfr_get_d (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1234 @deftypefunx {long double} mpfr_get_ld (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1235 @deftypefunx _Decimal64 mpfr_get_decimal64 (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1236 Convert @var{op} to a @code{double} (respectively @code{_Decimal64} or
1237 @code{long double}), using the rounding mode @var{rnd}.
1238 If @var{op} is NaN, some fixed NaN (either quiet or signaling) or the result
1239 of 0.0/0.0 is returned. If @var{op} is @pom{}Inf, an infinity of the same
1240 sign or the result of @pom{}1.0/0.0 is returned. If @var{op} is zero, these
1241 functions return a zero, trying to preserve its sign, if possible.
1242 The @code{mpfr_get_decimal64} function is built only under some conditions:
1243 see the documentation of @code{mpfr_set_decimal64}.
1244 @end deftypefun
1245
1246 @deftypefun double mpfr_get_d_2exp (long *@var{exp}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1247 @deftypefunx {long double} mpfr_get_ld_2exp (long *@var{exp}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1248 Return @var{d} and set @var{exp} such that @math{0.5@le{}@GMPabs{@var{d}}<1}
1249 and @m{@var{d}\times 2^{exp}, @var{d} times 2 raised to @var{exp}} equals
1250 @var{op} rounded to double (resp.@: long double)
1251 precision, using the given rounding mode.
1252 @comment See ISO C standard, frexp function.
1253 If @var{op} is zero, then a zero of the same sign (or an unsigned zero,
1254 if the implementation does not have signed zeros) is returned, and
1255 @var{exp} is set to 0.
1256 If @var{op} is NaN or an infinity, then the corresponding double precision
1257 (resp.@: long-double precision)
1258 value is returned, and @var{exp} is undefined.
1259 @end deftypefun
1260
1261 @deftypefun long mpfr_get_si (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1262 @deftypefunx {unsigned long} mpfr_get_ui (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1263 @deftypefunx intmax_t mpfr_get_sj (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1264 @deftypefunx uintmax_t mpfr_get_uj (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1265 Convert @var{op} to a @code{long}, an @code{unsigned long},
1266 an @code{intmax_t} or an @code{uintmax_t} (respectively) after rounding
1267 it with respect to @var{rnd}.
1268 If @var{op} is NaN, the result is undefined.
1269 If @var{op} is too big for the return type, it returns the maximum
1270 or the minimum of the corresponding C type, depending on the direction
1271 of the overflow. The @emph{erange} flag is set too.
1272 See also @code{mpfr_fits_slong_p}, @code{mpfr_fits_ulong_p},
1273 @code{mpfr_fits_intmax_p} and @code{mpfr_fits_uintmax_p}.
1274 @end deftypefun
1275
1276 @deftypefun mp_exp_t mpfr_get_z_exp (mpz_t @var{rop}, mpfr_t @var{op})
1277 Put the scaled significand of @var{op} (regarded as an integer, with the
1278 precision of @var{op}) into @var{rop}, and return the exponent @var{exp}
1279 (which may be outside the current exponent range) such that @var{op}
1280 exactly equals
1281 @ifnottex
1282 @var{rop} multiplied by two exponent @var{exp}.
1283 @end ifnottex
1284 @tex
1285 $rop \times 2^{\rm exp}$.
1286 @end tex
1287 If @var{op} is zero, the minimal exponent @code{emin} is returned.
1288 If the exponent is not representable in the @code{mp_exp_t} type, the
1289 behavior is undefined.
1290 @end deftypefun
1291
1292 @deftypefun void mpfr_get_z (mpz_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1293 Convert @var{op} to a @code{mpz_t}, after rounding it with respect to
1294 @var{rnd}. If @var{op} is NaN or Inf, the result is undefined.
1295 @end deftypefun
1296
1297 @deftypefun int mpfr_get_f (mpf_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1298 Convert @var{op} to a @code{mpf_t}, after rounding it with respect to
1299 @var{rnd}. Return zero iff no error occurred,
1300 in particular a non-zero value is returned if
1301 @var{op} is NaN or Inf, which do not exist in @code{mpf}.
1302 @end deftypefun
1303
1304 @deftypefun {char *} mpfr_get_str (char *@var{str}, mp_exp_t *@var{expptr}, int @var{b}, size_t @var{n}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1305 Convert @var{op} to a string of digits in base @var{b}, with rounding in
1306 the direction @var{rnd}, where @var{n} is either zero (see below) or the
1307 number of significant digits; in the latter case, @var{n} must be greater
1308 or equal to 2. The base may vary from 2 to 36.
1309
1310 The generated string is a fraction, with an implicit radix point immediately
1311 to the left of the first digit.  For example, the number @minus{}3.1416 would
1312 be returned as "@minus{}31416" in the string and 1 written at @var{expptr}.
1313 If @var{rnd} is to nearest, and @var{op} is exactly in the middle of two
1314 possible outputs, the one with an even significand is chosen:
1315 that significand is considered with the exponent of @var{op}.
1316 Note that for an odd base, this may not correspond to an even last digit:
1317 for example with 2 digits in base 7, 16 and a half is rounded to
1318 20 which is 14 in decimal,
1319 36 and a half is rounded to 40 which is 28 in decimal,
1320 and 66 and a half is rounded to 66 which is 48 in decimal.
1321
1322 If @var{n} is zero, the number of digits of the significand is chosen
1323 large enough so that re-reading the printed value with the same precision,
1324 assuming both output and input use rounding to nearest, will recover
1325 the original value of @var{op}.
1326 More precisely, in most cases, the chosen precision of @var{str} is
1327 the minimal precision depending on @var{p} = PREC(@var{op}) and @var{b}
1328 only that satisfies the above property, i.e.,
1329 @ifnottex
1330 m = 1 + ceil(@var{p}*log(2)/log(@var{b})),
1331 @end ifnottex
1332 @tex
1333 $m = 1 + \lceil p {\log 2 \over \log b} \rceil$,
1334 @end tex
1335 with @var{p} replaced by @var{p}@minus{}1 if @var{b} is a power of 2,
1336 but in some very rare cases, it might be @math{m+1}
1337 (the smallest case for bases up to 62 is when @var{p} equals 186564318007
1338 for bases 7 and 49).
1339
1340 If @var{str} is a null pointer, space for the significand is allocated using
1341 the current allocation function, and a pointer to the string is returned.
1342 To free the returned string, you must use @code{mpfr_free_str}.
1343
1344 If @var{str} is not a null pointer, it should point to a block of storage
1345 large enough for the significand, i.e., at least @code{max(@var{n} + 2, 7)}.
1346 The extra two bytes are for a possible minus sign, and for the terminating null
1347 character.
1348
1349 If the input number is an ordinary number, the exponent is written through
1350 the pointer @var{expptr} (the current minimal exponent for 0).
1351
1352 A pointer to the string is returned, unless there is an error, in which
1353 case a null pointer is returned.
1354 @end deftypefun
1355
1356 @deftypefun void mpfr_free_str (char *@var{str})
1357 Free a string allocated by @code{mpfr_get_str} using the current unallocation
1358 function (preliminary interface).
1359 The block is assumed to be @code{strlen(@var{str})+1} bytes.
1360 For more information about how it is done:
1361 @ifinfo
1362 @pxref{Custom Allocation,,, gmp.info,GNU MP}.
1363 @end ifinfo
1364 @ifnotinfo
1365 see Section ``Custom Allocation'' in @cite{GNU MP}.
1366 @end ifnotinfo
1367 @end deftypefun
1368
1369 @deftypefun int mpfr_fits_ulong_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1370 @deftypefunx int mpfr_fits_slong_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1371 @deftypefunx int mpfr_fits_uint_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1372 @deftypefunx int mpfr_fits_sint_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1373 @deftypefunx int mpfr_fits_ushort_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1374 @deftypefunx int mpfr_fits_sshort_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1375 @deftypefunx int mpfr_fits_intmax_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1376 @deftypefunx int mpfr_fits_uintmax_p (mpfr_t @var{op}, mp_rnd_t @var{rnd})
1377 Return non-zero if @var{op} would fit in the respective C data type, when
1378 rounded to an integer in the direction @var{rnd}.
1379 @end deftypefun
1380
1381 @node Basic Arithmetic Functions, Comparison Functions, Conversion Functions, MPFR Interface
1382 @comment  node-name,  next,  previous,  up
1383 @cindex Basic arithmetic functions
1384 @cindex Float arithmetic functions
1385 @cindex Arithmetic functions
1386 @section Basic Arithmetic Functions
1387
1388 @deftypefun int mpfr_add (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1389 @deftypefunx int mpfr_add_ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1390 @deftypefunx int mpfr_add_si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1391 @deftypefunx int mpfr_add_d (mpfr_t @var{rop}, mpfr_t @var{op1}, double @var{op2}, mp_rnd_t @var{rnd})
1392 @deftypefunx int mpfr_add_z (mpfr_t @var{rop}, mpfr_t @var{op1}, mpz_t @var{op2}, mp_rnd_t @var{rnd})
1393 @deftypefunx int mpfr_add_q (mpfr_t @var{rop}, mpfr_t @var{op1}, mpq_t @var{op2}, mp_rnd_t @var{rnd})
1394 Set @var{rop} to @math{@var{op1} + @var{op2}} rounded in the direction
1395 @var{rnd}. For types having no signed zero, it is considered unsigned
1396 (i.e.@: (+0) + 0 = (+0) and (@minus{}0) + 0 = (@minus{}0)).
1397 The @code{mpfr_add_d} function assumes that the radix of the @code{double} type
1398 is a power of 2, with a precision at most that declared by the C implementation
1399 (macro @code{IEEE_DBL_MANT_DIG}, and if not defined 53 bits).
1400 @end deftypefun
1401
1402 @deftypefun int mpfr_sub (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1403 @deftypefunx int mpfr_ui_sub (mpfr_t @var{rop}, unsigned long int @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1404 @deftypefunx int mpfr_sub_ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1405 @deftypefunx int mpfr_si_sub (mpfr_t @var{rop}, long int @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1406 @deftypefunx int mpfr_sub_si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1407 @deftypefunx int mpfr_d_sub (mpfr_t @var{rop}, double @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1408 @deftypefunx int mpfr_sub_d (mpfr_t @var{rop}, mpfr_t @var{op1}, double @var{op2}, mp_rnd_t @var{rnd})
1409 @deftypefunx int mpfr_sub_z (mpfr_t @var{rop}, mpfr_t @var{op1}, mpz_t @var{op2}, mp_rnd_t @var{rnd})
1410 @deftypefunx int mpfr_sub_q (mpfr_t @var{rop}, mpfr_t @var{op1}, mpq_t @var{op2}, mp_rnd_t @var{rnd})
1411 Set @var{rop} to @math{@var{op1} - @var{op2}} rounded in the direction
1412 @var{rnd}. For types having no signed zero, it is considered unsigned
1413 (i.e.@: (+0) @minus{} 0 = (+0), (@minus{}0) @minus{} 0 = (@minus{}0),
1414 0 @minus{} (+0) = (@minus{}0) and 0 @minus{} (@minus{}0) = (+0)).
1415 The same restrictions than for @code{mpfr_add_d} apply to @code{mpfr_d_sub}
1416 and @code{mpfr_sub_d}.
1417 @end deftypefun
1418
1419 @deftypefun int mpfr_mul (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1420 @deftypefunx int mpfr_mul_ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1421 @deftypefunx int mpfr_mul_si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1422 @deftypefunx int mpfr_mul_d (mpfr_t @var{rop}, mpfr_t @var{op1}, double @var{op2}, mp_rnd_t @var{rnd})
1423 @deftypefunx int mpfr_mul_z (mpfr_t @var{rop}, mpfr_t @var{op1}, mpz_t @var{op2}, mp_rnd_t @var{rnd})
1424 @deftypefunx int mpfr_mul_q (mpfr_t @var{rop}, mpfr_t @var{op1}, mpq_t @var{op2}, mp_rnd_t @var{rnd})
1425 Set @var{rop} to @math{@var{op1} @GMPtimes{} @var{op2}} rounded in the
1426 direction @var{rnd}.
1427 When a result is zero, its sign is the product of the signs of the operands
1428 (for types having no signed zero, it is considered positive).
1429 The same restrictions than for @code{mpfr_add_d} apply to @code{mpfr_mul_d}.
1430 @end deftypefun
1431
1432 @deftypefun int mpfr_sqr (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1433 Set @var{rop} to @m{@var{op}^{2}, the square of @var{op}}
1434 rounded in the direction @var{rnd}.
1435 @end deftypefun
1436
1437 @deftypefun int mpfr_div (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1438 @deftypefunx int mpfr_ui_div (mpfr_t @var{rop}, unsigned long int @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1439 @deftypefunx int mpfr_div_ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1440 @deftypefunx int mpfr_si_div (mpfr_t @var{rop}, long int @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1441 @deftypefunx int mpfr_div_si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1442 @deftypefunx int mpfr_d_div (mpfr_t @var{rop}, double @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1443 @deftypefunx int mpfr_div_d (mpfr_t @var{rop}, mpfr_t @var{op1}, double @var{op2}, mp_rnd_t @var{rnd})
1444 @deftypefunx int mpfr_div_z (mpfr_t @var{rop}, mpfr_t @var{op1}, mpz_t @var{op2}, mp_rnd_t @var{rnd})
1445 @deftypefunx int mpfr_div_q (mpfr_t @var{rop}, mpfr_t @var{op1}, mpq_t @var{op2}, mp_rnd_t @var{rnd})
1446 Set @var{rop} to @math{@var{op1}/@var{op2}} rounded in the direction @var{rnd}.
1447 When a result is zero, its sign is the product of the signs of the operands
1448 (for types having no signed zero, it is considered positive).
1449 The same restrictions than for @code{mpfr_add_d} apply to @code{mpfr_d_div}
1450 and @code{mpfr_div_d}.
1451 @end deftypefun
1452
1453 @deftypefun int mpfr_sqrt (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1454 @deftypefunx int mpfr_sqrt_ui (mpfr_t @var{rop}, unsigned long int @var{op}, mp_rnd_t @var{rnd})
1455 Set @var{rop} to @m{\sqrt{@var{op}}, the square root of @var{op}}
1456 rounded in the direction @var{rnd}. Return @minus{}0 if @var{op} is
1457 @minus{}0 (to be consistent with the IEEE 754 standard).
1458 Set @var{rop} to NaN if @var{op} is negative.
1459 @end deftypefun
1460
1461 @deftypefun int mpfr_rec_sqrt (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1462 Set @var{rop} to @m{1/\sqrt{@var{op}}, the reciprocal square root of @var{op}}
1463 rounded in the direction @var{rnd}. Return +Inf if @var{op} is @pom{}0,
1464 and +0 if @var{op} is +Inf.
1465 Set @var{rop} to NaN if @var{op} is negative.
1466 @end deftypefun
1467
1468 @deftypefun int mpfr_cbrt (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1469 @deftypefunx int mpfr_root (mpfr_t @var{rop}, mpfr_t @var{op}, unsigned long int @var{k}, mp_rnd_t @var{rnd})
1470 Set @var{rop} to the cubic root (resp.@: the @var{k}th root)
1471 of @var{op} rounded in the direction @var{rnd}.
1472 An odd (resp.@: even) root of a negative number (including @minus{}Inf)
1473 returns a negative number (resp.@: NaN).
1474 The @var{k}th root of @minus{}0 is defined to be @minus{}0,
1475 whatever the parity of @var{k}.
1476 @end deftypefun
1477
1478 @deftypefun int mpfr_pow (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1479 @deftypefunx int mpfr_pow_ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1480 @deftypefunx int mpfr_pow_si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1481 @deftypefunx int mpfr_pow_z (mpfr_t @var{rop}, mpfr_t @var{op1}, mpz_t @var{op2}, mp_rnd_t @var{rnd})
1482 @deftypefunx int mpfr_ui_pow_ui (mpfr_t @var{rop}, unsigned long int @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1483 @deftypefunx int mpfr_ui_pow (mpfr_t @var{rop}, unsigned long int @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1484 Set @var{rop} to @m{@var{op1}^{op2}, @var{op1} raised to @var{op2}},
1485 rounded in the direction @var{rnd}.
1486 Special values are currently handled as described in the ISO C99 standard
1487 for the @code{pow} function (note this may change in future versions):
1488 @itemize @bullet
1489 @item @code{pow(@pom{}0, @var{y})} returns plus or minus infinity for @var{y} a negative odd integer.
1490 @item @code{pow(@pom{}0, @var{y})} returns plus infinity for @var{y} negative and not an odd integer.
1491 @item @code{pow(@pom{}0, @var{y})} returns plus or minus zero for @var{y} a positive odd integer.
1492 @item @code{pow(@pom{}0, @var{y})} returns plus zero for @var{y} positive and not an odd integer.
1493 @item @code{pow(-1, @pom{}Inf)} returns 1.
1494 @item @code{pow(+1, @var{y})} returns 1 for any @var{y}, even a NaN.
1495 @item @code{pow(@var{x}, @pom{}0)} returns 1 for any @var{x}, even a NaN.
1496 @item @code{pow(@var{x}, @var{y})} returns NaN for finite negative @var{x} and finite non-integer @var{y}.
1497 @item @code{pow(@var{x}, -Inf)} returns plus infinity for @math{0 < @GMPabs{x} < 1}, and plus zero for @math{@GMPabs{x} > 1}.
1498 @item @code{pow(@var{x}, +Inf)} returns plus zero for @math{0 < @GMPabs{x} < 1}, and plus infinity for @math{@GMPabs{x} > 1}.
1499 @item @code{pow(-Inf, @var{y})} returns minus zero for @var{y} a negative odd integer.
1500 @item @code{pow(-Inf, @var{y})} returns plus zero for @var{y} negative and not an odd integer.
1501 @item @code{pow(-Inf, @var{y})} returns minus infinity for @var{y} a positive odd integer.
1502 @item @code{pow(-Inf, @var{y})} returns plus infinity for @var{y} positive and not an odd integer.
1503 @item @code{pow(+Inf, @var{y})} returns plus zero for @var{y} negative, and plus infinity for @var{y} positive.
1504 @end itemize
1505 @end deftypefun
1506
1507 @deftypefun int mpfr_neg (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1508 Set @var{rop} to @math{-@var{op}} rounded in the direction @var{rnd}.
1509 Just changes the sign if @var{rop} and @var{op} are the same variable.
1510 @end deftypefun
1511
1512 @deftypefun int mpfr_abs (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1513 Set @var{rop} to the absolute value of @var{op},
1514 rounded in the direction @var{rnd}.
1515 Just changes the sign if @var{rop} and @var{op} are the same variable.
1516 @end deftypefun
1517
1518 @deftypefun int mpfr_dim (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1519 Set @var{rop} to the positive difference of @var{op1} and @var{op2}, i.e.,
1520 @math{@var{op1} - @var{op2}} rounded in the direction @var{rnd}
1521 if @math{@var{op1} > @var{op2}}, and +0 otherwise.
1522 Returns NaN when @var{op1} or @var{op2} is NaN.
1523 @end deftypefun
1524
1525 @deftypefun int mpfr_mul_2ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1526 @deftypefunx int mpfr_mul_2si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1527 Set @var{rop} to @m{@var{op1} \times 2^{op2}, @var{op1} times 2 raised
1528 to @var{op2}}
1529 rounded in the direction @var{rnd}. Just increases the exponent by @var{op2}
1530 when @var{rop} and @var{op1} are identical.
1531 @end deftypefun
1532
1533 @deftypefun int mpfr_div_2ui (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
1534 @deftypefunx int mpfr_div_2si (mpfr_t @var{rop}, mpfr_t @var{op1}, long int @var{op2}, mp_rnd_t @var{rnd})
1535 Set @var{rop} to @m{@var{op1}/2^{op2}, @var{op1} divided by 2 raised
1536 to @var{op2}}
1537 rounded in the direction @var{rnd}. Just decreases the exponent by @var{op2}
1538 when @var{rop} and @var{op1} are identical.
1539 @end deftypefun
1540
1541 @node Comparison Functions, Special Functions, Basic Arithmetic Functions, MPFR Interface
1542 @comment  node-name,  next,  previous,  up
1543 @cindex Float comparisons functions
1544 @cindex Comparison functions
1545 @section Comparison Functions
1546
1547 @deftypefun int mpfr_cmp (mpfr_t @var{op1}, mpfr_t @var{op2})
1548 @deftypefunx int mpfr_cmp_ui (mpfr_t @var{op1}, unsigned long int @var{op2})
1549 @deftypefunx int mpfr_cmp_si (mpfr_t @var{op1}, signed long int @var{op2})
1550 @deftypefunx int mpfr_cmp_d (mpfr_t @var{op1}, double @var{op2})
1551 @deftypefunx int mpfr_cmp_ld (mpfr_t @var{op1}, long double @var{op2})
1552 @deftypefunx int mpfr_cmp_z (mpfr_t @var{op1}, mpz_t @var{op2})
1553 @deftypefunx int mpfr_cmp_q (mpfr_t @var{op1}, mpq_t @var{op2})
1554 @deftypefunx int mpfr_cmp_f (mpfr_t @var{op1}, mpf_t @var{op2})
1555 Compare @var{op1} and @var{op2}.  Return a positive value if @math{@var{op1} >
1556 @var{op2}}, zero if @math{@var{op1} = @var{op2}}, and a negative value if
1557 @math{@var{op1} < @var{op2}}.
1558 Both @var{op1} and @var{op2} are considered to their full own precision,
1559 which may differ.
1560 If one of the operands is NaN, set the @emph{erange} flag and return zero.
1561
1562 Note: These functions may be useful to distinguish the three possible cases.
1563 If you need to distinguish two cases only, it is recommended to use the
1564 predicate functions (e.g., @code{mpfr_equal_p} for the equality) described
1565 below; they behave like the IEEE 754 comparisons, in particular when one
1566 or both arguments are NaN. But only floating-point numbers can be compared
1567 (you may need to do a conversion first).
1568 @end deftypefun
1569
1570 @deftypefun int mpfr_cmp_ui_2exp (mpfr_t @var{op1}, unsigned long int @var{op2}, mp_exp_t @var{e})
1571 @deftypefunx int mpfr_cmp_si_2exp (mpfr_t @var{op1}, long int @var{op2}, mp_exp_t @var{e})
1572 Compare @var{op1} and @m{@var{op2} \times 2^e, @var{op2} multiplied by two to
1573 the power @var{e}}. Similar as above.
1574 @end deftypefun
1575
1576 @deftypefun int mpfr_cmpabs (mpfr_t @var{op1}, mpfr_t @var{op2})
1577 Compare @math{|@var{op1}|} and @math{|@var{op2}|}.  Return a positive value if
1578 @math{|@var{op1}| > |@var{op2}|}, zero if @math{|@var{op1}| = |@var{op2}|}, and
1579 a negative value if @math{|@var{op1}| < |@var{op2}|}.
1580 If one of the operands is NaN, set the @emph{erange} flag and return zero.
1581 @end deftypefun
1582
1583 @deftypefun int mpfr_nan_p (mpfr_t @var{op})
1584 @deftypefunx int mpfr_inf_p (mpfr_t @var{op})
1585 @deftypefunx int mpfr_number_p (mpfr_t @var{op})
1586 @deftypefunx int mpfr_zero_p (mpfr_t @var{op})
1587 Return non-zero if @var{op} is respectively NaN, an infinity, an ordinary
1588 number (i.e.@: neither NaN nor an infinity) or zero. Return zero otherwise.
1589 @end deftypefun
1590
1591 @deftypefn Macro int mpfr_sgn (mpfr_t @var{op})
1592 Return a positive value if @math{@var{op} > 0}, zero if @math{@var{op} = 0},
1593 and a negative value if @math{@var{op} < 0}.
1594 If the operand is NaN, set the @emph{erange} flag and return zero.
1595 @end deftypefn
1596
1597 @deftypefun int mpfr_greater_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1598 Return non-zero if @math{@var{op1} > @var{op2}}, zero otherwise.
1599 @end deftypefun
1600
1601 @deftypefun int mpfr_greaterequal_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1602 Return non-zero if @math{@var{op1} @ge{} @var{op2}}, zero otherwise.
1603 @end deftypefun
1604
1605 @deftypefun int mpfr_less_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1606 Return non-zero if @math{@var{op1} < @var{op2}}, zero otherwise.
1607 @end deftypefun
1608
1609 @deftypefun int mpfr_lessequal_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1610 Return non-zero if @math{@var{op1} @le{} @var{op2}}, zero otherwise.
1611 @end deftypefun
1612
1613 @deftypefun int mpfr_lessgreater_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1614 Return non-zero if @math{@var{op1} < @var{op2}} or
1615 @math{@var{op1} > @var{op2}} (i.e.@: neither @var{op1}, nor @var{op2} is
1616 NaN, and @math{@var{op1} @ne{} @var{op2}}), zero otherwise (i.e.@: @var{op1}
1617 and/or @var{op2} are NaN, or @math{@var{op1} = @var{op2}}).
1618 @end deftypefun
1619
1620 @deftypefun int mpfr_equal_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1621 Return non-zero if @math{@var{op1} = @var{op2}}, zero otherwise
1622 (i.e.@: @var{op1} and/or @var{op2} are NaN, or
1623 @math{@var{op1} @ne{} @var{op2}}).
1624 @end deftypefun
1625
1626 @deftypefun int mpfr_unordered_p (mpfr_t @var{op1}, mpfr_t @var{op2})
1627 Return non-zero if @var{op1} or @var{op2} is a NaN (i.e.@: they cannot be
1628 compared), zero otherwise.
1629 @end deftypefun
1630
1631 @node Special Functions, Input and Output Functions, Comparison Functions, MPFR Interface
1632 @cindex Special functions
1633 @section Special Functions
1634
1635 All those functions, except explicitly stated, return zero for an
1636 exact return value, a positive value for a return value larger than the
1637 exact result, and a negative value otherwise.
1638
1639 Important note: in some domains, computing special functions (either with
1640 correct or incorrect rounding) is expensive, even for small precision,
1641 for example the trigonometric and Bessel functions for large argument.
1642
1643 @deftypefun int mpfr_log (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1644 @deftypefunx int mpfr_log2 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1645 @deftypefunx int mpfr_log10 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1646 Set @var{rop} to the natural logarithm of @var{op},
1647 @m{\log_2 @var{op}, log2(@var{op})} or
1648 @m{\log_{10} @var{op}, log10(@var{op})}, respectively,
1649 rounded in the direction @var{rnd}.
1650 Return @minus{}Inf if @var{op} is @minus{}0 (i.e.@: the sign of the zero
1651 has no influence on the result).
1652 @end deftypefun
1653
1654 @deftypefun int mpfr_exp (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1655 @deftypefunx int mpfr_exp2 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1656 @deftypefunx int mpfr_exp10 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1657 Set @var{rop} to the exponential of @var{op},
1658  to @m{2^{op}, 2 power of @var{op}}
1659 or to @m{10^{op}, 10 power of @var{op}}, respectively,
1660 rounded in the direction @var{rnd}.
1661 @end deftypefun
1662
1663 @deftypefun int mpfr_cos (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1664 @deftypefunx int mpfr_sin (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1665 @deftypefunx int mpfr_tan (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1666 Set @var{rop} to the cosine of @var{op}, sine of @var{op},
1667 tangent of @var{op}, rounded in the direction @var{rnd}.
1668 @end deftypefun
1669
1670 @deftypefun int mpfr_sec (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1671 @deftypefunx int mpfr_csc (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1672 @deftypefunx int mpfr_cot (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1673 Set @var{rop} to the secant of @var{op}, cosecant of @var{op},
1674 cotangent of @var{op}, rounded in the direction @var{rnd}.
1675 @end deftypefun
1676
1677 @deftypefun int mpfr_sin_cos (mpfr_t @var{sop}, mpfr_t @var{cop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1678 Set simultaneously @var{sop} to the sine of @var{op} and
1679                    @var{cop} to the cosine of @var{op},
1680 rounded in the direction @var{rnd} with the corresponding precisions of
1681 @var{sop} and @var{cop}, which must be different variables.
1682 Return 0 iff both results are exact.
1683 @end deftypefun
1684
1685 @deftypefun int mpfr_acos (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1686 @deftypefunx int mpfr_asin (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1687 @deftypefunx int mpfr_atan (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1688 Set @var{rop} to the arc-cosine, arc-sine or arc-tangent of @var{op},
1689 rounded in the direction @var{rnd}.
1690 Note that since @code{acos(-1)} returns the floating-point number closest to
1691 @m{\pi,Pi} according to the given rounding mode, this number might not be
1692 in the output range @math{0 @le{} @var{rop} < \pi}
1693 of the arc-cosine function;
1694 still, the result lies in the image of the output range
1695 by the rounding function.
1696 The same holds for @code{asin(-1)}, @code{asin(1)}, @code{atan(-Inf)},
1697 @code{atan(+Inf)}.
1698 @end deftypefun
1699
1700 @deftypefun int mpfr_atan2 (mpfr_t @var{rop}, mpfr_t @var{y}, mpfr_t @var{x}, mp_rnd_t @var{rnd})
1701 Set @var{rop} to the arc-tangent2 of @var{y} and @var{x},
1702 rounded in the direction @var{rnd}:
1703 if @code{x > 0}, @code{atan2(y, x) = atan (y/x)};
1704 if @code{x < 0}, @code{atan2(y, x) = sign(y)*(Pi - atan (@GMPabs{y/x}))}.
1705 As for @code{atan}, in case the exact mathematical result is @m{+\pi,+Pi} or
1706 @m{-\pi,-Pi}, its rounded result might be outside the function output range.
1707
1708 @code{atan2(y, 0)} does not raise any floating-point exception.
1709 Special values are currently handled as described in the ISO C99 standard
1710 for the @code{atan2} function (note this may change in future versions):
1711 @itemize @bullet
1712 @item @code{atan2(+0, -0)} returns @m{+\pi,+Pi}.
1713 @item @code{atan2(-0, -0)} returns @m{-\pi,-Pi}.
1714 @item @code{atan2(+0, +0)} returns +0.
1715 @item @code{atan2(-0, +0)} returns @minus{}0.
1716 @item @code{atan2(+0, x)} returns @m{+\pi,+Pi} for @math{x < 0}.
1717 @item @code{atan2(-0, x)} returns @m{-\pi,-Pi} for @math{x < 0}.
1718 @item @code{atan2(+0, x)} returns +0 for @math{x > 0}.
1719 @item @code{atan2(-0, x)} returns @minus{}0 for @math{x > 0}.
1720 @item @code{atan2(y, 0)} returns @m{-\pi/2,-Pi/2} for @math{y < 0}.
1721 @item @code{atan2(y, 0)} returns @m{+\pi/2,+Pi/2} for @math{y > 0}.
1722 @item @code{atan2(+Inf, -Inf)} returns @m{+3*\pi/4,+3*Pi/4}.
1723 @item @code{atan2(-Inf, -Inf)} returns @m{-3*\pi/4,-3*Pi/4}.
1724 @item @code{atan2(+Inf, +Inf)} returns @m{+\pi/4,+Pi/4}.
1725 @item @code{atan2(-Inf, +Inf)} returns @m{-\pi/4,-Pi/4}.
1726 @item @code{atan2(+Inf, x)} returns @m{+\pi/2,+Pi/2} for finite @math{x}.
1727 @item @code{atan2(-Inf, x)} returns @m{-\pi/2,-Pi/2} for finite @math{x}.
1728 @item @code{atan2(y, -Inf)} returns @m{+\pi,+Pi} for finite @math{y > 0}.
1729 @item @code{atan2(y, -Inf)} returns @m{-\pi,-Pi} for finite @math{y < 0}.
1730 @item @code{atan2(y, +Inf)} returns +0 for finite @math{y > 0}.
1731 @item @code{atan2(y, +Inf)} returns @minus{}0 for finite @math{y < 0}.
1732 @end itemize
1733 @end deftypefun
1734
1735 @deftypefun int mpfr_cosh (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1736 @deftypefunx int mpfr_sinh (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1737 @deftypefunx int mpfr_tanh (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1738 Set @var{rop} to the hyperbolic cosine, sine or tangent of @var{op},
1739 rounded in the direction @var{rnd}.
1740 @end deftypefun
1741
1742 @deftypefun int mpfr_sinh_cosh (mpfr_t @var{sop}, mpfr_t @var{cop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1743 Set simultaneously @var{sop} to the hyperbolic sine of @var{op} and
1744                    @var{cop} to the hyperbolic cosine of @var{op},
1745 rounded in the direction @var{rnd} with the corresponding precision of
1746 @var{sop} and @var{cop} which must be different variables.
1747 Return 0 iff both results are exact.
1748 @end deftypefun
1749
1750 @deftypefun int mpfr_sech (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1751 @deftypefunx int mpfr_csch (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1752 @deftypefunx int mpfr_coth (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1753 Set @var{rop} to the hyperbolic secant of @var{op}, cosecant of @var{op},
1754 cotangent of @var{op}, rounded in the direction @var{rnd}.
1755 @end deftypefun
1756
1757 @deftypefun int mpfr_acosh (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1758 @deftypefunx int mpfr_asinh (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1759 @deftypefunx int mpfr_atanh (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1760 Set @var{rop} to the inverse hyperbolic cosine, sine or tangent of @var{op},
1761 rounded in the direction @var{rnd}.
1762 @end deftypefun
1763
1764 @deftypefun int mpfr_fac_ui (mpfr_t @var{rop}, unsigned long int  @var{op}, mp_rnd_t @var{rnd})
1765 Set @var{rop} to the factorial of the @code{unsigned long int} @var{op},
1766 rounded in the direction @var{rnd}.
1767 @end deftypefun
1768
1769 @deftypefun int mpfr_log1p (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1770 Set @var{rop} to the logarithm of one plus @var{op},
1771 rounded in the direction @var{rnd}.
1772 @end deftypefun
1773
1774 @deftypefun int mpfr_expm1 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1775 Set @var{rop} to the exponential of @var{op} minus one,
1776 rounded in the direction @var{rnd}.
1777 @end deftypefun
1778
1779 @deftypefun int mpfr_eint (mpfr_t @var{y}, mpfr_t @var{x}, mp_rnd_t @var{rnd})
1780 Set @var{y} to the exponential integral of @var{x},
1781 rounded in the direction @var{rnd}.
1782 For positive @var{x},
1783 the exponential integral is the sum of Euler's constant, of the logarithm
1784 of @var{x}, and of the sum for k from 1 to infinity of
1785 @ifnottex
1786 @var{x} to the power k, divided by k and factorial(k).
1787 @end ifnottex
1788 @tex
1789 $x^k/k/k!$.
1790 @end tex
1791 For negative @var{x}, the returned value is NaN.
1792 @end deftypefun
1793
1794 @deftypefun int mpfr_li2 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd_mode})
1795 Set @var{rop} to real part of the dilogarithm of @var{op}, rounded in the
1796 direction @var{rnd_mode}. The dilogarithm function is defined here as
1797 @m{-\int_{t=0}^x log(1-t)/t dt,the integral of -log(1-t)/t from 0 to x}.
1798 @end deftypefun
1799
1800 @deftypefun int mpfr_gamma (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1801 Set @var{rop} to the value of the Gamma function on @var{op}, rounded in the
1802 direction @var{rnd}. When @var{op} is a negative integer, NaN is returned.
1803 @end deftypefun
1804
1805 @deftypefun int mpfr_lngamma (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1806 Set @var{rop} to the value of the logarithm of the Gamma function on @var{op},
1807 rounded in the direction @var{rnd}.
1808 When @math{@minus{}2@var{k}@minus{}1 @le{} @var{x} @le{} @minus{}2@var{k}},
1809 @var{k} being a non-negative integer, NaN is returned.
1810 See also @code{mpfr_lgamma}.
1811 @end deftypefun
1812
1813 @deftypefun int mpfr_lgamma (mpfr_t @var{rop}, int *@var{signp}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1814 Set @var{rop} to the value of the logarithm of the absolute value of the
1815 Gamma function on @var{op}, rounded in the direction @var{rnd}. The sign
1816 (1 or @minus{}1) of Gamma(@var{op}) is returned in the object pointed to
1817 by @var{signp}. When @var{op} is an infinity or a non-positive integer,
1818 +Inf is returned. When @var{op} is NaN, @minus{}Inf or a negative integer,
1819 *@var{signp} is undefined, and when @var{op} is @pom{}0, *@var{signp} is
1820 the sign of the zero.
1821 @end deftypefun
1822
1823 @deftypefun int mpfr_zeta (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1824 @deftypefunx int mpfr_zeta_ui (mpfr_t @var{rop}, unsigned long @var{op}, mp_rnd_t @var{rnd})
1825 Set @var{rop} to the value of the Riemann Zeta function on @var{op},
1826 rounded in the direction @var{rnd}.
1827 @end deftypefun
1828
1829 @deftypefun int mpfr_erf (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1830 Set @var{rop} to the value of the error function on @var{op},
1831 rounded in the direction @var{rnd}.
1832 @end deftypefun
1833
1834 @deftypefun int mpfr_erfc (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1835 Set @var{rop} to the value of the complementary error function on @var{op},
1836 rounded in the direction @var{rnd}.
1837 @end deftypefun
1838
1839 @deftypefun int mpfr_j0 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1840 @deftypefunx int mpfr_j1 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1841 @deftypefunx int mpfr_jn (mpfr_t @var{rop}, long @var{n}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1842 Set @var{rop} to the value of the first kind Bessel function of order 0, 1
1843 and @var{n} on @var{op}, rounded in the direction @var{rnd}. When @var{op} is
1844 NaN, @var{rop} is always set to NaN. When @var{op} is plus or minus Infinity,
1845 @var{rop} is set to +0. When @var{op} is zero, and @var{n} is not zero,
1846 @var{rop} is +0 or @minus{}0 depending on the parity and sign of @var{n},
1847 and the sign of @var{op}.
1848 @end deftypefun
1849
1850 @deftypefun int mpfr_y0 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1851 @deftypefunx int mpfr_y1 (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1852 @deftypefunx int mpfr_yn (mpfr_t @var{rop}, long @var{n}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1853 Set @var{rop} to the value of the second kind Bessel function of order 0, 1
1854 and @var{n} on @var{op}, rounded in the direction @var{rnd}. When @var{op} is
1855 NaN or negative,
1856 @var{rop} is always set to NaN. When @var{op} is +Inf,
1857 @var{rop} is +0. When @var{op} is zero,
1858 @var{rop} is +Inf or @minus{}Inf depending on the parity and sign of @var{n}.
1859 @end deftypefun
1860
1861 @deftypefun int mpfr_fma (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mpfr_t @var{op3}, mp_rnd_t @var{rnd})
1862 Set @var{rop} to @math{(@var{op1} @GMPtimes{} @var{op2}) + @var{op3}},
1863 rounded in the direction @var{rnd}.
1864 @end deftypefun
1865
1866 @deftypefun int mpfr_fms (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mpfr_t @var{op3}, mp_rnd_t @var{rnd})
1867 Set @var{rop} to @math{(@var{op1} @GMPtimes{} @var{op2}) - @var{op3}},
1868 rounded in the direction @var{rnd}.
1869 @end deftypefun
1870
1871 @deftypefun int mpfr_agm (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
1872 Set @var{rop} to the arithmetic-geometric mean of @var{op1} and @var{op2},
1873 rounded in the direction @var{rnd}.
1874 The arithmetic-geometric mean is the common limit of the sequences
1875 u[n] and v[n], where u[0]=@var{op1}, v[0]=@var{op2}, u[n+1] is the
1876 arithmetic mean of u[n] and v[n], and v[n+1] is the geometric mean of
1877 u[n] and v[n].
1878 If any operand is negative, the return value is NaN.
1879 @end deftypefun
1880
1881 @deftypefun int mpfr_hypot (mpfr_t @var{rop}, mpfr_t @var{x}, mpfr_t @var{y}, mp_rnd_t @var{rnd})
1882 Set @var{rop} to the Euclidean norm of @var{x} and @var{y},
1883 @ifnottex
1884 i.e.@: the square root of the sum of the squares of @var{x} and @var{y},
1885 @end ifnottex
1886 @tex
1887 i.e.@: $\sqrt{x^2+y^2}$,
1888 @end tex
1889 rounded in the direction @var{rnd}.
1890 Special values are currently handled as described in Section F.9.4.3 of
1891 the ISO C99 standard, for the @code{hypot} function (note this may change
1892 in future versions): If @var{x} or @var{y} is an infinity, then plus
1893 infinity is returned in @var{rop}, even if the other number is NaN.
1894 @end deftypefun
1895
1896 @deftypefun int mpfr_const_log2 (mpfr_t @var{rop}, mp_rnd_t @var{rnd})
1897 @deftypefunx int mpfr_const_pi (mpfr_t @var{rop}, mp_rnd_t @var{rnd})
1898 @deftypefunx int mpfr_const_euler (mpfr_t @var{rop}, mp_rnd_t @var{rnd})
1899 @deftypefunx int mpfr_const_catalan (mpfr_t @var{rop}, mp_rnd_t @var{rnd})
1900 Set @var{rop} to the logarithm of 2, the value of @m{\pi,Pi},
1901 of Euler's constant 0.577@dots{}, of Catalan's constant 0.915@dots{},
1902 respectively, rounded in the direction
1903 @var{rnd}. These functions cache the computed values to avoid other
1904 calculations if a lower or equal precision is requested. To free these caches,
1905 use @code{mpfr_free_cache}.
1906 @end deftypefun
1907
1908 @deftypefun void mpfr_free_cache (void)
1909 Free various caches used by MPFR internally, in particular the
1910 caches used by the functions computing constants (currently
1911 @code{mpfr_const_log2}, @code{mpfr_const_pi},
1912 @code{mpfr_const_euler} and @code{mpfr_const_catalan}).
1913 You should call this function before terminating a thread, even if you did
1914 not call these functions directly (they could have been called internally).
1915 @end deftypefun
1916
1917 @deftypefun int mpfr_sum (mpfr_t @var{rop}, mpfr_ptr const @var{tab}[], unsigned long @var{n}, mp_rnd_t @var{rnd})
1918 Set @var{rop} to the sum of all elements of @var{tab}, whose size is @var{n},
1919 rounded in the direction @var{rnd}. Warning, @var{tab} is a table of pointers
1920 to mpfr_t, not a table of mpfr_t (preliminary interface). If the returned
1921 @code{int} value is zero, @var{rop} is guaranteed to be the exact
1922 sum; otherwise @var{rop} might be smaller than, equal to, or larger than
1923 the exact sum (in accordance to the rounding mode).
1924 However, @code{mpfr_sum} does guarantee the result is correctly rounded.
1925 @end deftypefun
1926
1927 @node Input and Output Functions, Formatted Output Functions, Special Functions, MPFR Interface
1928 @comment  node-name,  next,  previous,  up
1929 @cindex Float input and output functions
1930 @cindex Input functions
1931 @cindex Output functions
1932 @cindex I/O functions
1933 @section Input and Output Functions
1934
1935 This section describes functions that perform input from an input/output
1936 stream, and functions that output to an input/output stream.
1937 Passing a null pointer for a @code{stream} to any of these functions will make
1938 them read from @code{stdin} and write to @code{stdout}, respectively.
1939
1940 When using any of these functions, you must include the @code{<stdio.h>}
1941 standard header before @file{mpfr.h}, to allow @file{mpfr.h} to define
1942 prototypes for these functions.
1943
1944 @deftypefun size_t mpfr_out_str (FILE *@var{stream}, int @var{base}, size_t @var{n}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
1945 Output @var{op} on stream @var{stream}, as a string of digits in
1946 base @var{base}, rounded in the direction @var{rnd}.
1947 The base may vary from 2 to 36.  Print @var{n} significant digits exactly,
1948 or if @var{n} is 0, enough digits so that @var{op} can be read back
1949 exactly (see @code{mpfr_get_str}).
1950
1951 In addition to the significant digits, a decimal point (defined by the
1952 current locale) at the right of the
1953 first digit and a trailing exponent in base 10, in the form @samp{eNNN},
1954 are printed. If @var{base} is greater than 10, @samp{@@} will be used
1955 instead of @samp{e} as exponent delimiter.
1956
1957 Return the number of bytes written, or if an error occurred, return 0.
1958 @end deftypefun
1959
1960 @deftypefun size_t mpfr_inp_str (mpfr_t @var{rop}, FILE *@var{stream}, int @var{base}, mp_rnd_t @var{rnd})
1961 Input a string in base @var{base} from stream @var{stream},
1962 rounded in the direction @var{rnd}, and put the
1963 read float in @var{rop}.
1964 @c The argument @var{base} must be in the range 2 to 36.
1965
1966 @c The string is of the form @samp{M@@N} or, if the
1967 @c base is 10 or less, alternatively @samp{MeN} or @samp{MEN}, or, if the base
1968 @c is 16, alternatively @samp{MpB} or @samp{MPB}.
1969 @c @samp{M} is the significand in the specified base, @samp{N} is the exponent
1970 @c written in decimal for the specified base, and in base 16, @samp{B} is the
1971 @c binary exponent written in decimal (i.e.@: it indicates the power of 2 by
1972 @c which the significand is to be scaled).
1973 This function reads a word (defined as a sequence of characters between
1974 whitespace) and parses it using @code{mpfr_set_str} (it may change).
1975 See the documentation of @code{mpfr_strtofr} for a detailed description
1976 of the valid string formats.
1977 @c Special values can be read as follows (the case does not matter):
1978 @c @code{@@NaN@@}, @code{@@Inf@@}, @code{+@@Inf@@} and @code{-@@Inf@@},
1979 @c possibly followed by other characters; if the base is smaller or equal
1980 @c to 16, the following strings are accepted too: @code{NaN}, @code{Inf},
1981 @c @code{+Inf} and @code{-Inf}.
1982
1983 Return the number of bytes read, or if an error occurred, return 0.
1984 @end deftypefun
1985
1986 @c @deftypefun void mpfr_inp_raw (mpfr_t @var{float}, FILE *@var{stream})
1987 @c Input from stdio stream @var{stream} in the format written by
1988 @c @code{mpfr_out_raw}, and put the result in @var{float}.
1989 @c @end deftypefun
1990
1991 @node Formatted Output Functions, Integer Related Functions, Input and Output Functions, MPFR Interface
1992 @comment  node-name,  next,  previous,  up
1993 @cindex Float output functions
1994 @cindex Output functions
1995 @cindex I/O functions
1996 @section Formatted Output Functions
1997
1998 @subsection Requirements
1999 The class of @code{mpfr_printf} functions provides formatted output in a
2000 similar manner as the standard C @code{printf}. These functions are defined
2001 only if your system supports ISO C variadic functions and the corresponding
2002 argument access macros.
2003
2004 When using any of these functions, you must include the @code{<stdio.h>}
2005 standard header before @file{mpfr.h}, to allow @file{mpfr.h} to define
2006 prototypes for these functions.
2007
2008 @subsection Format String
2009 The format specification accepted by @code{mpfr_printf} is an extension of the
2010 @code{printf} one. The conversion specification is of the form:
2011 @example
2012 % [flags] [width] [.[precision]] [type] [rounding] conv
2013 @end example
2014 @samp{flags}, @samp{width}, and @samp{precision} have the same meaning as for
2015 the standard @code{printf} (in particular, notice that the @samp{precision} is
2016 related to the number of digits displayed in the base chosen by @samp{conv}
2017 and not related to the internal precision of the @code{mpfr_t} variable).
2018 @code{mpfr_printf} accepts the same @samp{type} specifiers as GMP (except the
2019 non-standard and deprecated @samp{q}, use @samp{ll} instead), plus @samp{R}
2020 and @samp{P}:
2021
2022 @quotation
2023 @multitable {(space)} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
2024 @item @samp{h}  @tab @code{short}
2025 @item @samp{hh} @tab @code{char}
2026 @item @samp{j}  @tab @code{intmax_t} or @code{uintmax_t}
2027 @item @samp{l}  @tab @code{long} or @code{wchar_t}
2028 @item @samp{ll} @tab @code{long long}
2029 @item @samp{L}  @tab @code{long double}
2030 @item @samp{t}  @tab @code{ptrdiff_t}
2031 @item @samp{z}  @tab @code{size_t}
2032
2033 @item @samp{F}  @tab @code{mpf_t}, float conversions
2034 @item @samp{Q}  @tab @code{mpq_t}, integer conversions
2035 @item @samp{M}  @tab @code{mp_limb_t}, integer conversions
2036 @item @samp{N}  @tab @code{mp_limb_t} array, integer conversions
2037 @item @samp{Z}  @tab @code{mpz_t}, integer conversions
2038
2039 @item @samp{P}  @tab @code{mp_prec_t}, integer conversions
2040 @item @samp{R}  @tab @code{mpfr_t}, float conversions
2041 @end multitable
2042 @end quotation
2043
2044 The @samp{type} specifiers have the same restrictions as those
2045 mentioned in the GMP documentation:
2046 @ifinfo
2047 @pxref{Formatted Output Strings,,, gmp.info,GNU MP}.
2048 @end ifinfo
2049 @ifnotinfo
2050 see Section ``Formatted Output Strings'' in @cite{GNU MP}.
2051 @end ifnotinfo
2052 In particular, the @samp{type} specifiers (except @samp{R} and @samp{P}
2053 defined by MPFR) are supported only if they are supported by @code{gmp_printf}
2054 in your GMP build; this implies that the standard specifiers, such as
2055 @samp{t}, must @emph{also} be supported by your C library if you want to use
2056 them.
2057
2058 The @samp{rounding} field is specific to @code{mpfr_t} arguments and should
2059 not be used with other types.
2060
2061 With conversion specification not involving @samp{P} and @samp{R} types,
2062 @code{mpfr_printf} behaves exactly as @code{gmp_printf}.
2063
2064 The @samp{P} type specifies that a following @samp{o}, @samp{u}, @samp{x}, or
2065 @samp{X} conversion specifier applies to a @code{mp_prec_t} argument.
2066 It is needed because the @code{mp_prec_t} type does not necessarily
2067 correspond to an @code{unsigned int} or any fixed standard type.
2068 The @samp{precision} field specifies the minimum number of digits to
2069 appear. The default @samp{precision} is 1.
2070 For example:
2071 @example
2072 mpfr_t x;
2073 mp_prec_t p;
2074 mpfr_init (x);
2075 @dots{}
2076 p = mpfr_get_prec (x);
2077 mpfr_printf ("variable x with %Pu bits", p);
2078 @end example
2079
2080 The @samp{R} type specifies that a following @samp{a}, @samp{A}, @samp{b},
2081 @samp{e}, @samp{E}, @samp{f}, @samp{F}, @samp{g}, @samp{G}, or @samp{n}
2082 conversion specifier applies to a @code{mpfr_t} argument.
2083 The @samp{R} type can be followed by a @samp{rounding} specifier denoted by
2084 one of the following characters:
2085
2086 @quotation
2087 @multitable {(space)} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
2088 @item @samp{U}  @tab round toward plus infinity
2089 @item @samp{D}  @tab round toward minus infinity
2090 @item @samp{Z}  @tab round toward zero
2091 @item @samp{N}  @tab round to nearest
2092 @item @samp{*}  @tab rounding mode indicated by the @code{mp_rnd_t} argument
2093 just before the corresponding @code{mpfr_t} variable.
2094 @end multitable
2095 @end quotation
2096
2097 The default rounding mode is rounding to nearest.
2098 The following three examples are equivalent:
2099 @example
2100 mpfr_t x;
2101 mpfr_init (x);
2102 @dots{}
2103 mpfr_printf ("%.128Rf", x);
2104 mpfr_printf ("%.128RNf", x);
2105 mpfr_printf ("%.128R*f", GMP_RNDN, x);
2106 @end example
2107
2108 The output @samp{conv} specifiers allowed with @code{mpfr_t} parameter are:
2109
2110 @quotation
2111 @multitable {(space)} {MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM}
2112 @item @samp{a} @samp{A} @tab hex float, C99 style
2113 @item @samp{b}          @tab binary output
2114 @item @samp{e} @samp{E} @tab scientific format float
2115 @item @samp{f} @samp{F} @tab fixed point float
2116 @item @samp{g} @samp{G} @tab fixed or scientific float
2117 @end multitable
2118 @end quotation
2119
2120 The conversion specifier @samp{b} which displays the argument in binary is
2121 specific to @code{mpfr_t} arguments and should not be used with other types.
2122 Other conversion specifiers have the same meaning as for a @code{double}
2123 argument.
2124
2125 In case of non-decimal output, only the significand is written in the
2126 specified base, the exponent is always displayed in decimal.
2127 Special values are always displayed as @code{nan}, @code{-inf}, and @code{inf}
2128 for @samp{a}, @samp{b}, @samp{e}, @samp{f}, and @samp{g} specifiers and
2129 @code{NAN}, @code{-INF}, and @code{INF} for @samp{A}, @samp{E}, @samp{F}, and
2130 @samp{G} specifiers.
2131
2132 If the @samp{precision} field is not empty, the @code{mpfr_t} number is
2133 rounded to the given precision in the direction specified by the rounding
2134 mode.
2135 If the precision is zero with rounding to nearest mode and one of the
2136 following @samp{conv} specifier: @samp{a}, @samp{A}, @samp{b}, @samp{e},
2137 @samp{E}, tie case is rounded to even when it lies between two values at the
2138 wanted precision which have the same exponent, otherwise, it is rounded away
2139 from zero.
2140 For instance, 85 is displayed as "8e+1" and 95 is displayed as "1e+2" with the
2141 format specification @code{"%.0RNe"}.
2142 This also applies when the @samp{g} (resp. @samp{G}) conversion specifier uses
2143 the @samp{e} (resp. @samp{E}) style.
2144 If the precision is set to a value greater than the maximum value for an
2145 @code{int}, it will be silently reduced down to @code{INT_MAX}.
2146
2147 If the @samp{precision} field is empty (as in @code{%Re} or @code{%.Re}) with
2148 @samp{conv} specifier @samp{e} and @samp{E}, the number is displayed with
2149 enough digits so that it can be read back exactly, assuming that the input and
2150 output variables have the same precision and that the input and output
2151 rounding modes are both rounding to nearest (as for @code{mpfr_get_str}).
2152 The default precision for an empty @samp{precision} field with @samp{conv}
2153 specifiers @samp{f}, @samp{F}, @samp{g}, and @samp{G} is 6.
2154
2155
2156 @subsection Functions
2157 @deftypefun int mpfr_fprintf (FILE *@var{stream}, const char *@var{template}, @dots{})
2158 @deftypefunx int mpfr_vfprintf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
2159 Print to the stream @var{stream} the optional arguments under the control of
2160 the template string @var{template}.
2161
2162 Return the number of characters written or a negative value if an error
2163 occurred. If the number of characters which ought to be written appears
2164 to exceed the maximum limit for an @code{int}, nothing is written in the
2165 stream, the function returns @minus{}1, sets the @emph{erange} flag, and (in
2166 POSIX system only) @code{errno} is set to @code{EOVERFLOW}.
2167 @end deftypefun
2168
2169 @deftypefun  int mpfr_printf (const char *@var{template}, @dots{})
2170 @deftypefunx int mpfr_vprintf (const char *@var{template}, va_list @var{ap})
2171 Print to @code{stdout} the optional arguments under the control of the
2172 template string @var{template}.
2173
2174 Return the number of characters written or a negative value if an error
2175 occurred. If the number of characters which ought to be written appears
2176 to exceed the maximum limit for an @code{int}, nothing is written in
2177 @code{stdout}, the function returns @minus{}1, sets the @emph{erange} flag,
2178 and (in POSIX system only) @code{errno} is set to @code{EOVERFLOW}.
2179 @end deftypefun
2180
2181 @deftypefun int mpfr_sprintf (char *@var{buf}, const char *@var{template}, @dots{})
2182 @deftypefunx int mpfr_vsprintf (char *@var{buf}, const char *@var{template}, va_list @var{ap})
2183 Form a null-terminated string in @var{buf}. No overlap is permitted between
2184 @var{buf} and the other arguments.
2185
2186 Return the number of characters written in the array @var{buf} not counting
2187 the terminating null character or a negative value if an error occurred. If
2188 the number of characters which ought to be written appears to exceed the
2189 maximum limit for an @code{int}, nothing is written in @var{buf}, the function
2190 returns @minus{}1, sets the @emph{erange} flag, and (in POSIX system only)
2191 @code{errno} is set to @code{EOVERFLOW}.
2192 @end deftypefun
2193
2194 @deftypefun int mpfr_snprintf (char *@var{buf}, size_t @var{n}, const char *@var{template}, @dots{})
2195 @deftypefunx int mpfr_vsnprintf (char *@var{buf}, size_t @var{n}, const char *@var{template}, va_list @var{ap})
2196 Form a null-terminated string in @var{buf}. If @var{n} is zero, nothing is
2197 written and @var{buf} may be a null pointer, otherwise, the @code{n-1} first
2198 characters are written in @var{buf} and the @var{n}-th is a null character.
2199
2200 Return the number of characters that would have been written had @var{n} be
2201 sufficiently large, not counting the terminating null character or a negative
2202 value if an error occurred. If the number of characters produced by the
2203 optional arguments under the control of the template string @var{template}
2204 appears to exceed the maximum limit for an @code{int}, nothing is written in
2205 @var{buf}, the function returns @minus{}1, sets the @emph{erange} flag, and
2206 (in POSIX system only) @code{errno} is set to @code{EOVERFLOW}.
2207 @end deftypefun
2208
2209 @deftypefun int mpfr_asprintf (char **@var{str}, const char *@var{template}, @dots{})
2210 @deftypefunx int mpfr_vasprintf (char **@var{str}, const char *@var{template}, va_list @var{ap})
2211 Write their output as a null terminated string in a block of memory allocated
2212 using the current allocation function. A pointer to the block is stored in
2213 @var{str}. The block of memory must be freed using @code{mpfr_free_str}.
2214
2215 The return value is the number of characters written in the string, excluding
2216 the null-terminator or a negative value if an error occurred. If the number of
2217 characters produced by the optional arguments under the control of the
2218 template string @var{template} appears to exceed the maximum limit for an
2219 @code{int}, @var{str} is a null pointer, the function returns @minus{}1, sets
2220 the @emph{erange} flag, and (in POSIX system only) @code{errno} is set to
2221 @code{EOVERFLOW}.
2222 @end deftypefun
2223
2224 @node Integer Related Functions, Rounding Related Functions, Formatted Output Functions, MPFR Interface
2225 @comment  node-name,  next,  previous,  up
2226 @cindex Integer related functions
2227 @section Integer and Remainder Related Functions
2228
2229 @deftypefun int mpfr_rint (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2230 @deftypefunx int mpfr_ceil (mpfr_t @var{rop}, mpfr_t @var{op})
2231 @deftypefunx int mpfr_floor (mpfr_t @var{rop}, mpfr_t @var{op})
2232 @deftypefunx int mpfr_round (mpfr_t @var{rop}, mpfr_t @var{op})
2233 @deftypefunx int mpfr_trunc (mpfr_t @var{rop}, mpfr_t @var{op})
2234 Set @var{rop} to @var{op} rounded to an integer.
2235 @code{mpfr_rint} rounds to the nearest representable integer in the
2236 given rounding mode, @code{mpfr_ceil} rounds
2237 to the next higher or equal representable integer, @code{mpfr_floor} to
2238 the next lower or equal representable integer, @code{mpfr_round} to the
2239 nearest representable integer, rounding halfway cases away from zero
2240 (as in the roundTiesToAway mode of IEEE 754-2008),
2241 and @code{mpfr_trunc} to the next representable integer toward zero.
2242
2243 The returned value is zero when the result is exact, positive when it is
2244 greater than the original value of @var{op}, and negative when it is smaller.
2245 More precisely, the returned value is 0 when @var{op} is an integer
2246 representable in @var{rop}, 1 or @minus{}1 when @var{op} is an integer
2247 that is not representable in @var{rop}, 2 or @minus{}2 when @var{op} is
2248 not an integer.
2249
2250 Note that @code{mpfr_round} is different from @code{mpfr_rint} called with
2251 the rounding to nearest mode (where halfway cases are rounded to an even
2252 integer or significand). Note also that no double rounding is performed; for
2253 instance, 4.5 (100.1 in binary) is rounded by @code{mpfr_round} to 4 (100
2254 in binary) in 2-bit precision, though @code{round(4.5)} is equal to 5 and
2255 5 (101 in binary) is rounded to 6 (110 in binary) in 2-bit precision.
2256 @end deftypefun
2257
2258 @deftypefun int mpfr_rint_ceil (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2259 @deftypefunx int mpfr_rint_floor (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2260 @deftypefunx int mpfr_rint_round (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2261 @deftypefunx int mpfr_rint_trunc (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2262 Set @var{rop} to @var{op} rounded to an integer.
2263 @code{mpfr_rint_ceil} rounds to the next higher or equal integer,
2264 @code{mpfr_rint_floor} to the next lower or equal integer,
2265 @code{mpfr_rint_round} to the nearest integer, rounding halfway cases away
2266 from zero, and @code{mpfr_rint_trunc} to the next integer toward zero.
2267 If the result is not representable, it is rounded in the direction @var{rnd}.
2268 The returned value is the ternary value associated with the considered
2269 round-to-integer function (regarded in the same way as any other
2270 mathematical function).
2271 @end deftypefun
2272
2273 @deftypefun int mpfr_frac (mpfr_t @var{rop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2274 Set @var{rop} to the fractional part of @var{op}, having the same sign as
2275 @var{op}, rounded in the direction @var{rnd} (unlike in @code{mpfr_rint},
2276 @var{rnd} affects only how the exact fractional part is rounded, not how
2277 the fractional part is generated).
2278 @end deftypefun
2279
2280 @deftypefun int mpfr_modf (mpfr_t @var{iop}, mpfr_t @var{fop}, mpfr_t @var{op}, mp_rnd_t @var{rnd})
2281 Set simultaneously @var{iop} to the integral part of @var{op} and @var{fop} to
2282 the fractional part of @var{op}, rounded in the direction @var{rnd} with the
2283 corresponding precision of @var{iop} and @var{fop} (equivalent to
2284 @code{mpfr_trunc(@var{iop}, @var{op}, @var{rnd})} and
2285 @code{mpfr_frac(@var{fop}, @var{op}, @var{rnd})}). The variables @var{iop} and
2286 @var{fop} must be different. Return 0 iff both results are exact.
2287 @end deftypefun
2288
2289 @deftypefun int mpfr_fmod (mpfr_t @var{r}, mpfr_t @var{x}, mpfr_t @var{y}, mp_rnd_t @var{rnd})
2290 @deftypefunx int mpfr_remainder (mpfr_t @var{r}, mpfr_t @var{x}, mpfr_t @var{y}, mp_rnd_t @var{rnd})
2291 @deftypefunx int mpfr_remquo (mpfr_t @var{r}, long* @var{q}, mpfr_t @var{x}, mpfr_t @var{y}, mp_rnd_t @var{rnd})
2292 Set @var{r} to the value of @math{x - n y}, rounded according to the direction
2293 @var{rnd}, where @math{n} is the integer quotient of @var{x} divided
2294 by @var{y}, defined as follows: @math{n} is rounded toward zero for
2295 @code{mpfr_fmod}, and to the nearest integer (ties rounded to even)
2296 for @code{mpfr_remainder} and @code{mpfr_remquo}.
2297
2298 Special values are handled as described in Section F.9.7.1 of
2299 the ISO C99 standard:
2300 If @var{x} is infinite or @var{y} is zero, @var{r} is NaN.
2301 If @var{y} is infinite and @var{x} is finite, @var{r} is @var{x} rounded
2302 to the precision of @var{r}.
2303 If @var{r} is zero, it has the sign of @var{x}.
2304 The return value is the ternary value corresponding to @var{r}.
2305
2306 Additionally, @code{mpfr_remquo} stores
2307 the low significant bits from the quotient in @var{*q}
2308 (more precisely the number of bits in a @code{long} minus one),
2309 with the sign of @var{x} divided by @var{y}
2310 (except if those low bits are all zero, in which case zero is returned).
2311 Note that @var{x} may be so large in magnitude relative to @var{y} that an
2312 exact representation of the quotient is not practical.
2313 @code{mpfr_remainder} and @code{mpfr_remquo} functions are useful for additive
2314 argument reduction.
2315 @end deftypefun
2316
2317 @deftypefun int mpfr_integer_p (mpfr_t @var{op})
2318 Return non-zero iff @var{op} is an integer.
2319 @end deftypefun
2320
2321 @node Rounding Related Functions, Miscellaneous Functions, Integer Related Functions, MPFR Interface
2322 @cindex Rounding mode related functions
2323 @section Rounding Related Functions
2324
2325 @deftypefun void mpfr_set_default_rounding_mode (mp_rnd_t @var{rnd})
2326 Set the default rounding mode to @var{rnd}.
2327 The default rounding mode is to nearest initially.
2328 @end deftypefun
2329
2330 @deftypefun mp_rnd_t mpfr_get_default_rounding_mode (void)
2331 Get the default rounding mode.
2332 @end deftypefun
2333
2334 @deftypefun int mpfr_prec_round (mpfr_t @var{x}, mp_prec_t @var{prec}, mp_rnd_t @var{rnd})
2335 Round @var{x} according to @var{rnd} with precision @var{prec}, which
2336 must be an integer between @code{MPFR_PREC_MIN} and @code{MPFR_PREC_MAX}
2337 (otherwise the behavior is undefined).
2338 If @var{prec} is greater or equal to the precision of @var{x}, then new
2339 space is allocated for the significand, and it is filled with zeros.
2340 Otherwise, the significand is rounded to precision @var{prec} with the given
2341 direction. In both cases, the precision of @var{x} is changed to @var{prec}.
2342 @end deftypefun
2343
2344 @deftypefun int mpfr_round_prec (mpfr_t @var{x}, mp_rnd_t @var{rnd}, mp_prec_t @var{prec})
2345 [This function is obsolete. Please use @code{mpfr_prec_round} instead.]
2346 @end deftypefun
2347
2348 @deftypefun int mpfr_can_round (mpfr_t @var{b}, mp_exp_t @var{err}, mp_rnd_t @var{rnd1}, mp_rnd_t @var{rnd2}, mp_prec_t @var{prec})
2349 Assuming @var{b} is an approximation of an unknown number
2350 @var{x} in the direction @var{rnd1} with error at most two to the power
2351 E(b)-@var{err} where E(b) is the exponent of @var{b}, return a non-zero
2352 value if one is able to round correctly @var{x} to precision
2353 @var{prec} with the direction @var{rnd2},
2354 and 0 otherwise (including for NaN and Inf).
2355 This function @strong{does not modify} its arguments.
2356
2357 Note: if one wants to also determine the correct ternary value when rounding
2358 @var{b} to precision @var{prec}, a useful trick is the following:
2359 @verbatim
2360    if (mpfr_can_round (b, err, rnd1, GMP_RNDZ, prec + (rnd2 == GMP_RNDN)))
2361       ...
2362 @end verbatim
2363 Indeed, if @var{rnd2} is @code{GMP_RNDN}, this will check if one can
2364 round to @var{prec}+1 bits with a directed rounding:
2365 if so, one can surely round to nearest to @var{prec} bits,
2366 and in addition one can determine the correct ternary value, which would not
2367 be the case when @var{b} is near from a value exactly representable on
2368 @var{prec} bits.
2369 @end deftypefun
2370
2371 @deftypefun {const char *} mpfr_print_rnd_mode (mp_rnd_t @var{rnd})
2372 Return the input string (GMP_RNDD, GMP_RNDU, GMP_RNDN, GMP_RNDZ)
2373 corresponding to the rounding mode @var{rnd} or a null pointer if
2374 @var{rnd} is an invalid rounding mode.
2375 @end deftypefun
2376
2377 @node Miscellaneous Functions, Exception Related Functions, Rounding Related Functions, MPFR Interface
2378 @comment  node-name,  next,  previous,  up
2379 @cindex Miscellaneous float functions
2380 @section Miscellaneous Functions
2381
2382 @deftypefun void mpfr_nexttoward (mpfr_t @var{x}, mpfr_t @var{y})
2383 If @var{x} or @var{y} is NaN, set @var{x} to NaN. Otherwise, if @var{x}
2384 is different from @var{y}, replace @var{x} by the next floating-point
2385 number (with the precision of @var{x} and the current exponent range)
2386 in the direction of @var{y}, if there is one
2387 (the infinite values are seen as the smallest and largest floating-point
2388 numbers). If the result is zero, it keeps the same sign. No underflow or
2389 overflow is generated.
2390 @end deftypefun
2391
2392 @deftypefun void mpfr_nextabove (mpfr_t @var{x})
2393 Equivalent to @code{mpfr_nexttoward} where @var{y} is plus infinity.
2394 @end deftypefun
2395
2396 @deftypefun void mpfr_nextbelow (mpfr_t @var{x})
2397 Equivalent to @code{mpfr_nexttoward} where @var{y} is minus infinity.
2398 @end deftypefun
2399
2400 @deftypefun int mpfr_min (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
2401 Set @var{rop} to the minimum of @var{op1} and @var{op2}. If @var{op1}
2402 and @var{op2} are both NaN, then @var{rop} is set to NaN. If @var{op1}
2403 or @var{op2} is NaN, then @var{rop} is set to the numeric value. If
2404 @var{op1} and @var{op2} are zeros of different signs, then @var{rop}
2405 is set to @minus{}0.
2406 @end deftypefun
2407
2408 @deftypefun int mpfr_max (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
2409 Set @var{rop} to the maximum of @var{op1} and @var{op2}. If @var{op1}
2410 and @var{op2} are both NaN, then @var{rop} is set to NaN. If @var{op1}
2411 or @var{op2} is NaN, then @var{rop} is set to the numeric value. If
2412 @var{op1} and @var{op2} are zeros of different signs, then @var{rop}
2413 is set to +0.
2414 @end deftypefun
2415
2416 @deftypefun int mpfr_urandomb (mpfr_t @var{rop}, gmp_randstate_t @var{state})
2417 Generate a uniformly distributed random float in the interval
2418 @math{0 @le{} @var{rop} < 1}. More precisely, the number can be seen as a
2419 float with a random non-normalized significand and exponent 0, which is then
2420 normalized (thus if @var{e} denotes the exponent after normalization, then
2421 the least @math{-@var{e}} significant bits of the significand are always 0).
2422 Return 0, unless the exponent is not in the current exponent range, in
2423 which case @var{rop} is set to NaN and a non-zero value is returned (this
2424 should never happen in practice, except in very specific cases). The
2425 second argument is a @code{gmp_randstate_t} structure which should be
2426 created using the GMP @code{gmp_randinit} function, see the GMP manual.
2427 @end deftypefun
2428
2429 @deftypefun void mpfr_random (mpfr_t @var{rop})
2430 Generate a uniformly distributed random float in the interval
2431 @math{0 @le{} @var{rop} < 1}.
2432
2433 This function is deprecated and will be suppressed in the next release;
2434 @code{mpfr_urandomb} should be used instead.
2435 @end deftypefun
2436
2437 @deftypefun void mpfr_random2 (mpfr_t @var{rop}, mp_size_t @var{size}, mp_exp_t @var{exp})
2438 Generate a random float of at most @var{size} limbs, with long strings of
2439 zeros and ones in the binary representation. The exponent of the number is in
2440 the interval @minus{}@var{exp} to @var{exp}.
2441 This function is useful for
2442 testing functions and algorithms, since this kind of random numbers have
2443 proven to be more likely to trigger corner-case bugs.
2444 Negative random numbers are generated when @var{size} is negative.
2445 Put +0 in @var{rop} when size if zero. The internal state of the default
2446 pseudorandom number generator is modified by a call to this function (the
2447 same one as GMP if MPFR was built using @samp{--with-gmp-build}).
2448
2449 This function is deprecated and will be suppressed in the next release.
2450 @end deftypefun
2451
2452 @deftypefun mp_exp_t mpfr_get_exp (mpfr_t @var{x})
2453 Get the exponent of @var{x}, assuming that @var{x} is a non-zero ordinary
2454 number and the significand is chosen in [1/2,1). The behavior for NaN,
2455 infinity or zero is undefined.
2456 @end deftypefun
2457
2458 @deftypefun int mpfr_set_exp (mpfr_t @var{x}, mp_exp_t @var{e})
2459 Set the exponent of @var{x} if @var{e} is in the current exponent range,
2460 and return 0 (even if @var{x} is not a non-zero ordinary number);
2461 otherwise, return a non-zero value.
2462 The significand is assumed to be in [1/2,1).
2463 @end deftypefun
2464
2465 @deftypefun int mpfr_signbit (mpfr_t @var{op})
2466 Return a non-zero value iff @var{op} has its sign bit set (i.e.@: if it is
2467 negative, @minus{}0, or a NaN whose representation has its sign bit set).
2468 @end deftypefun
2469
2470 @deftypefun int mpfr_setsign (mpfr_t @var{rop}, mpfr_t @var{op}, int @var{s}, mp_rnd_t @var{rnd})
2471 Set the value of @var{rop} from @var{op}, rounded toward the given
2472 direction @var{rnd}, then set (resp.@: clear) its sign bit if @var{s}
2473 is non-zero (resp.@: zero), even when @var{op} is a NaN.
2474 @end deftypefun
2475
2476 @deftypefun int mpfr_copysign (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
2477 Set the value of @var{rop} from @var{op1}, rounded toward the given
2478 direction @var{rnd}, then set its sign bit to that of @var{op2} (even
2479 when @var{op1} or @var{op2} is a NaN). This function is equivalent to
2480 @code{mpfr_setsign (@var{rop}, @var{op1}, mpfr_signbit (@var{op2}), @var{rnd})}.
2481 @end deftypefun
2482
2483 @c By definition, a C string is always null-terminated, so that we
2484 @c could just say "string" or "null-terminated character array",
2485 @c but "null-terminated string" is not an error and probably better
2486 @c for most users.
2487 @deftypefun {const char *} mpfr_get_version (void)
2488 Return the MPFR version, as a null-terminated string.
2489 @end deftypefun
2490
2491 @defmac MPFR_VERSION
2492 @defmacx MPFR_VERSION_MAJOR
2493 @defmacx MPFR_VERSION_MINOR
2494 @defmacx MPFR_VERSION_PATCHLEVEL
2495 @defmacx MPFR_VERSION_STRING
2496 @code{MPFR_VERSION} is the version of MPFR as a preprocessing constant.
2497 @code{MPFR_VERSION_MAJOR}, @code{MPFR_VERSION_MINOR} and
2498 @code{MPFR_VERSION_PATCHLEVEL} are respectively the major, minor and patch
2499 level of MPFR version, as preprocessing constants.
2500 @code{MPFR_VERSION_STRING} is the version (with an optional suffix, used
2501 in development and pre-release versions) as a string constant, which can
2502 be compared to the result of @code{mpfr_get_version} to check at run time
2503 the header file and library used match:
2504 @example
2505 if (strcmp (mpfr_get_version (), MPFR_VERSION_STRING))
2506   fprintf (stderr, "Warning: header and library do not match\n");
2507 @end example
2508 Note: Obtaining different strings is not necessarily an error, as
2509 in general, a program compiled with some old MPFR version can be
2510 dynamically linked with a newer MPFR library version (if allowed
2511 by the library versioning system).
2512 @end defmac
2513
2514 @deftypefn Macro long MPFR_VERSION_NUM (@var{major}, @var{minor}, @var{patchlevel})
2515 Create an integer in the same format as used by @code{MPFR_VERSION} from the
2516 given @var{major}, @var{minor} and @var{patchlevel}.
2517 Here is an example of how to check the MPFR version at compile time:
2518 @example
2519 #if (!defined(MPFR_VERSION) || (MPFR_VERSION<MPFR_VERSION_NUM(2,1,0)))
2520 # error "Wrong MPFR version."
2521 #endif
2522 @end example
2523 @end deftypefn
2524
2525 @deftypefun {const char *} mpfr_get_patches (void)
2526 Return a null-terminated string containing the ids of the patches applied to
2527 the MPFR library (contents of the @file{PATCHES} file), separated by spaces.
2528 Note: If the program has been compiled with an older MPFR version and is
2529 dynamically linked with a new MPFR library version, the ids of the patches
2530 applied to the old (compile-time) MPFR version are not available (however
2531 this information should not have much interest in general).
2532 @end deftypefun
2533
2534 @node Exception Related Functions, Compatibility with MPF, Miscellaneous Functions, MPFR Interface
2535 @comment  node-name,  next,  previous,  up
2536 @cindex Exception related functions
2537 @section Exception Related Functions
2538
2539 @deftypefun mp_exp_t mpfr_get_emin (void)
2540 @deftypefunx mp_exp_t mpfr_get_emax (void)
2541 Return the (current) smallest and largest exponents allowed for a
2542 floating-point variable. The smallest positive value of a floating-point
2543 variable is @m{1/2 \times 2^{\rm emin}, one half times 2 raised to the
2544 smallest exponent} and the largest value has the form @m{(1 - \varepsilon)
2545 \times 2^{\rm emax}, (1 - epsilon) times 2 raised to the largest exponent}.
2546 @end deftypefun
2547
2548 @deftypefun int mpfr_set_emin (mp_exp_t @var{exp})
2549 @deftypefunx int mpfr_set_emax (mp_exp_t @var{exp})
2550 Set the smallest and largest exponents allowed for a floating-point variable.
2551 Return a non-zero value when @var{exp} is not in the range accepted by the
2552 implementation (in that case the smallest or largest exponent is not changed),
2553 and zero otherwise.
2554 If the user changes the exponent range, it is her/his responsibility to check
2555 that all current floating-point variables are in the new allowed range
2556 (for example using @code{mpfr_check_range}), otherwise the subsequent
2557 behavior will be undefined, in the sense of the ISO C standard.
2558 @c It is also her/his responsibility to check that @m {emin <= emax}.
2559 @end deftypefun
2560
2561 @deftypefun mp_exp_t mpfr_get_emin_min (void)
2562 @deftypefunx mp_exp_t mpfr_get_emin_max (void)
2563 @deftypefunx mp_exp_t mpfr_get_emax_min (void)
2564 @deftypefunx mp_exp_t mpfr_get_emax_max (void)
2565 Return the minimum and maximum of the smallest and largest exponents
2566 allowed for @code{mpfr_set_emin} and @code{mpfr_set_emax}. These values
2567 are implementation dependent; it is possible to create a non
2568 portable program by writing @code{mpfr_set_emax(mpfr_get_emax_max())}
2569 and @code{mpfr_set_emin(mpfr_get_emin_min())} since the values
2570 of the smallest and largest exponents become implementation dependent.
2571 @end deftypefun
2572
2573 @deftypefun int mpfr_check_range (mpfr_t @var{x}, int @var{t}, mp_rnd_t @var{rnd})
2574 This function forces @var{x} to be in the current range of acceptable
2575 values, @var{t} being the current ternary value: negative if @var{x}
2576 is smaller than the exact value, positive if @var{x} is larger than
2577 the exact value and zero if @var{x} is exact (before the call). It
2578 generates an underflow or an overflow if the exponent of @var{x} is
2579 outside the current allowed range; the value of @var{t} may be used
2580 to avoid a double rounding. This function returns zero if the rounded
2581 result is equal to the exact one, a positive value if the rounded
2582 result is larger than the exact one, a negative value if the rounded
2583 result is smaller than the exact one. Note that unlike most functions,
2584 the result is compared to the exact one, not the input value @var{x},
2585 i.e.@: the ternary value is propagated.
2586
2587 Note: If @var{x} is an infinity and @var{t} is different from zero (i.e.,
2588 if the rounded result is an inexact infinity), then the overflow flag is
2589 set. This is useful because @code{mpfr_check_range} is typically called
2590 (at least in MPFR functions) after restoring the flags that could have
2591 been set due to internal computations.
2592 @end deftypefun
2593
2594 @deftypefun int mpfr_subnormalize (mpfr_t @var{x}, int @var{t}, mp_rnd_t @var{rnd})
2595 This function rounds @var{x} emulating subnormal number arithmetic:
2596 if @var{x} is outside the subnormal exponent range, it just propagates the
2597 ternary value @var{t}; otherwise, it rounds @var{x} to precision
2598 @code{EXP(x)-emin+1} according to rounding mode @var{rnd} and previous
2599 ternary value @var{t}, avoiding double rounding problems.
2600 More precisely in the subnormal domain, denoting by @var{e} the value of
2601 @code{emin}, @var{x} is rounded in fixed-point
2602 arithmetic to an integer multiple of @m{2^{e-1}, two to the power
2603 @var{e}@minus{}1}; as a consequence, @m{1.5 \times 2^{e-1},
2604 1.5 multiplied by two to the power @var{e}@minus{}1} when @var{t} is zero
2605 is rounded to @m{2^e, two to the power @var{e}} with rounding to nearest.
2606
2607 @code{PREC(x)} is not modified by this function.
2608 @var{rnd} and @var{t} must be the used rounding mode for computing @var{x}
2609 and the returned ternary value when computing @var{x}.
2610 The subnormal exponent range is from @code{emin} to @code{emin+PREC(x)-1}.
2611 If the result cannot be represented in the current exponent range
2612 (due to a too small @code{emax}), the behavior is undefined.
2613 Note that unlike most functions, the result is compared to the exact one,
2614 not the input value @var{x}, i.e.@: the ternary value is propagated.
2615 This is a preliminary interface.
2616 @end deftypefun
2617
2618 This is an example of how to emulate binary double IEEE 754 arithmetic
2619 (binary64 in IEEE 754-2008) using MPFR:
2620
2621 @example
2622 @{
2623   mpfr_t xa, xb;
2624   int i;
2625   volatile double a, b;
2626
2627   mpfr_set_default_prec (53);
2628   mpfr_set_emin (-1073);
2629   mpfr_set_emax (1024);
2630
2631   mpfr_init (xa); mpfr_init (xb);
2632
2633   b = 34.3; mpfr_set_d (xb, b, GMP_RNDN);
2634   a = 0x1.1235P-1021; mpfr_set_d (xa, a, GMP_RNDN);
2635
2636   a /= b;
2637   i = mpfr_div (xa, xa, xb, GMP_RNDN);
2638   i = mpfr_subnormalize (xa, i, GMP_RNDN);
2639
2640   mpfr_clear (xa); mpfr_clear (xb);
2641 @}
2642 @end example
2643
2644 Warning: this emulates a double IEEE 754 arithmetic with correct rounding
2645 in the subnormal range, which may not be the case for your hardware.
2646
2647 @deftypefun void mpfr_clear_underflow (void)
2648 @deftypefunx void mpfr_clear_overflow (void)
2649 @deftypefunx void mpfr_clear_nanflag (void)
2650 @deftypefunx void mpfr_clear_inexflag (void)
2651 @deftypefunx void mpfr_clear_erangeflag (void)
2652 Clear the underflow, overflow, invalid, inexact and @emph{erange} flags.
2653 @end deftypefun
2654
2655 @deftypefun void mpfr_set_underflow (void)
2656 @deftypefunx void mpfr_set_overflow (void)
2657 @deftypefunx void mpfr_set_nanflag (void)
2658 @deftypefunx void mpfr_set_inexflag (void)
2659 @deftypefunx void mpfr_set_erangeflag (void)
2660 Set the underflow, overflow, invalid, inexact and @emph{erange} flags.
2661 @end deftypefun
2662
2663 @deftypefun void mpfr_clear_flags (void)
2664 Clear all global flags (underflow, overflow, inexact, invalid, @emph{erange}).
2665 @end deftypefun
2666
2667 @deftypefun int mpfr_underflow_p (void)
2668 @deftypefunx int mpfr_overflow_p (void)
2669 @deftypefunx int mpfr_nanflag_p (void)
2670 @deftypefunx int mpfr_inexflag_p (void)
2671 @deftypefunx int mpfr_erangeflag_p (void)
2672 Return the corresponding (underflow, overflow, invalid, inexact, @emph{erange})
2673 flag, which is non-zero iff the flag is set.
2674 @end deftypefun
2675
2676 @node Compatibility with MPF, Custom Interface, Exception Related Functions, MPFR Interface
2677 @cindex Compatibility with MPF
2678 @section Compatibility With MPF
2679
2680 A header file @file{mpf2mpfr.h} is included in the distribution of MPFR for
2681 compatibility with the GNU MP class MPF.
2682 After inserting the following two lines after the @code{#include <gmp.h>}
2683 line,
2684 @verbatim
2685 #include <mpfr.h>
2686 #include <mpf2mpfr.h>
2687 @end verbatim
2688 @noindent
2689 any program written for
2690 MPF can be compiled directly with MPFR without any changes.
2691 All operations are then performed with the default MPFR rounding mode,
2692 which can be reset with @code{mpfr_set_default_rounding_mode}.
2693
2694 Warning: the @code{mpf_init} and @code{mpf_init2} functions initialize
2695 to zero, whereas the corresponding MPFR functions initialize to NaN:
2696 this is useful to detect uninitialized values, but is slightly incompatible
2697 with @code{mpf}.
2698
2699 @deftypefun void mpfr_set_prec_raw (mpfr_t @var{x}, mp_prec_t @var{prec})
2700 Reset the precision of @var{x} to be @strong{exactly} @var{prec} bits.
2701 The only difference with @code{mpfr_set_prec} is that @var{prec} is assumed to
2702 be small enough so that the significand fits into the current allocated memory
2703 space for @var{x}. Otherwise the behavior is undefined.
2704 @end deftypefun
2705
2706 @deftypefun int mpfr_eq (mpfr_t @var{op1}, mpfr_t @var{op2}, unsigned long int @var{op3})
2707 Return non-zero if @var{op1} and @var{op2} are both non-zero ordinary
2708 numbers with the same exponent and the same first @var{op3} bits, both
2709 zero, or both infinities of the same sign. Return zero otherwise. This
2710 function is defined for compatibility with @code{mpf}. Do not use it if
2711 you want to know whether two numbers are close to each other; for instance,
2712 1.011111 and 1.100000 are currently regarded as different for any value of
2713 @var{op3} larger than 1 (but this may change in the next release).
2714 @end deftypefun
2715
2716 @deftypefun void mpfr_reldiff (mpfr_t @var{rop}, mpfr_t @var{op1}, mpfr_t @var{op2}, mp_rnd_t @var{rnd})
2717 Compute the relative difference between @var{op1} and @var{op2}
2718 and store the result in @var{rop}.
2719 This function does not guarantee the correct rounding on the relative
2720 difference; it just computes @math{|@var{op1}-@var{op2}|/@var{op1}}, using the
2721 rounding mode @var{rnd} for all operations and the precision of @var{rop}.
2722 @end deftypefun
2723
2724 @deftypefun int mpfr_mul_2exp (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
2725 @deftypefunx int mpfr_div_2exp (mpfr_t @var{rop}, mpfr_t @var{op1}, unsigned long int @var{op2}, mp_rnd_t @var{rnd})
2726 See @code{mpfr_mul_2ui} and @code{mpfr_div_2ui}. These functions are only kept
2727 for compatibility with MPF.
2728 @end deftypefun
2729
2730
2731 @node Custom Interface, Internals, Compatibility with MPF, MPFR Interface
2732 @cindex Custom interface
2733 @section Custom Interface
2734
2735 Some applications use a stack to handle the memory and their objects.
2736 However, the MPFR memory design is not well suited for such a thing. So that
2737 such applications are able to use MPFR, an auxiliary memory interface has
2738 been created: the Custom Interface.
2739
2740 The following interface allows them to use MPFR in two ways:
2741 @itemize
2742 @item Either they directly store the MPFR FP number as a @code{mpfr_t}
2743 on the stack.
2744 @item Either they store their own representation of a FP number on the
2745 stack and construct a new temporary @code{mpfr_t} each time it is needed.
2746 @end itemize
2747 Nothing has to be done to destroy the FP numbers except garbaging the used
2748 memory: all the memory stuff (allocating, destroying, garbaging) is kept to
2749 the application.
2750
2751 Each function in this interface is also implemented as a macro for
2752 efficiency reasons: for example @code{mpfr_custom_init (s, p)}
2753 uses the macro, while @code{(mpfr_custom_init) (s, p)} uses the function.
2754
2755 Note 1: MPFR functions may still initialize temporary FP numbers
2756 using standard mpfr_init. See Custom Allocation (GNU MP).
2757
2758 Note 2: MPFR functions may use the cached functions (mpfr_const_pi for
2759 example), even if they are not explicitly called. You have to call
2760 @code{mpfr_free_cache} each time you garbage the memory iff mpfr_init, through
2761 GMP Custom Allocation, allocates its memory on the application stack.
2762
2763 Note 3: This interface is preliminary.
2764
2765 @deftypefun size_t mpfr_custom_get_size (mp_prec_t @var{prec})
2766 Return the needed size in bytes to store the significand of a FP number
2767 of precision @var{prec}.
2768 @end deftypefun
2769
2770 @deftypefun void mpfr_custom_init (void *@var{significand}, mp_prec_t @var{prec})
2771 Initialize a significand of precision @var{prec}.
2772 @var{significand} must be an area of @code{mpfr_custom_get_size (prec)} bytes
2773 at least and be suitably aligned for an array of @code{mp_limb_t}.
2774 @end deftypefun
2775
2776 @deftypefun void mpfr_custom_init_set (mpfr_t @var{x}, int @var{kind}, mp_exp_t @var{exp}, mp_prec_t @var{prec}, void *@var{significand})
2777 Perform a dummy initialization of a @code{mpfr_t} and set it to:
2778 @itemize
2779 @item if @code{ABS(kind) == MPFR_NAN_KIND}, @var{x} is set to NaN;
2780 @item if @code{ABS(kind) == MPFR_INF_KIND}, @var{x} is set to the infinity
2781 of sign @code{sign(kind)};
2782 @item if @code{ABS(kind) == MPFR_ZERO_KIND}, @var{x} is set to the zero of
2783 sign @code{sign(kind)};
2784 @item if @code{ABS(kind) == MPFR_REGULAR_KIND}, @var{x} is set to a regular
2785 number: @code{x = sign(kind)*significand*2^exp}
2786 @end itemize
2787 In all cases, it uses @var{significand} directly for further computing
2788 involving @var{x}. It will not allocate anything.
2789 A FP number initialized with this function cannot be resized using
2790 @code{mpfr_set_prec}, or cleared using @code{mpfr_clear}!
2791 @var{significand} must have been initialized with @code{mpfr_custom_init}
2792 using the same precision @var{prec}.
2793 @end deftypefun
2794
2795 @deftypefun int mpfr_custom_get_kind (mpfr_t @var{x})
2796 Return the current kind of a @code{mpfr_t} as used by
2797 @code{mpfr_custom_init_set}.
2798 The behavior of this function for any @code{mpfr_t} not initialized
2799 with @code{mpfr_custom_init_set} is undefined.
2800 @end deftypefun
2801
2802 @deftypefun {void *} mpfr_custom_get_mantissa (mpfr_t @var{x})
2803 Return a pointer to the significand used by a @code{mpfr_t} initialized with
2804 @code{mpfr_custom_init_set}.
2805 The behavior of this function for any @code{mpfr_t} not initialized
2806 with @code{mpfr_custom_init_set} is undefined.
2807 @end deftypefun
2808
2809 @deftypefun mp_exp_t mpfr_custom_get_exp (mpfr_t @var{x})
2810 Return the exponent of @var{x}, assuming that @var{x} is a non-zero ordinary
2811 number. The return value for NaN, Infinity or Zero is unspecified but does not
2812 produce any trap.
2813 The behavior of this function for any @code{mpfr_t} not initialized
2814 with @code{mpfr_custom_init_set} is undefined.
2815 @end deftypefun
2816
2817 @deftypefun void mpfr_custom_move (mpfr_t @var{x}, void *@var{new_position})
2818 Inform MPFR that the significand has moved due to a garbage collect
2819 and update its new position to @code{new_position}.
2820 However the application has to move the significand and the @code{mpfr_t}
2821 itself.
2822 The behavior of this function for any @code{mpfr_t} not initialized
2823 with @code{mpfr_custom_init_set} is undefined.
2824 @end deftypefun
2825
2826 See the test suite for examples.
2827
2828 @node Internals,  , Custom Interface, MPFR Interface
2829 @cindex Internals
2830 @section Internals
2831
2832 The following types and
2833 functions were mainly designed for the implementation of MPFR,
2834 but may be useful for users too.
2835 However no upward compatibility is guaranteed.
2836 You may need to include @file{mpfr-impl.h} to use them.
2837
2838 The @code{mpfr_t} type consists of four fields.
2839
2840 @itemize @bullet
2841
2842 @item The @code{_mpfr_prec} field is used to store the precision of
2843 the variable (in bits); this is not less than @code{MPFR_PREC_MIN}.
2844
2845 @item The @code{_mpfr_sign} field is used to store the sign of the variable.
2846
2847 @item The @code{_mpfr_exp} field stores the exponent.
2848 An exponent of 0 means a radix point just above the most significant
2849 limb.  Non-zero values @math{n} are a multiplier @math{2^n} relative to that
2850 point.
2851 A NaN, an infinity and a zero are indicated by a special value of the exponent.
2852
2853 @item Finally, the @code{_mpfr_d} is a pointer to the limbs, least
2854 significant limbs stored first.
2855 The number of limbs in use is controlled by @code{_mpfr_prec}, namely
2856 ceil(@code{_mpfr_prec}/@code{mp_bits_per_limb}).
2857 Non-singular values always have the most significant bit of the most
2858 significant limb set to 1.  When the precision does not correspond to a
2859 whole number of limbs, the excess bits at the low end of the data are zero.
2860
2861 @end itemize
2862
2863 @c The following function is not documented any more after mpfr-2.3.0
2864 @c @deftypefun double mpfr_get_d1 (mpfr_t @var{op})
2865 @c Convert @var{op} to a @code{double}, using the default MPFR rounding mode
2866 @c (see function @code{mpfr_set_default_rounding_mode}). This function is
2867 @c obsolete.
2868 @c @end deftypefun
2869
2870 @node Contributors, References, MPFR Interface, Top
2871 @comment  node-name,  next,  previous,  up
2872 @unnumbered Contributors
2873
2874 The main developers of MPFR are Guillaume Hanrot, Vincent Lef@`evre,
2875 Patrick P@'elissier, Philippe Th@'eveny and Paul Zimmermann.
2876
2877 Sylvie Boldo from ENS-Lyon, France,
2878 contributed the functions @code{mpfr_agm} and @code{mpfr_log}.
2879 Emmanuel Jeandel, from ENS-Lyon too,
2880 contributed the generic hypergeometric code,
2881 as well as the @code{mpfr_exp3},
2882 a first implementation of the sine and cosine,
2883 and improved versions of
2884 @code{mpfr_const_log2} and @code{mpfr_const_pi}.
2885 Mathieu Dutour contributed the functions @code{mpfr_atan} and @code{mpfr_asin},
2886 and a previous version of @code{mpfr_gamma};
2887 David Daney contributed the hyperbolic and inverse hyperbolic functions,
2888 the base-2 exponential, and the factorial function. Fabrice Rouillier
2889 contributed the original version of @file{mul_ui.c}, the @file{gmp_op.c}
2890 file, and helped to the Microsoft Windows porting.
2891 Jean-Luc R@'emy contributed the @code{mpfr_zeta} code.
2892 Ludovic Meunier helped in the design of the @code{mpfr_erf} code.
2893 Damien Stehl@'e contributed the @code{mpfr_get_ld_2exp} function.
2894
2895 We would like to thank Jean-Michel Muller and Joris van der Hoeven for very
2896 fruitful discussions at the beginning of that project, Torbj@"orn Granlund
2897 and Kevin Ryde for their help about design issues,
2898 and Nathalie Revol for her careful reading of a previous version of
2899 this documentation.
2900 Kevin Ryde did a tremendous job for the portability of MPFR in 2002-2004.
2901
2902 The development of the MPFR library would not have been possible without
2903 the continuous support of INRIA, and of the LORIA (Nancy, France) and LIP
2904 (Lyon, France) laboratories. In particular the main authors were or are
2905 members of the PolKA, Spaces, Cacao project-teams at LORIA and of the
2906 Arenaire project-team at LIP.
2907 This project was started during the Fiable (reliable in French) action
2908 supported by INRIA, and continued during the AOC action.
2909 The development of MPFR was also supported by a grant
2910 (202F0659 00 MPN 121) from the Conseil R@'egional de Lorraine in 2002,
2911 and from INRIA by an "associate engineer" grant (2003-2005)
2912 and an "op@'eration de d@'eveloppement logiciel" grant (2007-2009).
2913
2914 @node References, GNU Free Documentation License, Contributors, Top
2915 @comment  node-name,  next,  previous,  up
2916 @unnumbered References
2917
2918 @itemize @bullet
2919
2920 @item
2921 Laurent Fousse, Guillaume Hanrot, Vincent Lef@`evre,
2922 Patrick P@'elissier and Paul Zimmermann,
2923 "MPFR: A Multiple-Precision Binary Floating-Point Library With Correct Rounding",
2924 ACM Transactions on Mathematical Software,
2925 volume 33, issue 2, article 13, 15 pages, 2007,
2926 @url{http://doi.acm.org/10.1145/1236463.1236468}.
2927
2928 @item
2929 Torbj@"orn Granlund, "GNU MP: The GNU Multiple Precision Arithmetic Library",
2930   version 4.2.2, 2007, @url{http://gmplib.org}.
2931
2932 @item
2933 IEEE standard for binary floating-point arithmetic, Technical Report
2934 ANSI-IEEE Standard 754-1985, New York, 1985.
2935 Approved March 21, 1985: IEEE Standards Board; approved July 26,
2936   1985: American National Standards Institute, 18 pages.
2937
2938 @item
2939 IEEE Standard for Floating-Point Arithmetic,
2940 ANSI-IEEE Standard 754-2008, 2008.
2941 Revision of ANSI-IEEE Standard 754-1985,
2942 approved June 12, 2008: IEEE Standards Board, 70 pages.
2943
2944 @item
2945 Donald E. Knuth, "The Art of Computer Programming", vol 2,
2946 "Seminumerical Algorithms", 2nd edition, Addison-Wesley, 1981.
2947
2948 @item
2949 Jean-Michel Muller, "Elementary Functions, Algorithms and Implementation",
2950 Birkhauser, Boston, 2nd edition, 2006.
2951
2952 @end itemize
2953
2954
2955 @node GNU Free Documentation License, Concept Index, References, Top
2956 @appendix GNU Free Documentation License
2957 @cindex GNU Free Documentation License
2958 @include fdl.texi
2959
2960
2961 @node Concept Index, Function Index, GNU Free Documentation License, Top
2962 @comment  node-name,  next,  previous,  up
2963 @unnumbered Concept Index
2964 @printindex cp
2965
2966 @node Function Index,  , Concept Index, Top
2967 @comment  node-name,  next,  previous,  up
2968 @unnumbered Function and Type Index
2969 @printindex fn
2970
2971 @bye
2972
2973 @c Local variables:
2974 @c fill-column: 78
2975 @c End: