4 MPFR integration for SBCL
6 The MPFR library [1] provides arbitrary precision arithmetic on
7 floating-point numbers. It makes use of the GMP library and therefore
8 SB-MPFR requires SB-GMP [2]. Contrary to SB-GMP the SB-MPFR contrib does
9 not deeply integrate with any standard Common Lisp functions. However,
10 the SB-MPFR provides several function with equivalent names of
11 standard function and similar interface, e.g. SB-MPFR:COERCE,
17 The rounding mode for SB-MPFR is defined by the value of
18 *MPFR-RND*. Possible values are :MPFR_RNDNA, :MPFR_RNDN, :MPFR_RNDZ,
19 :MPFR_RNDU, :MPFR_RNDD, :MPFR_RNDA, :MPFR_RNDF which implement the
20 corresponding rounding modes from MPFR and are members of the type
26 MPFR allows to define the preicision of a computation or newly created
27 floating-points. This can either be set via the SB-MPFR:WITH-PRECISION
28 macro which takes the number of bits and an expression body as
29 arguments, or via SB-MPFR:SET-PRECISION which sets the precision until
32 Example, function to compute PI with 400 bits precision:
35 (sb-mpfr:set-precision 400)
39 .31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470e+1
42 (defun sample-pi-x (x)
43 (sb-mpfr:with-precision x
46 CL-USER> (sample-pi-x 42)
49 CL-USER> (sample-pi-x 142)
50 .31415926535897932384626433832795028841971697e+1
56 Almost every MPFR-related function returns a second value. This value
57 is of the ternary set {-1, 0, 1}. If the ternary value is zero, it
58 means that the value stored in the destination variable is the exact
59 result of the corresponding mathematical function. If the ternary
60 value is positive (resp. negative), it means the value stored in the
61 destination variable is greater (resp. lower) than the exact
62 result. The returned value depends on the current rounding mode. Refer
63 to [1] for further details.
69 Creating arbitrary precision floating points can be achieved in two
70 ways. First, by coercing a standard CL non-complex number with
73 CL-USER> (sb-mpfr:coerce 1.0d0 'sb-mpfr:mpfr-float)
75 CL-USER> (sb-mpfr:coerce 1/2 'sb-mpfr:mpfr-float)
78 The second method uses the ready maco #M:
82 CL-USER> (setf *print-readably* t)
85 #M"15000000000000000@-16"
90 The following functions, macros and symbols are exported from the SB-MPFR package:
92 ;; +mpfr-precision+ is a pseudo-constant
93 ;; Do not set via LET but use the two methods below!
97 ;; parameters and types
101 ;; arithmetic operations
103 #:mpfr-float-to-string
164 #:arithmetic-geometric-mean
169 ;; comparison functions and predicates
191 ;; miscellaneous functions
200 #:set-div-by-zero-flag
211 ;; random number generation
216 #:rounded-int-ceiling
219 #:rounded-int-truncate
237 [1] http://www.mpfr.org/mpfr-current/mpfr.html
238 [2] https://github.com/sfrank/sb-gmp