1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module float
)
15 ;; EXPERIMENTAL BIGFLOAT PACKAGE VERSION 2- USING BINARY MANTISSA
16 ;; AND POWER-OF-2 EXPONENT.
17 ;; EXPONENTS MAY BE BIG NUMBERS NOW (AUG. 1975 --RJF)
18 ;; Modified: July 1979 by CWH to run on the Lisp Machine and to comment
20 ;; August 1980 by CWH to run on Multics and to install
22 ;; December 1980 by JIM to fix BIGLSH not to pass LSH a second
23 ;; argument with magnitude greater than MACHINE-FIXNUM-PRECISION.
25 ;; Number of bits of precision in a fixnum and in the fields of a flonum for
26 ;; a particular machine. These variables should only be around at eval
27 ;; and compile time. These variables should probably be set up in a prelude
28 ;; file so they can be accessible to all Macsyma files.
31 #+gcl
(compile load eval
)
32 #-gcl
(:compile-toplevel
:load-toplevel
:execute
)
33 (defconstant +machine-fixnum-precision
+ (integer-length most-positive-fixnum
)))
38 "If TRUE, no MAXIMA-ERROR message is printed when a floating point number is
39 converted to a bigfloat number.")
42 "Controls the conversion of bigfloat numbers to rational numbers. If
43 FALSE, RATEPSILON will be used to control the conversion (this results in
44 relatively small rational numbers). If TRUE, the rational number generated
45 will accurately represent the bigfloat.")
48 "If TRUE, printing of bigfloat numbers will truncate trailing zeroes.
49 Otherwise, all trailing zeroes are printed.")
51 (defmvar $fpprintprec
0
52 "Controls the number of significant digits printed for floats. If
53 0, then full precision is used."
56 (defmvar $maxfpprintprec
(ceiling (log (expt 2 (float-digits 1.0)) 10.0))
57 "The maximum number of significant digits printed for floats.")
59 (defmvar $fpprec $maxfpprintprec
60 "Number of decimal digits of precision to use when creating new bigfloats.
61 One extra decimal digit in actual representation for rounding purposes.")
63 (defmvar bigfloatzero
'((bigfloat simp
56.
) 0 0)
64 "Bigfloat representation of 0" in-core
)
66 (defmvar bigfloatone
'((bigfloat simp
56.
) #.
(expt 2 55.
) 1)
67 "Bigfloat representation of 1" in-core
)
69 (defmvar bfhalf
'((bigfloat simp
56.
) #.
(expt 2 55.
) 0)
70 "Bigfloat representation of 1/2")
72 (defmvar bfmhalf
'((bigfloat simp
56.
) #.
(- (expt 2 55.
)) 0)
73 "Bigfloat representation of -1/2")
75 (defmvar bigfloat%e
'((bigfloat simp
56.
) 48968212118944587.
2)
76 "Bigfloat representation of %E")
78 (defmvar bigfloat%pi
'((bigfloat simp
56.
) 56593902016227522.
2)
79 "Bigfloat representation of %pi")
81 (defmvar bigfloat%gamma
'((bigfloat simp
56.
) 41592772053807304.
0)
82 "Bigfloat representation of %gamma")
84 (defmvar bigfloat_log2
'((bigfloat simp
56.
) 49946518145322874.
0)
85 "Bigfloat representation of log(2)")
89 ;; Number of bits of precision in the mantissa of newly created bigfloats.
90 ;; FPPREC = ($FPPREC+1)*(Log base 2 of 10)
94 ;; FPROUND uses this to return a second value, i.e. it sets it before
95 ;; returning. This number represents the number of binary digits its input
96 ;; bignum had to be shifted right to be aligned into the mantissa. For
97 ;; example, aligning 1 would mean shifting it FPPREC-1 places left, and
98 ;; aligning 7 would mean shifting FPPREC-3 places left.
102 ;; *DECFP = T if the computation is being done in decimal radix. NIL implies
103 ;; base 2. Decimal radix is used only during output.
107 (defvar max-bfloat-%pi bigfloat%pi
)
108 (defvar max-bfloat-%e bigfloat%e
)
109 (defvar max-bfloat-%gamma bigfloat%gamma
)
110 (defvar max-bfloat-log2 bigfloat_log2
)
113 (declare-top (special *cancelled $float $bfloat $ratprint $ratepsilon $domain $m1pbranch
))
115 ;; Representation of a Bigfloat: ((BIGFLOAT SIMP precision) mantissa exponent)
116 ;; precision -- number of bits of precision in the mantissa.
117 ;; precision = (integer-length mantissa)
118 ;; mantissa -- a signed integer representing a fractional portion computed by
119 ;; fraction = (// mantissa (^ 2 precision)).
120 ;; exponent -- a signed integer representing the scale of the number.
121 ;; The actual number represented is (* fraction (^ 2 exponent)).
128 (defun fpprec1 (assign-var q
)
129 (declare (ignore assign-var
))
130 (if (or (not (fixnump q
)) (< q
1))
131 (merror (intl:gettext
"fpprec: value must be a positive integer; found: ~M") q
))
132 (setq fpprec
(+ 2 (integer-length (expt 10. q
)))
133 bigfloatone
($bfloat
1)
134 bigfloatzero
($bfloat
0)
135 bfhalf
(list (car bigfloatone
) (cadr bigfloatone
) 0)
136 bfmhalf
(list (car bigfloatone
) (- (cadr bigfloatone
)) 0))
139 ;; FPSCAN is called by lexical scan when a
140 ;; bigfloat is encountered. For example, 12.01B-3
141 ;; would be the result of (FPSCAN '(/1 /2) '(/0 /1) '(/- /3))
142 ;; Arguments to FPSCAN are a list of characters to the left of the
143 ;; decimal point, to the right of the decimal point, and in the exponent.
145 (defun fpscan (lft rt exp
&aux
(*read-base
* 10.
) (*m
1) (*cancelled
0))
146 (setq exp
(readlist exp
))
148 (let ((fpprec (+ 4 fpprec
(integer-length exp
)
149 (floor (1+ (* #.
(/ (log 10.0) (log 2.0)) (length lft
))))))
151 (setq temp
(add (readlist lft
)
152 (div (readlist rt
) (expt 10.
(length rt
)))))
153 ($bfloat
(cond ((> (abs exp
) 1000.
)
154 (cons '(mtimes) (list temp
(list '(mexpt) 10. exp
))))
155 (t (mul2 temp
(power 10. exp
))))))))
157 (defun dim-bigfloat (form result
)
158 (let (($lispdisp nil
))
159 (dimension-atom (maknam (fpformat form
)) result
)))
161 ;; Converts the bigfloat L to list of digits including |.| and the
162 ;; exponent marker |b|. The number of significant digits is controlled
165 (if (not (member 'simp
(cdar l
) :test
#'eq
))
166 (setq l
(cons (cons (caar l
) (cons 'simp
(cdar l
))) (cdr l
))))
167 (cond ((equal (cadr l
) 0)
168 (if (not (equal (caddr l
) 0))
169 (mtell "FPFORMAT: warning: detected an incorrect form of 0.0b0: ~M, ~M~%"
171 (list '|
0|
'|.|
'|
0|
'|b|
'|
0|
))
172 (t ;; L IS ALWAYS POSITIVE FP NUMBER
173 (let ((extradigs (floor (1+ (quotient (integer-length (caddr l
)) #.
(/ (log 10.0) (log 2.0))))))
178 (fpprec (+ extradigs
(decimalsin (- (caddar l
) 2))))
182 (setq expon
(- (cadr l
) of
))
183 (setq l
(if (minusp expon
)
184 (fpquotient (intofp (car l
)) (fpintexpt 2 (- expon
) of
))
185 (fptimes* (intofp (car l
)) (fpintexpt 2 expon of
))))
186 (incf fpprec
(- extradigs
))
187 (list (fpround (car l
)) (+ (- extradigs
) *m
(cadr l
))))))
188 (let ((*print-base
* 10.
)
191 (setq l1
(if (not $bftrunc
)
193 (do ((l (nreverse (explodec (car l
))) (cdr l
)))
194 ((not (eq '|
0|
(car l
))) (nreverse l
)))))
195 (nconc (ncons (car l1
)) (ncons '|.|
)
197 (cond ((or (zerop $fpprintprec
)
198 (not (< $fpprintprec $fpprec
))
201 (t (setq l1
(cdr l1
))
202 (do ((i $fpprintprec
(1- i
)) (l2))
203 ((or (< i
2) (null l1
))
204 (cond ((not $bftrunc
) (nreverse l2
))
205 (t (do ((l3 l2
(cdr l3
)))
206 ((not (eq '|
0|
(car l3
)))
208 (setq l2
(cons (car l1
) l2
) l1
(cdr l1
))))))
211 (explodec (1- (cadr l
))))))))
214 ;; NOTE: This is a modified version of FORMAT-EXP-AUX from CMUCL to
215 ;; support printing of bfloats.
216 (defun bfloat-format-e (stream arg colonp atp
217 &optional w d e
(k 1)
218 overflowchar
(padchar #\space
) exponentchar
)
219 (declare (ignore colonp
))
220 (flet ((exponent-value (x)
221 ;; Compute the (decimal exponent) of the bfloat number X.
222 (let* (($fpprintprec
1)
224 (marker (position '|b| f
)))
225 ;; FIXME: do something better than printing and reading
228 (format nil
"~{~A~}" (nthcdr (1+ marker
) f
)))))
229 (bfloat-to-string (x fdigits scale
)
230 ;; Print the bfloat X with FDIGITS after the decimal
231 ;; point. This means, roughtly, FDIGITS+1 significant
233 (let* (($fpprintprec
(if fdigits
238 (f (fpformat (bcons (fpabs (cdr x
)))))
239 (marker (position '|b| f
))
240 (digits (remove '|.|
(subseq f
0 marker
))))
241 ;; Depending on the value of k, move the decimal
242 ;; point. DIGITS was printed assuming the decimal point
243 ;; is after the first digit. But if fdigits = 0, fpformat
244 ;; actually printed out one too many digits, so we need
246 (when (and fdigits
(zerop fdigits
))
247 (setf digits
(butlast digits
)))
251 ;; Put the leading decimal and then some zeroes
256 ;; The number is scaled by 10^k. Do this by
257 ;; putting the decimal point in the right place,
258 ;; appending zeroes if needed.
260 (cond ((> k
(length digits
))
263 (make-list (- k
(length digits
))
264 :initial-element
#\
0)
270 (subseq digits k
)))))))
271 (let* ((str (format nil
"~{~A~}" digits
))
273 (when (and fdigits
(>= fdigits len
))
274 ;; Append some zeroes to get the desired number of digits
275 (setf str
(concatenate 'string str
276 (make-string (+ 1 k
(- fdigits len
))
277 :initial-element
#\
0)))
278 (setf len
(length str
)))
281 (char= (aref str
0) #\.
)
282 (char= (aref str
(1- (length str
))) #\.
)
285 (let* ((num-expt (exponent-value arg
))
286 (expt (if (zerop (second arg
))
288 (1+ (- num-expt k
))))
289 (estr (format nil
"~D" (abs expt
)))
290 (elen (if e
(max (length estr
) e
) (length estr
)))
292 (cond ((and w overflowchar e
(> elen e
))
295 (write-char overflowchar stream
)))
305 (if (or atp
(minusp (second arg
)))
310 (format t
"d, k = ~D ~D~%" d k
)
311 (format t
"fdig = ~D, spaceleft = ~D~%" fdig spaceleft
))
313 (multiple-value-bind (fstr flen lpoint tpoint
)
314 (bfloat-to-string arg fdig
(or k
1))
316 (format t
"fstr flen lpoint tpoint = ~S ~S ~S ~S~%"
317 fstr flen lpoint tpoint
)
318 (when (and d
(zerop d
)) (setq tpoint nil
))
320 (decf spaceleft flen
)
321 ;; See CLHS 22.3.3.2. "If the parameter d is
322 ;; omitted, ... [and] if the fraction to be
323 ;; printed is zero then a single zero digit should
324 ;; appear after the decimal point." So we need to
325 ;; subtract one from here because we're going to
326 ;; add an extra 0 digit later.
327 (when (and (null d
) (char= (aref fstr
(1- flen
)) #\.
))
331 (if (or (> spaceleft
0) tpoint
)
334 (when (and tpoint
(<= spaceleft
0))
337 (format t
"w, spaceleft overflowchar = ~S ~S ~S~%"
338 w spaceleft overflowchar
)
339 (cond ((and w
(< spaceleft
0) overflowchar
)
340 ;; Significand overflow; output the overflow char
342 (write-char overflowchar stream
)))
345 (dotimes (i spaceleft
)
346 (write-char padchar stream
)))
347 (if (minusp (second arg
))
348 (write-char #\- stream
)
349 (when atp
(write-char #\
+ stream
)))
351 (write-char #\
0 stream
))
353 (write-string fstr stream
)
354 ;; Add a zero if we need it. Which means
355 ;; we figured out we need one above, or
356 ;; another condition. Basically, append a
357 ;; zero if there are no width constraints
358 ;; and if the last char to print was a
359 ;; decimal (so the trailing fraction is
363 (char= (aref fstr
(1- flen
)) #\.
)))
364 (write-char #\
0 stream
))
365 (write-char (if exponentchar
369 (write-char (if (minusp expt
) #\-
#\
+) stream
)
371 (dotimes (i (- e
(length estr
)))
372 (write-char #\
0 stream
)))
373 (write-string estr stream
)))))))))
376 ;; NOTE: This is a modified version of FORMAT-FIXED-AUX from CMUCL to
377 ;; support printing of bfloats.
378 (defun bfloat-format-f (stream number colonp atsign
&optional w d
(k 0) ovf
(pad #\space
))
379 (declare (ignore colonp
))
382 ;; Compute the (decimal exponent) of the bfloat number X.
383 (let* (($fpprintprec
1)
385 (marker (position '|b| f
)))
386 ;; FIXME: do something better than printing and reading
389 (format nil
"~{~A~}" (nthcdr (1+ marker
) f
)))))
390 (bfloat-to-string (x fdigits scale spaceleft
)
391 ;; Print the bfloat X with FDIGITS after the decimal
392 ;; point. To do this we need to know the exponent because
393 ;; fpformat always produces exponential output. If the
394 ;; exponent is E, and we want FDIGITS after the decimal
395 ;; point, we need FDIGITS + E digits printed.
396 (flet ((compute-prec (exp spaceleft
)
398 (format t
"compute-prec ~D ~D~%" exp spaceleft
)
402 (max (1- spaceleft
) (1+ exp
)))
405 (let* ((exp (+ k
(exponent-value x
)))
406 ($fpprintprec
(compute-prec exp spaceleft
))
407 (f (let ((maxima::$bftrunc nil
))
409 (format t
"printprec = ~D~%" $fpprintprec
)
410 (fpformat (bcons (fpabs (cdr x
))))))
411 (marker (position '|b| f
))
412 (digits (remove '|.|
(subseq f
0 marker
))))
413 ;; Depending on the value of scale, move the decimal
414 ;; point. DIGITS was printed assuming the decimal point
415 ;; is after the first digit. But if fdigits = 0, fpformat
416 ;; actually printed out one too many digits, so we need
419 (format t
"exp, fdigits = ~D ~D, digits = ~S~%" exp fdigits digits
)
421 (when (and fdigits
(zerop fdigits
))
422 (setf digits
(butlast digits
)))
423 ;; Figure out where the decimal point should go. An
424 ;; exponent of 0 means the decimal is after the first
427 (dotimes (k (1- (abs exp
)))
430 ((< exp
(length digits
))
432 (format t
"exp, len = ~D ~D~%" exp
(length digits
))
433 (setf digits
(concatenate 'list
434 (subseq digits
0 (1+ exp
))
436 (subseq digits
(1+ exp
)))))
438 (setf digits
(append digits
(list '|.|
)))))
439 (let* ((str (format nil
"~{~A~}" digits
))
442 (format t
"str = ~S~%" str
)
443 (when (and fdigits
(>= fdigits len
))
444 ;; Append some zeroes to get the desired number of digits
445 (setf str
(concatenate 'string str
446 (make-string (+ 1 scale
(- fdigits len
))
447 :initial-element
#\
0)))
448 (setf len
(length str
)))
451 (char= (aref str
0) #\.
)
452 (char= (aref str
(1- (length str
))) #\.
)
456 (when (and w
(or atsign
(minusp (second number
))))
458 (multiple-value-bind (str len lpoint tpoint
)
459 (bfloat-to-string number d k spaceleft
)
460 ;;if caller specifically requested no fraction digits, suppress the
461 ;;optional trailing zero
462 (when (and d
(zerop d
)) (setq tpoint nil
))
465 ;;optional leading zero
467 (if (or (> spaceleft
0) tpoint
) ;force at least one digit
470 ;;optional trailing zero
475 (cond ((and w
(< spaceleft
0) ovf
)
476 ;;field width overflow
477 (dotimes (i w
) (write-char ovf stream
))
480 (when w
(dotimes (i spaceleft
) (write-char pad stream
)))
481 (if (minusp (second number
))
482 (write-char #\- stream
)
483 (if atsign
(write-char #\
+ stream
)))
484 (when lpoint
(write-char #\
0 stream
))
485 (write-string str stream
)
486 (when tpoint
(write-char #\
0 stream
))
489 ;; NOTE: This is a modified version of FORMAT-EXP-AUX from CMUCL to
490 ;; support printing of bfloats.
491 (defun bfloat-format-g (stream arg colonp atsign
492 &optional w d e
(k 1)
493 ovf
(pad #\space
) exponentchar
)
494 (declare (ignore colonp
))
495 (flet ((exponent-value (x)
496 ;; Compute the (decimal exponent) of the bfloat number X.
497 (let* (($fpprintprec
1)
499 (marker (position '|b| f
)))
500 ;; FIXME: do something better than printing and reading
503 (format nil
"~{~A~}" (nthcdr (1+ marker
) f
)))))
504 (bfloat-to-string (x fdigits
)
505 ;; Print the bfloat X with FDIGITS after the decimal
506 ;; point. This means, roughtly, FDIGITS+1 significant
508 (let* (($fpprintprec
(if fdigits
513 (f (fpformat (bcons (fpabs (cdr x
)))))
514 (marker (position '|b| f
))
515 (digits (remove '|.|
(subseq f
0 marker
))))
516 ;; Depending on the value of k, move the decimal
517 ;; point. DIGITS was printed assuming the decimal point
518 ;; is after the first digit. But if fdigits = 0, fpformat
519 ;; actually printed out one too many digits, so we need
521 (when (and fdigits
(zerop fdigits
))
522 (setf digits
(butlast digits
)))
526 ;; Put the leading decimal and then some zeroes
531 ;; The number is scaled by 10^k. Do this by
532 ;; putting the decimal point in the right place,
533 ;; appending zeroes if needed.
535 (cond ((> k
(length digits
))
538 (make-list (- k
(length digits
))
539 :initial-element
#\
0)
545 (subseq digits k
)))))))
546 (let* ((str (format nil
"~{~A~}" digits
))
548 (when (and fdigits
(>= fdigits len
))
549 ;; Append some zeroes to get the desired number of digits
550 (setf str
(concatenate 'string str
551 (make-string (+ 1 k
(- fdigits len
))
552 :initial-element
#\
0)))
553 (setf len
(length str
)))
556 (char= (aref str
0) #\.
)
557 (char= (aref str
(1- (length str
))) #\.
)
560 (let* ((n (1+ (exponent-value arg
)))
562 ;; Default d if omitted. The procedure is taken directly from
563 ;; the definition given in the manual (CLHS 22.3.3.3), and is
564 ;; not very efficient, since we generate the digits twice.
565 ;; Future maintainers are encouraged to improve on this.
567 ;; It's also not very clear whether q in the spec is the
568 ;; number of significant digits or not. I (rtoy) think it
569 ;; makes more sense if q is the number of significant digits.
570 ;; That way 1d300 isn't printed as 1 followed by 300 zeroes.
571 ;; Exponential notation would be used instead.
573 (let* ((q (1- (nth-value 1 (bfloat-to-string arg nil
)))))
574 (setq d
(max q
(min n
7)))))
575 (let* ((ee (if e
(+ e
2) 4))
576 (ww (if w
(- w ee
) nil
))
580 (format t
"d = ~A~%" d
)
581 (format t
"ee = ~A~%" ee
)
582 (format t
"ww = ~A~%" ww
)
583 (format t
"dd = ~A~%" dd
)
584 (format t
"n = ~A~%" n
))
586 ;; Use dd fraction digits, even if that would cause
587 ;; the width to be exceeded. We choose accuracy over
588 ;; width in this case.
589 (let* ((fill-char (if (bfloat-format-f stream arg nil atsign
596 (dotimes (i ee
) (write-char fill-char stream
))))
598 (bfloat-format-e stream arg nil atsign
602 ovf pad exponentchar
)))))))
604 ;; Tells you if you have a bigfloat object. BUT, if it is a bigfloat,
605 ;; it will normalize it by making the precision of the bigfloat match
606 ;; the current precision setting in fpprec. And it will also convert
607 ;; bogus zeroes (mantissa is zero, but exponent is not) to a true
610 ;; A bigfloat object looks like '((bigfloat simp <prec>) <mantissa> <exp>)
612 (cond ((not ($bfloatp x
)) (return nil
))
613 ((= fpprec
(caddar x
))
614 ;; Precision matches. (Should we fix up bogus bigfloat
617 ((> fpprec
(caddar x
))
618 ;; Current precision is higher than bigfloat precision.
619 ;; Scale up mantissa and adjust exponent to get the
620 ;; correct precision.
621 (setq x
(bcons (list (fpshift (cadr x
) (- fpprec
(caddar x
)))
624 ;; Current precision is LOWER than bigfloat precision.
625 ;; Round the number to the desired precision.
626 (setq x
(bcons (list (fpround (cadr x
))
627 (+ (caddr x
) *m fpprec
(- (caddar x
))))))))
628 ;; Fix up any bogus zeros that we might have created.
629 (return (if (equal (cadr x
) 0) (bcons (list 0 0)) x
))))
631 (defun bigfloat2rat (x)
632 (setq x
(bigfloatp x
))
637 (setq exp
(cond ((minusp (cadr x
))
639 y
(fpration1 (cons (car x
) (fpabs (cdr x
)))))
640 (rplaca y
(* -
1 (car y
))))
643 (princ "`rat' replaced ")
644 (when sign
(princ "-"))
645 (princ (maknam (fpformat (cons (car x
) (fpabs (cdr x
))))))
651 (setq x
($bfloat
(list '(rat simp
) (car exp
) (cdr exp
))))
652 (when sign
(princ "-"))
653 (princ (maknam (fpformat (cons (car x
) (fpabs (cdr x
))))))
658 (let ((fprateps (cdr ($bfloat
(if $bftorat
659 (list '(rat simp
) 1 (exptrl 2 (1- fpprec
)))
661 (or (and (equal x bigfloatzero
) (cons 0 1))
663 (return (do ((xx x
(setq y
(invertbigfloat
664 (bcons (fpdifference (cdr xx
) (cdr ($bfloat a
)))))))
665 (num (setq a
(fpentier x
))
666 (+ (* (setq a
(fpentier y
)) num
) onum
))
667 (den 1 (+ (* a den
) oden
))
670 ((and (not (zerop den
))
673 (fpdifference (cdr x
)
674 (fpquotient (cdr ($bfloat num
))
675 (cdr ($bfloat den
))))
678 (cons num den
))))))))
680 (defun float-nan-p (x)
681 (and (floatp x
) (not (= x x
))))
683 (defun float-inf-p (x)
684 (and (floatp x
) (not (float-nan-p x
)) (beyond-extreme-values x
)))
686 (defun beyond-extreme-values (x)
687 (multiple-value-bind (most-negative most-positive
) (extreme-float-values x
)
689 ((< x
0) (< x most-negative
))
690 ((> x
0) (> x most-positive
))
693 (defun extreme-float-values (x)
694 ;; BLECHH, I HATE ENUMERATING CASES. IS THERE A BETTER WAY ??
696 (short-float (values most-negative-short-float most-positive-short-float
))
697 (single-float (values most-negative-single-float most-positive-single-float
))
698 (double-float (values most-negative-double-float most-positive-double-float
))
699 (long-float (values most-negative-long-float most-positive-long-float
))
700 ;; NOT SURE THE FOLLOWING REALLY WORKS
701 ;; #+(and cmu double-double)
702 ;; (kernel:double-double-float
703 ;; (values most-negative-double-double-float most-positive-double-double-float))
706 ;; Convert a floating point number into a bigfloat.
709 (merror (intl:gettext
"bfloat: attempted conversion of floating point NaN (not-a-number).~%")))
711 (merror (intl:gettext
"bfloat: attempted conversion of floating-point infinity.~%")))
713 (mtell (intl:gettext
"bfloat: converting float ~S to bigfloat.~%") x
))
715 ;; Need to check for zero because different lisps return different
716 ;; values for integer-decode-float of a 0. In particular CMUCL
717 ;; returns 0, -1075. A bigfloat zero needs to have an exponent and
721 (multiple-value-bind (frac exp sign
)
722 (integer-decode-float x
)
723 ;; Scale frac to the desired number of bits, and adjust the
724 ;; exponent accordingly.
725 (let ((scale (- fpprec
(integer-length frac
))))
726 (list (ash (* sign frac
) scale
)
727 (+ fpprec
(- exp scale
)))))))
729 ;; Convert a bigfloat into a floating point number.
731 (let ((precision (caddar l
))
734 (fpprec machine-mantissa-precision
)
736 ;; Round the mantissa to the number of bits of precision of the
737 ;; machine, and then convert it to a floating point fraction. We
738 ;; have 0.5 <= mantissa < 1
739 (setq mantissa
(quotient (fpround mantissa
) (expt 2.0 machine-mantissa-precision
)))
740 ;; Multiply the mantissa by the exponent portion. I'm not sure
741 ;; why the exponent computation is so complicated.
743 ;; GCL doesn't signal overflow from scale-float if the number
744 ;; would overflow. We have to do it this way. 0.5 <= mantissa <
745 ;; 1. The largest double-float is .999999 * 2^1024. So if the
746 ;; exponent is 1025 or higher, we have an overflow.
747 (let ((e (+ exponent
(- precision
) *m machine-mantissa-precision
)))
749 (merror (intl:gettext
"float: floating point overflow converting ~:M") l
)
750 (scale-float mantissa e
)))))
752 ;; New machine-independent version of FIXFLOAT. This may be buggy. - CWH
753 ;; It is buggy! On the PDP10 it dies on (RATIONALIZE -1.16066076E-7)
754 ;; which calls FLOAT on some rather big numbers. ($RATEPSILON is approx.
758 (let (($ratepsilon
(expt 2.0 (- machine-mantissa-precision
))))
759 (maxima-rationalize x
)))
761 ;; Takes a flonum arg and returns a rational number corresponding to the flonum
762 ;; in the form of a dotted pair of two integers. Since the denominator will
763 ;; always be a positive power of 2, this number will not always be in lowest
767 `((bigfloat simp
,fpprec
) .
,s
))
771 (cond ((bigfloatp x
))
773 (member x
'($%e $%pi $%gamma
) :test
#'eq
))
775 ((or (atom x
) (member 'array
(cdar x
) :test
#'eq
))
777 ($bfloat
'((mtimes simp
)
779 ((mplus simp
) 1 ((mexpt simp
) 5 ((rat simp
) 1 2)))))
781 ((eq (caar x
) 'mexpt
)
782 (if (equal (cadr x
) '$%e
)
783 (*fpexp
($bfloat
(caddr x
)))
784 (exptbigfloat ($bfloat
(cadr x
)) (caddr x
))))
785 ((eq (caar x
) 'mncexpt
)
786 (list '(mncexpt) ($bfloat
(cadr x
)) (caddr x
)))
788 (ratbigfloat (cdr x
)))
789 ((setq y
(safe-get (caar x
) 'floatprog
))
790 (funcall y
(mapcar #'$bfloat
(cdr x
))))
791 ((or (trigp (caar x
)) (arcp (caar x
)) (eq (caar x
) '$entier
))
792 (setq y
($bfloat
(cadr x
)))
794 (cond ((eq (caar x
) '$entier
) ($entier y
))
796 (setq y
($bfloat
(logarc (caar x
) y
)))
798 y
(let ($ratprint
) (fparcsimp ($rectform y
)))))
799 ((member (caar x
) '(%cot %sec %csc
) :test
#'eq
)
801 ($bfloat
(list (ncons (safe-get (caar x
) 'recip
)) y
))))
802 (t ($bfloat
(exponentialize (caar x
) y
))))
803 (subst0 (list (ncons (caar x
)) y
) x
)))
804 (t (recur-apply #'$bfloat x
)))))
806 (defprop mplus addbigfloat floatprog
)
807 (defprop mtimes timesbigfloat floatprog
)
808 (defprop %sin sinbigfloat floatprog
)
809 (defprop %cos cosbigfloat floatprog
)
810 (defprop rat ratbigfloat floatprog
)
811 (defprop %atan atanbigfloat floatprog
)
812 (defprop %tan tanbigfloat floatprog
)
813 (defprop %log logbigfloat floatprog
)
814 (defprop mabs mabsbigfloat floatprog
)
816 (defmfun addbigfloat
(h)
817 (prog (fans tst r nfans
)
818 (setq fans
(setq tst bigfloatzero
) nfans
0)
821 (cond ((setq r
(bigfloatp (car l
)))
822 (setq fans
(bcons (fpplus (cdr r
) (cdr fans
)))))
823 (t (setq nfans
(list '(mplus) (car l
) nfans
)))))
824 (return (cond ((equal nfans
0) fans
)
825 ((equal fans tst
) nfans
)
826 (t (simplify (list '(mplus) fans nfans
)))))))
828 (defmfun ratbigfloat
(r)
829 ;; R is a Maxima ratio, represented as a list of the numerator and
830 ;; denominator. FLOAT-RATIO doesn't like it if the numerator is 0,
831 ;; so handle that here.
834 (bcons (float-ratio r
))))
836 ;; This is borrowed from CMUCL (float-ratio-float), and modified for
837 ;; converting ratios to Maxima's bfloat numbers.
838 (defun float-ratio (x)
839 (let* ((signed-num (first x
))
840 (plusp (plusp signed-num
))
841 (num (if plusp signed-num
(- signed-num
)))
845 (declare (fixnum digits scale
))
847 ;; Strip any trailing zeros from the denominator and move it into the scale
848 ;; factor (to minimize the size of the operands.)
849 (let ((den-twos (1- (integer-length (logxor den
(1- den
))))))
850 (declare (fixnum den-twos
))
851 (decf scale den-twos
)
852 (setq den
(ash den
(- den-twos
))))
854 ;; Guess how much we need to scale by from the magnitudes of the numerator
855 ;; and denominator. We want one extra bit for a guard bit.
856 (let* ((num-len (integer-length num
))
857 (den-len (integer-length den
))
858 (delta (- den-len num-len
))
859 (shift (1+ (the fixnum
(+ delta digits
))))
860 (shifted-num (ash num shift
)))
861 (declare (fixnum delta shift
))
863 (labels ((float-and-scale (bits)
864 (let* ((bits (ash bits -
1))
865 (len (integer-length bits
)))
866 (cond ((> len digits
)
867 (assert (= len
(the fixnum
(1+ digits
))))
868 (multiple-value-bind (f0)
869 (floatit (ash bits -
1))
870 (list (first f0
) (+ (second f0
)
873 (multiple-value-bind (f0)
875 (list (first f0
) (+ (second f0
) scale
)))))))
877 (let ((sign (if plusp
1 -
1)))
878 (list (* sign bits
) 0))))
880 (multiple-value-bind (fraction-and-guard rem
)
881 (truncate shifted-num den
)
882 (let ((extra (- (integer-length fraction-and-guard
) digits
)))
883 (declare (fixnum extra
))
885 (assert (> extra
1)))
886 ((oddp fraction-and-guard
)
890 (if (zerop (logand fraction-and-guard
2))
892 (1+ fraction-and-guard
)))
893 (float-and-scale (1+ fraction-and-guard
)))))
895 (return (float-and-scale fraction-and-guard
)))))
896 (setq shifted-num
(ash shifted-num -
1))
899 (defun decimalsin (x)
900 (do ((i (quotient (* 59. x
) 196.
) (1+ i
))) ;log[10](2)=.301029
902 (when (> (integer-length (expt 10. i
)) x
)
905 (defmfun atanbigfloat
(x)
906 (*fpatan
(car x
) (cdr x
)))
908 (defmfun *fpatan
(a y
)
909 (fpend (let ((fpprec (+ 8. fpprec
)))
911 (if ($bfloatp a
) (fpatan (cdr ($bfloat a
)))
913 (fpatan2 (cdr ($bfloat a
)) (cdr ($bfloat
(car y
))))))))
917 (prog (term x2 ans oans one two tmp
)
918 (setq one
(intofp 1) two
(intofp 2))
919 (cond ((fpgreaterp (fpabs x
) one
)
923 ;; atan(x) + acot(x) = +/- pi/2 (+ for x >= 0, - for x < 0)
926 ;; acot(z) = atan(1/z)
927 (setq tmp
(fpquotient (fppi) two
))
928 (setq ans
(fpdifference tmp
(fpatan (fpquotient one x
))))
929 (return (cond ((fplessp x
(intofp 0))
930 (fpdifference ans
(fppi)))
932 ((fpgreaterp (fpabs x
) (fpquotient one two
))
935 ;; Use A&S 4.4.42, third formula:
937 ;; atan(z) = z/(1+z^2)*[1 + 2/3*r + (2*4)/(3*5)*r^2 + ...]
940 (setq tmp
(fpquotient x
(fpplus (fptimes* x x
) one
)))
941 (setq x2
(fptimes* x tmp
) term
(setq ans one
))
945 (fptimes* term
(fptimes* x2
(fpquotient
946 (intofp (+ 2 (* 2 n
)))
947 (intofp (+ (* 2 n
) 3))))))
948 (setq oans ans ans
(fpplus term ans
)))
949 (setq ans
(fptimes* tmp ans
)))
951 ;; |x| <= 1/2. Use Taylor series (A&S 4.4.42, first
953 (setq ans x x2
(fpminus (fptimes* x x
)) term x
)
956 (setq term
(fptimes* term x2
))
958 ans
(fpplus ans
(fpquotient term
(intofp n
)))))))
961 ;; atan(y/x) taking into account the quadrant. (Also equal to
964 (cond ((equal (car x
) 0)
965 ;; atan(y/0) = atan(inf), but what sign?
966 (cond ((equal (car y
) 0)
967 (merror (intl:gettext
"atan2: atan2(0, 0) is undefined.")))
969 ;; We're on the negative imaginary axis, so -pi/2.
970 (fpquotient (fppi) (intofp -
2)))
972 ;; The positive imaginary axis, so +pi/2
973 (fpquotient (fppi) (intofp 2)))))
975 ;; x > 0. atan(y/x) is the correct value.
976 (fpatan (fpquotient y x
)))
978 ;; x < 0, and y > 0. We're in quadrant II, so the angle we
979 ;; want is pi+atan(y/x).
980 (fpplus (fppi) (fpatan (fpquotient y x
))))
982 ;; x <= 0 and y <= 0. We're in quadrant III, so the angle we
983 ;; want is atan(y/x)-pi.
984 (fpdifference (fpatan (fpquotient y x
)) (fppi)))))
986 (defun tanbigfloat (a)
988 (fpend (let ((fpprec (+ 8. fpprec
)))
990 (setq a
(cdr ($bfloat a
)))
991 (fpquotient (fpsin a t
) (fpsin a nil
)))
992 (t (list '(%tan
) a
))))))
994 ;; Returns a list of a mantissa and an exponent.
996 (cond ((not (atom l
)) ($bfloat l
))
997 ((floatp l
) (floattofp l
))
999 ((eq l
'$%pi
) (fppi))
1001 ((eq l
'$%gamma
) (fpgamma))
1002 (t (list (fpround l
) (+ *m fpprec
)))))
1004 ;; It seems to me that this function gets called on an integer
1005 ;; and returns the mantissa portion of the mantissa/exponent pair.
1007 ;; "STICKY BIT" CALCULATION FIXED 10/14/75 --RJF
1008 ;; BASE must not get temporarily bound to NIL by being placed
1009 ;; in a PROG list as this will confuse stepping programs.
1011 (defun fpround (l &aux
(*print-base
* 10.
) *print-radix
*)
1015 ;;*M will be positive if the precision of the argument is greater than
1016 ;;the current precision being used.
1017 (setq *m
(- (integer-length l
) fpprec
))
1021 ;;FPSHIFT is essentially LSH.
1022 (setq adjust
(fpshift 1 (1- *m
)))
1023 (when (minusp l
) (setq adjust
(- adjust
)))
1025 (setq *m
(- (integer-length l
) fpprec
))
1026 (setq *cancelled
(abs *m
))
1027 (cond ((zerop (hipart l
(- *m
)))
1028 ;ONLY ZEROES SHIFTED OFF
1029 (return (fpshift (fpshift l
(- -
1 *m
))
1030 1))) ; ROUND TO MAKE EVEN
1031 (t (return (fpshift l
(- *m
))))))
1033 (setq *m
(- (flatsize (abs l
)) fpprec
))
1034 (setq adjust
(fpshift 1 (1- *m
)))
1035 (when (minusp l
) (setq adjust
(- adjust
)))
1036 (setq adjust
(* 5 adjust
))
1037 (setq *m
(- (flatsize (abs (setq l
(+ l adjust
)))) fpprec
))
1038 (return (fpshift l
(- *m
)))))))
1040 ;; Compute (* L (expt d n)) where D is 2 or 10 depending on
1041 ;; *decfp. Throw away an fractional part by truncating to zero.
1042 (defun fpshift (l n
)
1043 (cond ((null *decfp
)
1044 (cond ((and (minusp n
) (minusp l
))
1045 ;; Left shift of negative number requires some
1046 ;; care. (That is, (truncate l (expt 2 n)), but use
1054 (quotient l
(expt 10.
(- n
))))
1057 ;; Bignum LSH -- N is assumed (and declared above) to be a fixnum.
1058 ;; This isn't really LSH, since the sign bit isn't propagated when
1059 ;; shifting to the right, i.e. (BIGLSH -100 -3) = -40, whereas
1060 ;; (LSH -100 -3) = 777777777770 (on a 36 bit machine).
1061 ;; This actually computes (* X (EXPT 2 N)). As of 12/21/80, this function
1062 ;; was only called by FPSHIFT. I would like to hear an argument as why this
1063 ;; is more efficient than simply writing (* X (EXPT 2 N)). Is the
1064 ;; intermediate result created by (EXPT 2 N) the problem? I assume that
1065 ;; EXPT tries to LSH when possible.
1068 (cond ((and (not (bignump x
))
1069 (< n
#.
(- +machine-fixnum-precision
+)))
1071 ;; Either we are shifting a fixnum to the right, or shifting
1072 ;; a fixnum to the left, but not far enough left for it to become
1074 ((and (not (bignump x
))
1076 (< (+ (integer-length x
) n
) #.
+machine-fixnum-precision
+)))
1077 ;; The form which follows is nearly identical to (ASH X N), however
1078 ;; (ASH -100 -20) = -1, whereas (BIGLSH -100 -20) = 0.
1081 (- (biglsh (- x
) n
)))) ;(- x) may be a bignum even is x is a fixnum.
1082 ;; If we get here, then either X is a bignum or our answer is
1083 ;; going to be a bignum.
1085 (cond ((> (abs n
) (integer-length x
)) 0)
1087 (hipart x
(+ (integer-length x
) n
)))
1088 (t (- (hipart x
(+ (integer-length x
) n
))))))
1090 ;; Isn't this the kind of optimization that compilers are
1091 ;; supposed to make?
1092 ((< n
#.
(1- +machine-fixnum-precision
+)) (* x
(ash 1 n
)))
1093 (t (* x
(expt 2 n
)))))
1098 ;; For negative x, use exp(-x) = 1/exp(x)
1100 ;; For x > 0, exp(x) = exp(r+y) = exp(r) * exp(y), where x = r + y and
1104 (unless (signp ge
(car x
))
1105 (return (fpquotient (fpone) (fpexp (fpabs x
)))))
1106 (setq r
(fpintpart x
))
1107 (return (cond ((< r
2)
1110 (setq s
(fpexp1 (fpdifference x
(intofp r
))))
1113 (let ((fpprec (+ fpprec
(integer-length r
) -
1))
1115 (bcons (fpexpt (fpe) r
))))))))))) ; patch for full precision %E
1117 ;; exp(x) for small x, using Taylor series.
1119 (prog (term ans oans
)
1120 (setq ans
(setq term
(fpone)))
1123 (setq term
(fpquotient (fptimes* x term
) (intofp n
)))
1125 (setq ans
(fpplus ans term
)))
1128 ;; Does one higher precision to round correctly.
1129 ;; A and B are each a list of a mantissa and an exponent.
1130 (defun fpquotient (a b
)
1131 (cond ((equal (car b
) 0)
1132 (merror (intl:gettext
"pquotient: attempted quotient by zero.")))
1133 ((equal (car a
) 0) '(0 0))
1134 (t (list (fpround (quotient (fpshift (car a
) (+ 3 fpprec
)) (car b
)))
1135 (+ -
3 (- (cadr a
) (cadr b
)) *m
)))))
1137 (defun fpgreaterp (a b
)
1138 (fpposp (fpdifference a b
)))
1140 (defun fplessp (a b
)
1141 (fpposp (fpdifference b a
)))
1146 (defmfun fpmin
(arg1 &rest args
)
1148 (mapc #'(lambda (u) (if (fplessp u min
) (setq min u
))) args
)
1151 (defmfun fpmax
(arg1 &rest args
)
1153 (mapc #'(lambda (u) (if (fpgreaterp u max
) (setq max u
))) args
)
1156 ;; The following functions compute bigfloat values for %e, %pi,
1157 ;; %gamma, and log(2). For each precision, the computed value is
1158 ;; cached in a hash table so it doesn't need to be computed again.
1159 ;; There are functions to return the hash table or clear the hash
1160 ;; table, for debugging.
1162 ;; Note that each of these return a bigfloat number, but without the
1166 ;; https://sourceforge.net/tracker/?func=detail&atid=104933&aid=2910437&group_id=4933
1167 ;; for an explanation.
1168 (let ((table (make-hash-table)))
1170 (let ((value (gethash fpprec table
)))
1173 (setf (gethash fpprec table
) (cdr (fpe1))))))
1176 (defun clear_fpe_table ()
1179 (let ((table (make-hash-table)))
1181 (let ((value (gethash fpprec table
)))
1184 (setf (gethash fpprec table
) (cdr (fppi1))))))
1185 (defun fppi-table ()
1187 (defun clear_fppi_table ()
1190 (let ((table (make-hash-table)))
1192 (let ((value (gethash fpprec table
)))
1195 (setf (gethash fpprec table
) (cdr (fpgamma1))))))
1196 (defun fpgamma-table ()
1198 (defun clear_fpgamma_table ()
1201 (let ((table (make-hash-table)))
1203 (let ((value (gethash fpprec table
)))
1206 (setf (gethash fpprec table
) (comp-log2)))))
1207 (defun fplog2-table ()
1209 (defun clear_fplog2_table ()
1212 ;; This doesn't need a hash table because there's never a problem with
1213 ;; using a high precision value and rounding to a lower precision
1214 ;; value because 1 is always an exact bfloat.
1216 (cond (*decfp
(intofp 1))
1217 ((= fpprec
(caddar bigfloatone
)) (cdr bigfloatone
))
1220 ;;----------------------------------------------------------------------------;;
1222 ;; The values of %e, %pi, %gamma and log(2) are computed by the technique of
1223 ;; binary splitting. See http://www.ginac.de/CLN/binsplit.pdf for details.
1225 ;; Volker van Nek, Sept. 2014
1231 (let ((e (compe (+ fpprec
12)))) ;; compute additional bits
1232 (bcons (list (fpround (car e
)) (cadr e
))) )) ;; round to fpprec
1234 ;; Taylor: %e = sum(s[i] ,i,0,inf) where s[i] = 1/i!
1237 (let ((fpprec prec
))
1238 (multiple-value-bind (tt qq
) (split-taylor-e 0 (taylor-e-size prec
))
1239 (fpquotient (intofp tt
) (intofp qq
)) )))
1241 ;; binary splitting:
1244 ;; s[i] = ----------------------
1245 ;; q[0]*q[1]*q[2]*..*q[i]
1250 (defun split-taylor-e (i j
)
1253 (setq qq
(if (= i
0) 1 i
)
1255 (let ((m (ash (+ i j
) -
1)))
1256 (multiple-value-bind (tl ql
) (split-taylor-e i m
)
1257 (multiple-value-bind (tr qr
) (split-taylor-e m j
)
1259 tt
(+ (* qr tl
) tr
) )))))
1262 ;; stop when i! > 2^fpprec
1264 ;; log(i!) = sum(log(k), k,1,i) > fpprec * log(2)
1266 (defun taylor-e-size (prec)
1268 (lim (* prec
(log 2))) )
1271 (incf acc
(log i
)) )))
1273 ;;----------------------------------------------------------------------------;;
1278 (let ((pi1 (comppi (+ fpprec
10))))
1279 (bcons (list (fpround (car pi1
)) (cadr pi1
))) ))
1281 ;; Chudnovsky & Chudnovsky:
1283 ;; C^(3/2)/(12*%pi) = sum(s[i], i,0,inf),
1285 ;; where s[i] = (-1)^i*(6*i)!*(A*i+B) / (i!^3*(3*i)!*C^(3*i))
1287 ;; and A = 545140134, B = 13591409, C = 640320
1289 (defun comppi (prec)
1291 nr n d oldn tt qq n
*qq
)
1293 ;; compute n/d = sqrt(10005) :
1295 ;; n[0] n[i+1] = n[i]^2+a*d[i]^2 n[inf]
1296 ;; quadratic Heron: x[0] = ----, , sqrt(a) = ------
1297 ;; d[0] d[i+1] = 2*n[i]*d[i] d[inf]
1299 (multiple-value-setq (nr n d
) (sqrt-10005-constants fpprec
))
1302 n
(+ (* n n
) (* 10005 d d
))
1305 ;; divide C^(3/2)/12 = 3335*2^7*sqrt(10005)
1306 ;; by Chudnovsky-sum = tt/qq :
1308 (setq nr
(ceiling (* fpprec
0.021226729578153))) ;; nr of summands
1309 ;; fpprec*log(2)/log(C^3/(24*6*2*6))
1310 (multiple-value-setq (tt qq
) (split-chudnovsky 0 (1+ nr
)))
1312 n
*qq
(intofp (* n qq
)) )
1313 (fpquotient (list (car n
*qq
) (+ (cadr n
*qq
) 7))
1314 (intofp (* d tt
)) )))
1316 ;; The returned n and d serve as start values for the iteration.
1317 ;; n/d = sqrt(10005) with a precision of p = ceiling(prec/2^nr) bits
1318 ;; where nr is the number of needed iterations.
1320 (defun sqrt-10005-constants (prec)
1321 (let (ilen p nr n d
)
1324 (setq ilen
(integer-length prec
)
1326 p
(ceiling (* prec
(expt 2.0 (- nr
)))) ))
1328 ((<= p
76) (setq n
256192036001 d
2561280120))
1329 ((<= p
89) (setq n
51244811200700 d
512320048001))
1330 ((<= p
102) (setq n
2050048640064001 d
20495363200160))
1331 ((<= p
115) (setq n
410060972824000900 d
4099584960080001))
1332 (t (setq n
16404488961600100001 d
164003893766400200)) )
1335 ;; binary splitting:
1337 ;; a[i] * p[0]*p[1]*p[2]*..*p[i]
1338 ;; s[i] = -----------------------------
1339 ;; q[0]*q[1]*q[2]*..*q[i]
1344 ;; p[i] = - (6*i-5)*(2*i-1)*(6*i-1)
1345 ;; q[i] = C^3/24*i^3
1347 (defun split-chudnovsky (i j
)
1348 (let (aa pp
/qq pp qq tt
)
1351 (setq aa
13591409 pp
1 qq
1 tt aa
)
1352 (setq aa
(+ (* i
545140134) 13591409)
1353 pp
/qq
(/ (* (- 5 (* 6 i
)) (- (* 2 i
) 1) (- (* 6 i
) 1))
1354 10939058860032000 ) ; C^3/24
1355 pp
(numerator pp
/qq
)
1356 qq
(* (denominator pp
/qq
) (expt i
3))
1358 (let ((m (ash (+ i j
) -
1)))
1359 (multiple-value-bind (tl ql pl
) (split-chudnovsky i m
)
1360 (multiple-value-bind (tr qr pr
) (split-chudnovsky m j
)
1363 tt
(+ (* qr tl
) (* pl tr
)) )))))
1364 (values tt qq pp
) ))
1366 ;;----------------------------------------------------------------------------;;
1368 ;; Euler-Mascheroni constant GAMMA
1371 (let ((res (comp-bf%gamma
(+ fpprec
14))))
1372 (bcons (list (fpround (car res
)) (cadr res
))) ))
1374 ;; Brent-McMillan algorithm
1377 ;; alpha = 4.970625759544
1379 ;; n > 0 and N-1 >= alpha*n
1381 ;; H(k) = sum(1/i, i,1,k)
1383 ;; S = sum(H(k)*(n^k/k!)^2, k,0,N-1)
1385 ;; I = sum((n^k/k!)^2, k,0,N-1)
1387 ;; T = 1/(4*n)*sum((2*k)!^3/(k!^4*(16*n)^(2*k)), k,0,2*n-1)
1390 ;; %gamma = S/I - T/I^2 - log(n)
1393 ;; |%gamma - gamma| < 24 * e^(-8*n)
1395 ;; (Corollary 2, Remark 2, Brent/Johansson http://arxiv.org/pdf/1312.0039v1.pdf)
1397 (defun comp-bf%gamma
(prec)
1398 (let* ((fpprec prec
)
1399 (n (ceiling (* 1/8 (+ (* prec
(log 2.0)) (log 24.0)))))
1401 (alpha 4.970625759544)
1402 (lim (ceiling (* alpha n
)))
1404 sumi sumi2
;; I and I^2
1405 sumt
/sumi2
) ;; T/I^2
1406 (multiple-value-bind (vv tt qq dd
) (split-gamma-1 1 (1+ lim
) n2
)
1408 ;; sums = vv/(qq*dd)
1410 ;; sums/sumi = vv/(qq*dd)*qq/tt = vv/(dd*tt)
1412 (setq sums
/sumi
(fpquotient (intofp vv
) (intofp (* dd tt
)))
1413 sumi
(fpquotient (intofp tt
) (intofp qq
))
1414 sumi2
(fptimes* sumi sumi
) )
1416 (multiple-value-bind (ttt qqq
) (split-gamma-2 0 (* 2 n
) (* 32 n2
))
1418 ;; sumt = 1/(4*n)*ttt/qqq
1419 ;; sumt/sumi2 = ttt/(4*n*qqq*sumi2)
1421 (setq sumt
/sumi2
(fpquotient (intofp ttt
)
1422 (fptimes* (intofp (* 4 n qqq
)) sumi2
) ))
1424 (fpdifference sums
/sumi
(fpplus sumt
/sumi2
(log-n n
)) )))))
1426 ;; split S and I simultaneously:
1428 ;; summands I[0] = 1, I[i]/I[i-1] = n^2/i^2
1430 ;; S[0] = 0, S[i]/S[i-1] = n^2/i^2*H(i)/H(i-1)
1432 ;; p[0]*p[1]*p[2]*..*p[i]
1433 ;; I[i] = ----------------------
1434 ;; q[0]*q[1]*q[2]*..*q[i]
1440 ;; c[0] c[1] c[2] c[i]
1441 ;; S[i] = H[i] * I[i], where H[i] = ---- + ---- + ---- + .. + ----
1442 ;; d[0] d[1] d[2] d[i]
1448 (defun split-gamma-1 (i j n2
)
1449 (let (pp cc dd qq tt vv
)
1452 (if (= i
1) ;; S[0] is 0 -> start with i=1 and add I[0]=1 to tt :
1453 (setq pp n2 cc
1 dd
1 qq
1 tt
(1+ n2
) vv n2
)
1454 (setq pp n2 cc
1 dd i qq
(* i i
) tt pp vv tt
) ))
1456 (let* ((m (ash (+ i j
) -
1)) tmp
)
1457 (multiple-value-bind (vl tl ql dl cl pl
) (split-gamma-1 i m n2
)
1458 (multiple-value-bind (vr tr qr dr cr pr
) (split-gamma-1 m j n2
)
1460 cc
(+ (* cl dr
) (* dl cr
))
1464 tt
(+ (* tl qr
) tmp
)
1465 vv
(+ (* dr
(+ (* vl qr
) (* cl tmp
))) (* dl pl vr
)) ))))))
1466 (values vv tt qq dd cc pp
) ))
1470 ;; summands T[0] = 1, T[i]/T[i-1] = (2*i-1)^3/(32*i*n^2)
1472 ;; p[0]*p[1]*p[2]*..*p[i]
1473 ;; T[i] = ----------------------
1474 ;; q[0]*q[1]*q[2]*..*q[i]
1476 ;; where p[0] = q[0] = 1
1480 (defun split-gamma-2 (i j n2
*32)
1485 (setq pp
1 qq
1 tt
1)
1486 (setq pp
(expt (1- (* 2 i
)) 3) qq
(* i n2
*32) tt pp
) ))
1488 (let* ((m (ash (+ i j
) -
1)))
1489 (multiple-value-bind (tl ql pl
) (split-gamma-2 i m n2
*32)
1490 (multiple-value-bind (tr qr pr
) (split-gamma-2 m j n2
*32)
1493 tt
(+ (* tl qr
) (* pl tr
)) ))))))
1494 (values tt qq pp
) ))
1496 ;;----------------------------------------------------------------------------;;
1498 ;; log(2) = 18*L(26) - 2*L(4801) + 8*L(8749)
1500 ;; where L(k) = atanh(1/k)
1502 ;; see http://numbers.computation.free.fr/Constants/constants.html
1504 ;;;(defun $log2 () (bcons (comp-log2))) ;; checked against reference table
1508 (let ((fpprec (+ fpprec
12)))
1510 (fpdifference (n*atanh-1
/k
18 26) (n*atanh-1
/k
2 4801))
1511 (n*atanh-1
/k
8 8749) ))))
1512 (list (fpround (car res
)) (cadr res
)) ))
1514 ;; Taylor: atanh(1/k) = sum(s[i], i,0,inf)
1516 ;; where s[i] = 1/((2*i+1)*k^(2*i+1))
1518 (defun n*atanh-1
/k
(n k
) ;; integer n,k
1520 (nr (ceiling (* fpprec
(/ (log 2) (log k2
))))) )
1521 (multiple-value-bind (tt qq bb
) (split-atanh-1/k
0 (1+ nr
) k k2
)
1522 (fpquotient (intofp (* n tt
)) (intofp (* bb qq
))) )))
1524 ;; binary splitting:
1526 ;; s[i] = -----------------------------
1527 ;; b[i] * q[0]*q[1]*q[2]*..*q[i]
1534 (defun split-atanh-1/k
(i j k k2
)
1538 (setq bb
1 qq k tt
1)
1539 (setq bb
(1+ (* 2 i
)) qq k2 tt
1) )
1540 (let ((m (ash (+ i j
) -
1)))
1541 (multiple-value-bind (tl ql bl
) (split-atanh-1/k i m k k2
)
1542 (multiple-value-bind (tr qr br
) (split-atanh-1/k m j k k2
)
1545 tt
(+ (* br qr tl
) (* bl tr
)) )))))
1546 (values tt qq bb
) ))
1548 ;;----------------------------------------------------------------------------;;
1550 ;; log(n) = log(n/2^k) + k*log(2)
1552 ;;;(defun $log10 () (bcons (log-n 10))) ;; checked against reference table
1554 (defun log-n (n) ;; integer n > 0
1556 ((= 1 n
) (list 0 0))
1557 ((= 2 n
) (comp-log2))
1560 (let ((fpprec (+ fpprec
10))
1561 (k (integer-length n
)) )
1562 ;; choose k so that |n/2^k - 1| is as small as possible:
1563 (when (< n
(* (coerce 2/3 'flonum
) (ash 1 k
))) (decf k
))
1564 ;; now |n/2^k - 1| <= 1/3
1565 (fpplus (log-u/2^k n k fpprec
)
1566 (fptimes* (intofp k
) (comp-log2)) ))))
1567 (list (fpround (car res
)) (cadr res
)) ))))
1569 ;; log(1+u/v) = 2 * sum(s[i], i,0,inf)
1571 ;; where s[i] = (u/(2*v+u))^(2*i+1)/(2*i+1)
1573 (defun log-u/2^k
(u k prec
) ;; integer u k; x = u/2^k; |x - 1| < 1
1574 (setq u
(- u
(ash 1 k
))) ;; x <-- x - 1
1576 ((= 0 u
) (list 0 0))
1578 (while (evenp u
) (setq u
(ash u -
1)) (decf k
))
1582 (nr (ceiling (* prec
(/ (log 2) 2 (log (abs (/ w u
)))))))
1584 (multiple-value-bind (tt qq bb
) (split-log-1+u
/v
0 (1+ nr
) u u2 w w2
)
1585 (setq lg
/2 (fpquotient (intofp tt
) (intofp (* bb qq
)))) ;; sum
1586 (list (car lg
/2) (1+ (cadr lg
/2))) ))))) ;; 2*sum
1588 ;; binary splitting:
1590 ;; p[0]*p[1]*p[2]*..*p[i]
1591 ;; s[i] = -----------------------------
1592 ;; b[i] * q[0]*q[1]*q[2]*..*q[i]
1601 (defun split-log-1+u
/v
(i j u u2 w w2
)
1605 (setq pp u bb
1 qq w tt u
)
1606 (setq pp u2 bb
(1+ (* 2 i
)) qq w2 tt pp
) )
1607 (let ((m (ash (+ i j
) -
1)))
1608 (multiple-value-bind (tl ql bl pl
) (split-log-1+u
/v i m u u2 w w2
)
1609 (multiple-value-bind (tr qr br pr
) (split-log-1+u
/v m j u u2 w w2
)
1613 tt
(+ (* br qr tl
) (* bl pl tr
)) )))))
1614 (values tt qq bb pp
) ))
1616 ;;----------------------------------------------------------------------------;;
1619 (defun fpdifference (a b
)
1620 (fpplus a
(fpminus b
)))
1623 (if (equal (car x
) 0)
1625 (list (- (car x
)) (cadr x
))))
1628 (prog (*m exp man sticky
)
1630 (cond ((equal (car a
) 0) (return b
))
1631 ((equal (car b
) 0) (return a
)))
1632 (setq exp
(- (cadr a
) (cadr b
)))
1633 (setq man
(cond ((equal exp
0)
1635 (fpshift (+ (car a
) (car b
)) 2))
1637 (setq sticky
(hipart (car b
) (- 1 exp
)))
1638 (setq sticky
(cond ((signp e sticky
) 0)
1639 ((signp l
(car b
)) -
1)
1641 ; COMPUTE STICKY BIT
1642 (+ (fpshift (car a
) 2)
1643 ; MAKE ROOM FOR GUARD DIGIT & STICKY BIT
1644 (fpshift (car b
) (- 2 exp
))))
1645 (t (setq sticky
(hipart (car a
) (1+ exp
)))
1646 (setq sticky
(cond ((signp e sticky
) 0)
1647 ((signp l
(car a
)) -
1)
1649 (+ (fpshift (car b
) 2)
1650 (fpshift (car a
) (+ 2 exp
))))))
1651 (setq man
(+ man sticky
))
1652 (return (cond ((equal man
0) '(0 0))
1653 (t (setq man
(fpround man
))
1654 (setq exp
(+ -
2 *m
(max (cadr a
) (cadr b
))))
1657 (defun fptimes* (a b
)
1658 (if (or (zerop (car a
)) (zerop (car b
)))
1660 (list (fpround (* (car a
) (car b
)))
1661 (+ *m
(cadr a
) (cadr b
) (- fpprec
)))))
1663 ;; Don't use the symbol BASE since it is SPECIAL.
1665 (defun fpintexpt (int nn fixprec
) ;INT is integer
1666 (setq fixprec
(truncate fixprec
(1- (integer-length int
)))) ;NN is pos
1667 (let ((bas (intofp (expt int
(min nn fixprec
)))))
1669 (fptimes* (intofp (expt int
(rem nn fixprec
)))
1670 (fpexpt bas
(quotient nn fixprec
)))
1673 ;; NN is positive or negative integer
1675 (defun fpexpt (p nn
)
1676 (cond ((zerop nn
) (fpone))
1678 ((< nn
0) (fpquotient (fpone) (fpexpt p
(- nn
))))
1683 (do ((ii (quotient nn
2) (quotient ii
2)))
1685 (setq p
(fptimes* p p
))
1687 (setq u
(fptimes* u p
))))
1690 (defun exptbigfloat (p n
)
1691 (cond ((equal n
1) p
)
1692 ((equal n
0) ($bfloat
1))
1693 ((not ($bfloatp p
)) (list '(mexpt) p n
))
1694 ((equal (cadr p
) 0) ($bfloat
0))
1695 ((and (< (cadr p
) 0) (ratnump n
))
1696 (mul2 (let ($numer $float $keepfloat $ratprint
)
1698 (exptbigfloat (bcons (fpminus (cdr p
))) n
)))
1699 ((and (< (cadr p
) 0) (not (integerp n
)))
1700 (cond ((or (equal n
0.5) (equal n bfhalf
))
1701 (exptbigfloat p
'((rat simp
) 1 2)))
1702 ((or (equal n -
0.5) (equal n bfmhalf
))
1703 (exptbigfloat p
'((rat simp
) -
1 2)))
1704 (($bfloatp
(setq n
($bfloat n
)))
1705 (cond ((equal n
($bfloat
(fpentier n
)))
1706 (exptbigfloat p
(fpentier n
)))
1707 (t ;; for P<0: P^N = (-P)^N*cos(pi*N) + i*(-P)^N*sin(pi*N)
1708 (setq p
(exptbigfloat (bcons (fpminus (cdr p
))) n
)
1709 n
($bfloat
`((mtimes) $%pi
,n
)))
1710 (add2 ($bfloat
`((mtimes) ,p
,(*fpsin n nil
)))
1711 `((mtimes simp
) ,($bfloat
`((mtimes) ,p
,(*fpsin n t
)))
1713 (t (list '(mexpt) p n
))))
1714 ((and (ratnump n
) (< (caddr n
) 10.
))
1715 (bcons (fpexpt (fproot p
(caddr n
)) (cadr n
))))
1717 (setq n
($bfloat n
))
1719 ((not ($bfloatp n
)) (list '(mexpt) p n
))
1721 (let ((extrabits (max 1 (+ (caddr n
) (integer-length (caddr p
))))))
1723 (let ((fpprec (+ extrabits fpprec
)))
1724 (fpexp (fptimes* (cdr (bigfloatp n
)) (fplog (cdr (bigfloatp p
)))))))
1725 (setq p
(list (fpround (car p
)) (+ (- extrabits
) *m
(cadr p
))))
1727 ;; The number of extra bits required
1728 ((< n
0) (invertbigfloat (exptbigfloat p
(- n
))))
1729 (t (bcons (fpexpt (cdr p
) n
)))))
1731 (defun fproot (a n
) ; computes a^(1/n) see Fitch, SIGSAM Bull Nov 74
1733 ;; Special case for a = 0b0. General algorithm loops endlessly in that case.
1735 ;; Unlike many or maybe all of the other functions named FP-something,
1736 ;; FPROOT assumes it is called with an argument like
1737 ;; '((BIGFLOAT ...) FOO BAR) instead of '(FOO BAR).
1738 ;; However FPROOT does return something like '(FOO BAR).
1743 (let* ((ofprec fpprec
)
1744 (fpprec (+ fpprec
2)) ;assumes a>0 n>=2
1745 (bk (fpexpt (intofp 2) (1+ (quotient (cadr (setq a
(cdr (bigfloatp a
)))) n
)))))
1746 (do ((x bk
(fpdifference x
1747 (setq bk
(fpquotient (fpdifference
1748 x
(fpquotient a
(fpexpt x n1
))) n
))))
1751 ((or (equal bk
'(0 0))
1752 (> (- (cadr x
) (cadr bk
)) ofprec
))
1754 (list (fpround (car a
)) (+ -
2 *m
(cadr a
))))))
1756 (defun timesbigfloat (h)
1757 (prog (fans r nfans
)
1758 (setq fans
(bcons (fpone)) nfans
1)
1761 (if (setq r
(bigfloatp (car l
)))
1762 (setq fans
(bcons (fptimes* (cdr r
) (cdr fans
))))
1763 (setq nfans
(list '(mtimes) (car l
) nfans
))))
1764 (return (if (equal nfans
1)
1766 (simplify (list '(mtimes) fans nfans
))))))
1768 (defun invertbigfloat (a)
1769 ;; If A is a bigfloat, be sure to round it to the current precision.
1770 ;; (See Bug 2543079 for one of the symptoms.)
1771 (let ((b (bigfloatp a
)))
1773 (bcons (fpquotient (fpone) (cdr b
)))
1774 (simplify (list '(mexpt) a -
1)))))
1777 (fpend (let ((fpprec (+ 8. fpprec
)))
1779 (fpexp (cdr (bigfloatp a
)))
1780 (list '(mexpt) '$%e a
)))))
1782 (defun *fpsin
(a fl
)
1783 (fpend (let ((fpprec (+ 8. fpprec
)))
1784 (cond (($bfloatp a
) (fpsin (cdr ($bfloat a
)) fl
))
1785 (fl (list '(%sin
) a
))
1786 (t (list '(%cos
) a
))))))
1789 (cond ((equal (car a
) 0) (bcons a
))
1791 (setq a
(list (fpround (car a
)) (+ -
8.
*m
(cadr a
))))
1795 (defun fparcsimp (e) ; needed for e.g. ASIN(.123567812345678B0) with
1796 ;; FPPREC 16, to get rid of the miniscule imaginary
1797 ;; part of the a+bi answer.
1798 (if (and (mplusp e
) (null (cdddr e
))
1799 (mtimesp (caddr e
)) (null (cdddr (caddr e
)))
1800 ($bfloatp
(cadr (caddr e
)))
1801 (eq (caddr (caddr e
)) '$%i
)
1802 (< (caddr (cadr (caddr e
))) (+ (- fpprec
) 2)))
1806 (defun sinbigfloat (x)
1809 (defun cosbigfloat (x)
1810 (*fpsin
(car x
) nil
))
1812 ;; THIS VERSION OF FPSIN COMPUTES SIN OR COS TO PRECISION FPPREC,
1813 ;; BUT CHECKS FOR THE POSSIBILITY OF CATASTROPHIC CANCELLATION DURING
1814 ;; ARGUMENT REDUCTION (E.G. SIN(N*%PI+EPSILON))
1815 ;; *FPSINCHECK* WILL CAUSE PRINTOUT OF ADDITIONAL INFO WHEN
1816 ;; EXTRA PRECISION IS NEEDED FOR SIN/COS CALCULATION. KNOWN
1817 ;; BAD FEATURES: IT IS NOT NECESSARY TO USE EXTRA PRECISION FOR, E.G.
1818 ;; SIN(PI/2), WHICH IS NOT NEAR ZERO, BUT EXTRA
1819 ;; PRECISION IS USED SINCE IT IS NEEDED FOR COS(PI/2).
1820 ;; PRECISION SEEMS TO BE 100% SATSIFACTORY FOR LARGE ARGUMENTS, E.G.
1821 ;; SIN(31415926.0B0), BUT LESS SO FOR SIN(3.1415926B0). EXPLANATION
1822 ;; NOT KNOWN. (9/12/75 RJF)
1824 (defvar *fpsincheck
* nil
)
1827 (prog (piby2 r sign res k
*cancelled
)
1828 (setq sign
(cond (fl (signp g
(car x
)))
1831 (when (equal (car x
) 0)
1832 (return (if fl
(intofp 0) (intofp 1))))
1836 (let ((fpprec (max fpprec
(+ fpprec
(cadr x
))))
1841 loop
(setq x
(cdr (bigfloatp xt
)))
1842 (setq piby2
(fpquotient (fppi) (intofp 2)))
1843 (setq r
(fpintpart (fpquotient x piby2
)))
1844 (setq x
(fpplus x
(fptimes* (intofp (- r
)) piby2
)))
1846 (fpplus x
(fpminus piby2
))
1847 (setq *cancelled
(max k
*cancelled
))
1849 (print `(*canc
= ,*cancelled fpprec
= ,fpprec oldprec
= ,oldprec
)))
1850 (cond ((not (> oldprec
(- fpprec
*cancelled
)))
1853 (cond (fl (cond ((= r
0) (fpsin1 x
))
1854 ((= r
1) (fpcos1 x
))
1855 ((= r
2) (fpminus (fpsin1 x
)))
1856 ((= r
3) (fpminus (fpcos1 x
)))))
1857 (t (cond ((= r
0) (fpcos1 x
))
1858 ((= r
1) (fpminus (fpsin1 x
)))
1859 ((= r
2) (fpminus (fpcos1 x
)))
1860 ((= r
3) (fpsin1 x
))))))
1861 (return (bcons (if sign res
(fpminus res
)))))
1863 (incf fpprec
*cancelled
)
1869 ;; Compute SIN or COS in (0,PI/2). FL is T for SIN, NIL for COS.
1871 ;; Use Taylor series
1872 (defun fpsincos1 (x fl
)
1873 (prog (ans term oans x2
)
1874 (setq ans
(if fl x
(intofp 1))
1875 x2
(fpminus(fptimes* x x
)))
1877 (do ((n (if fl
3 2) (+ n
2)))
1879 (setq term
(fptimes* term
(fpquotient x2
(intofp (* n
(1- n
))))))
1881 ans
(fpplus ans term
)))
1888 (if (signp ge
(car x
))
1890 (cons (- (car x
)) (cdr x
))))
1892 (defmfun fpentier
(f)
1893 (let ((fpprec (caddar f
)))
1894 (fpintpart (cdr f
))))
1896 ;; Calculate the integer part of a floating point number that is represented as
1899 ;; (MANTISSA EXPONENT)
1901 ;; The special variable fpprec should be bound to the precision (in bits) of the
1902 ;; number. This encodes how many bits are known of the result and also a right
1903 ;; shift. The pair denotes the number MANTISSA * 2^(EXPONENT - FPPREC), of which
1904 ;; FPPREC bits are known.
1906 ;; If EXPONENT is large and positive then we might not have enough information
1907 ;; to calculate the integer part. Specifically, we only have enough information
1908 ;; if EXPONENT < FPPREC. If that isn't the case, we signal a Maxima error.
1909 (defun fpintpart (f)
1910 (destructuring-bind (mantissa exponent
) f
1911 (if (< exponent fpprec
)
1912 (quotient mantissa
(expt 2 (- fpprec exponent
)))
1913 (merror "~M doesn't have enough precision to compute its integer part"
1914 `((bigfloat ,fpprec
) ,mantissa
,exponent
)))))
1916 (defun logbigfloat (a)
1917 (cond (($bfloatp
(car a
))
1918 (big-float-log ($bfloat
(car a
))))
1920 (list '(%log
) (car a
)))))
1923 ;;; Computes the log of a bigfloat number.
1927 ;;; log(1+x) = sum((x/(x+2))^(2*n+1)/(2*n+1),n,0,inf);
1933 ;;; = 2 > --------------
1939 ;;; which converges for x > 0.
1941 ;;; Note that FPLOG is given 1+X, not X.
1943 ;;; However, to aid convergence of the series, we scale 1+x until 1/e
1947 (prog (over two ans oldans term e sum
)
1948 (unless (> (car x
) 0)
1949 (merror (intl:gettext
"fplog: argument must be positive; found: ~M") (car x
)))
1951 over
(fpquotient (fpone) e
)
1953 ;; Scale X until 1/e < X <= E. ANS keeps track of how
1954 ;; many factors of E were used. Set X to NIL if X is E.
1957 (cond ((equal x e
) (setq x nil
) (return nil
))
1958 ((and (fplessp x e
) (fplessp over x
))
1961 (setq x
(fptimes* x e
))
1965 (setq x
(fpquotient x e
)))))
1966 (when (null x
) (return (intofp (1+ ans
))))
1967 ;; Prepare X for the series. The series is for 1 + x, so
1968 ;; get x from our X. TERM is (x/(x+2)). X becomes
1970 (setq x
(fpdifference x
(fpone))
1972 (setq x
(fpexpt (setq term
(fpquotient x
(fpplus x
(setq two
(intofp 2))))) 2))
1973 ;; Sum the series until the sum (in ANS) doesn't change
1975 (setq sum
(intofp 0))
1977 ((equal sum oldans
))
1979 (setq sum
(fpplus sum
(fpquotient term
(intofp n
))))
1980 (setq term
(fptimes* term x
)))
1981 (return (fpplus ans
(fptimes* two sum
)))))
1983 (defun mabsbigfloat (l)
1985 (setq r
(bigfloatp (car l
)))
1986 (return (if (null r
)
1987 (list '(mabs) (car l
))
1988 (bcons (fpabs (cdr r
)))))))
1991 ;;;; Bigfloat implementations of special functions.
1994 ;;; This is still a bit messy. Some functions here take bigfloat
1995 ;;; numbers, represented by ((bigfloat) <mant> <exp>), but others want
1996 ;;; just the FP number, represented by (<mant> <exp>). Likewise, some
1997 ;;; return a bigfloat, some return just the FP.
1999 ;;; This needs to be systemized somehow. It isn't helped by the fact
2000 ;;; that some of the routines above also do the samething.
2002 ;;; The implementation for the special functions for a complex
2003 ;;; argument are mostly taken from W. Kahan, "Branch Cuts for Complex
2004 ;;; Elementary Functions or Much Ado About Nothing's Sign Bit", in
2005 ;;; Iserles and Powell (eds.) "The State of the Art in Numerical
2006 ;;; Analysis", pp 165-211, Clarendon Press, 1987
2008 ;; Compute exp(x) - 1, but do it carefully to preserve precision when
2009 ;; |x| is small. X is a FP number, and a FP number is returned. That
2010 ;; is, no bigfloat stuff.
2012 ;; What is the right breakpoint here? Is 1 ok? Perhaps 1/e is better?
2013 (cond ((fpgreaterp (fpabs x
) (fpone))
2015 (fpdifference (fpexp x
) (fpone)))
2017 ;; Use Taylor series for exp(x) - 1
2023 (setf term
(fpquotient (fptimes* x term
) (intofp n
)))
2025 (setf ans
(fpplus ans term
)))
2028 ;; log(1+x) for small x. X is FP number, and a FP number is returned.
2030 ;; Use the same series as given above for fplog. For small x we use
2031 ;; the series, otherwise fplog is accurate enough.
2032 (cond ((fpgreaterp (fpabs x
) (fpone))
2033 (fplog (fpplus x
(fpone))))
2035 (let* ((sum (intofp 0))
2036 (term (fpquotient x
(fpplus x
(intofp 2))))
2037 (f (fptimes* term term
))
2040 ((equal sum oldans
))
2042 (setq sum
(fpplus sum
(fpquotient term
(intofp n
))))
2043 (setq term
(fptimes* term f
)))
2044 (fptimes* sum
(intofp 2))))))
2046 ;; sinh(x) for real x. X is a bigfloat, and a bigfloat is returned.
2048 ;; X must be a maxima bigfloat
2050 ;; See, for example, Hart et al., Computer Approximations, 6.2.27:
2052 ;; sinh(x) = 1/2*(D(x) + D(x)/(1+D(x)))
2054 ;; where D(x) = exp(x) - 1.
2056 ;; But for negative x, use sinh(x) = -sinh(-x) because D(x)
2057 ;; approaches -1 for large negative x.
2058 (cond ((equal 0 (cadr x
))
2059 ;; Special case: x=0. Return immediately.
2063 (let ((d (fpexpm1 (cdr (bigfloatp x
)))))
2064 (bcons (fpquotient (fpplus d
(fpquotient d
(fpplus d
(fpone))))
2069 (fpminus (cdr (fpsinh (bcons (fpminus (cdr (bigfloatp x
)))))))))))
2071 (defun big-float-sinh (x &optional y
)
2072 ;; The rectform for sinh for complex args should be numerically
2073 ;; accurate, so return nil in that case.
2077 ;; asinh(x) for real x. X is a bigfloat, and a bigfloat is returned.
2079 ;; asinh(x) = sign(x) * log(|x| + sqrt(1+x*x))
2083 ;; asinh(x) = x, if 1+x*x = 1
2084 ;; = sign(x) * (log(2) + log(x)), large |x|
2085 ;; = sign(x) * log(2*|x| + 1/(|x|+sqrt(1+x*x))), if |x| > 2
2086 ;; = sign(x) * log1p(|x|+x^2/(1+sqrt(1+x*x))), otherwise.
2088 ;; But I'm lazy right now and we only implement the last 2 cases.
2089 ;; We should implement all cases.
2090 (let* ((fp-x (cdr (bigfloatp x
)))
2094 (minus (minusp (car fp-x
)))
2096 ;; We only use two formulas here. |x| <= 2 and |x| > 2. Should
2097 ;; we add one for very big x and one for very small x, as given above.
2098 (cond ((fpgreaterp absx two
)
2101 ;; log(2*|x| + 1/(|x|+sqrt(1+x^2)))
2102 (setf result
(fplog (fpplus (fptimes* absx two
)
2105 (fproot (bcons (fpplus one
2106 (fptimes* absx absx
)))
2111 ;; log1p(|x|+x^2/(1+sqrt(1+x^2)))
2112 (let ((x*x
(fptimes* absx absx
)))
2113 (setq result
(fplog1p (fpplus absx
2116 (fproot (bcons (fpplus one x
*x
))
2119 (bcons (fpminus result
))
2122 (defun complex-asinh (x y
)
2123 ;; asinh(z) = -%i * asin(%i*z)
2124 (multiple-value-bind (u v
)
2125 (complex-asin (mul -
1 y
) x
)
2126 (values v
(bcons (fpminus (cdr u
))))))
2128 (defun big-float-asinh (x &optional y
)
2130 (multiple-value-bind (u v
)
2132 (add u
(mul '$%i v
)))
2135 (defun fpasin-core (x)
2136 ;; asin(x) = atan(x/(sqrt(1-x^2))
2137 ;; = sgn(x)*[%pi/2 - atan(sqrt(1-x^2)/abs(x))]
2139 ;; Use the first for 0 <= x < 1/2 and the latter for 1/2 < x <= 1.
2141 ;; If |x| > 1, we need to do something else.
2143 ;; asin(x) = -%i*log(sqrt(1-x^2)+%i*x)
2144 ;; = -%i*log(%i*x + %i*sqrt(x^2-1))
2145 ;; = -%i*[log(|x + sqrt(x^2-1)|) + %i*%pi/2]
2146 ;; = %pi/2 - %i*log(|x+sqrt(x^2-1)|)
2148 (let ((fp-x (cdr (bigfloatp x
))))
2149 (cond ((minusp (car fp-x
))
2150 ;; asin(-x) = -asin(x);
2151 (mul -
1 (fpasin (bcons (fpminus fp-x
)))))
2152 ((fplessp fp-x
(cdr bfhalf
))
2154 ;; asin(x) = atan(x/sqrt(1-x^2))
2156 (fpatan (fpquotient fp-x
2158 (fptimes* (fpdifference (fpone) fp-x
)
2159 (fpplus (fpone) fp-x
)))
2161 ((fpgreaterp fp-x
(fpone))
2163 ;; asin(x) = %pi/2 - %i*log(|x+sqrt(x^2-1)|)
2165 ;; Should we try to do something a little fancier with the
2166 ;; argument to log and use log1p for better accuracy?
2167 (let ((arg (fpplus fp-x
2168 (fproot (bcons (fptimes* (fpdifference fp-x
(fpone))
2169 (fpplus fp-x
(fpone))))
2172 (mul -
1 '$%i
(bcons (fplog arg
))))))
2176 ;; asin(x) = %pi/2 - atan(sqrt(1-x^2)/x)
2181 (fpquotient (fproot (bcons (fptimes* (fpdifference (fpone) fp-x
)
2182 (fpplus (fpone) fp-x
)))
2186 ;; asin(x) for real x. X is a bigfloat, and a maxima number (real or
2187 ;; complex) is returned.
2189 ;; asin(x) = atan(x/(sqrt(1-x^2))
2190 ;; = sgn(x)*[%pi/2 - atan(sqrt(1-x^2)/abs(x))]
2192 ;; Use the first for 0 <= x < 1/2 and the latter for 1/2 < x <= 1.
2194 ;; If |x| > 1, we need to do something else.
2196 ;; asin(x) = -%i*log(sqrt(1-x^2)+%i*x)
2197 ;; = -%i*log(%i*x + %i*sqrt(x^2-1))
2198 ;; = -%i*[log(|x + sqrt(x^2-1)|) + %i*%pi/2]
2199 ;; = %pi/2 - %i*log(|x+sqrt(x^2-1)|)
2201 ($bfloat
(fpasin-core x
)))
2203 ;; Square root of a complex number (xx, yy). Both are bigfloats. FP
2204 ;; (non-bigfloat) numbers are returned.
2205 (defun complex-sqrt (xx yy
)
2206 (let* ((x (cdr (bigfloatp xx
)))
2207 (y (cdr (bigfloatp yy
)))
2208 (rho (fpplus (fptimes* x x
)
2210 (setf rho
(fpplus (fpabs x
) (fproot (bcons rho
) 2)))
2211 (setf rho
(fpplus rho rho
))
2212 (setf rho
(fpquotient (fproot (bcons rho
) 2) (intofp 2)))
2216 (when (fpgreaterp rho
(intofp 0))
2217 (setf nu
(fpquotient (fpquotient nu rho
) (intofp 2)))
2218 (when (fplessp x
(intofp 0))
2219 (setf eta
(fpabs nu
))
2220 (setf nu
(if (minusp (car y
))
2225 ;; asin(z) for complex z = x + %i*y. X and Y are bigfloats. The real
2226 ;; and imaginary parts are returned as bigfloat numbers.
2227 (defun complex-asin (x y
)
2228 (let ((x (cdr (bigfloatp x
)))
2229 (y (cdr (bigfloatp y
))))
2230 (multiple-value-bind (re-sqrt-1-z im-sqrt-1-z
)
2231 (complex-sqrt (bcons (fpdifference (intofp 1) x
))
2232 (bcons (fpminus y
)))
2233 (multiple-value-bind (re-sqrt-1+z im-sqrt-1
+z
)
2234 (complex-sqrt (bcons (fpplus (intofp 1) x
))
2236 ;; Realpart is atan(x/Re(sqrt(1-z)*sqrt(1+z)))
2237 ;; Imagpart is asinh(Im(conj(sqrt(1-z))*sqrt(1+z)))
2239 (let ((d (fpdifference (fptimes* re-sqrt-1-z
2241 (fptimes* im-sqrt-1-z
2243 ;; Check for division by zero. If we would divide
2244 ;; by zero, return pi/2 or -pi/2 according to the
2246 (cond ((equal d
'(0 0))
2247 (if (fplessp x
'(0 0))
2248 (fpminus (fpquotient (fppi) (intofp 2)))
2249 (fpquotient (fppi) (intofp 2))))
2251 (fpatan (fpquotient x d
))))))
2253 (fpdifference (fptimes* re-sqrt-1-z
2255 (fptimes* im-sqrt-1-z
2256 re-sqrt-1
+z
)))))))))
2258 (defun big-float-asin (x &optional y
)
2260 (multiple-value-bind (u v
) (complex-asin x y
)
2261 (add u
(mul '$%i v
)))
2265 ;; tanh(x) for real x. X is a bigfloat, and a bigfloat is returned.
2267 ;; X is Maxima bigfloat
2268 ;; tanh(x) = D(2*x)/(2+D(2*x))
2269 (let* ((two (intofp 2))
2270 (fp (cdr (bigfloatp x
)))
2271 (d (fpexpm1 (fptimes* fp two
))))
2272 (bcons (fpquotient d
(fpplus d two
)))))
2274 ;; tanh(z), z = x + %i*y. X, Y are bigfloats, and a maxima number is
2276 (defun complex-tanh (x y
)
2277 (let* ((tv (cdr (tanbigfloat (list y
))))
2278 (beta (fpplus (fpone) (fptimes* tv tv
)))
2279 (s (cdr (fpsinh x
)))
2280 (s^
2 (fptimes* s s
))
2281 (rho (fproot (bcons (fpplus (fpone) s^
2)) 2))
2282 (den (fpplus (fpone) (fptimes* beta s^
2))))
2283 (values (bcons (fpquotient (fptimes* beta
(fptimes* rho s
)) den
))
2284 (bcons (fpquotient tv den
)))))
2286 (defun big-float-tanh (x &optional y
)
2288 (multiple-value-bind (u v
) (complex-tanh x y
)
2289 (add u
(mul '$%i v
)))
2292 ;; atanh(x) for real x, |x| <= 1. X is a bigfloat, and a bigfloat is
2295 ;; atanh(x) = -atanh(-x)
2296 ;; = 1/2*log1p(2*x/(1-x)), x >= 0.5
2297 ;; = 1/2*log1p(2*x+2*x*x/(1-x)), x <= 0.5
2299 (let* ((fp-x (cdr (bigfloatp x
))))
2300 (cond ((fplessp fp-x
(intofp 0))
2301 ;; atanh(x) = -atanh(-x)
2302 (mul -
1 (fpatanh (bcons (fpminus fp-x
)))))
2303 ((fpgreaterp fp-x
(fpone))
2304 ;; x > 1, so use complex version.
2305 (multiple-value-bind (u v
)
2306 (complex-atanh x
(bcons (intofp 0)))
2307 (add u
(mul '$%i v
))))
2308 ((fpgreaterp fp-x
(cdr bfhalf
))
2309 ;; atanh(x) = 1/2*log1p(2*x/(1-x))
2311 (fptimes* (cdr bfhalf
)
2312 (fplog1p (fpquotient (fptimes* (intofp 2) fp-x
)
2313 (fpdifference (fpone) fp-x
))))))
2315 ;; atanh(x) = 1/2*log1p(2*x + 2*x*x/(1-x))
2316 (let ((2x (fptimes* (intofp 2) fp-x
)))
2318 (fptimes* (cdr bfhalf
)
2320 (fpquotient (fptimes* 2x fp-x
)
2321 (fpdifference (fpone) fp-x
)))))))))))
2323 ;; Stuff which follows is derived from atanh z = (log(1 + z) - log(1 - z))/2
2324 ;; which apparently originates with Kahan's "Much ado" paper.
2326 ;; The formulas for eta and nu below can be easily derived from
2327 ;; rectform(atanh(x+%i*y)) =
2329 ;; 1/4*log(((1+x)^2+y^2)/((1-x)^2+y^2)) + %i/2*(arg(1+x+%i*y)-arg(1-x+%i*(-y)))
2331 ;; Expand the argument of log out and divide it out and we get
2333 ;; log(((1+x)^2+y^2)/((1-x)^2+y^2)) = log(1+4*x/((1-x)^2+y^2))
2335 ;; When y = 0, Im atanh z = 1/2 (arg(1 + x) - arg(1 - x))
2336 ;; = if x < -1 then %pi/2 else if x > 1 then -%pi/2 else <whatever>
2338 ;; Otherwise, arg(1 - x + %i*(-y)) = - arg(1 - x + %i*y),
2339 ;; and Im atanh z = 1/2 (arg(1 + x + %i*y) + arg(1 - x + %i*y)).
2340 ;; Since arg(x)+arg(y) = arg(x*y) (almost), we can simplify the
2341 ;; imaginary part to
2343 ;; arg((1+x+%i*y)*(1-x+%i*y)) = arg((1-x)*(1+x)-y^2+2*y*%i)
2344 ;; = atan2(2*y,((1-x)*(1+x)-y^2))
2346 ;; These are the eta and nu forms below.
2347 (defun complex-atanh (x y
)
2348 (let* ((fpx (cdr (bigfloatp x
)))
2349 (fpy (cdr (bigfloatp y
)))
2350 (beta (if (minusp (car fpx
))
2353 (x-lt-minus-1 (mevalp `((mlessp) ,x -
1)))
2354 (x-gt-plus-1 (mevalp `((mgreaterp) ,x
1)))
2355 (y-equals-0 (like y
'((bigfloat) 0 0)))
2356 (x (fptimes* beta fpx
))
2357 (y (fptimes* beta
(fpminus fpy
)))
2358 ;; Kahan has rho = 4/most-positive-float. What should we do
2359 ;; here about that? Our big floats don't really have a
2360 ;; most-positive float value.
2362 (t1 (fpplus (fpabs y
) rho
))
2363 (t1^
2 (fptimes* t1 t1
))
2364 (1-x (fpdifference (fpone) x
))
2365 ;; eta = log(1+4*x/((1-x)^2+y^2))/4
2367 (fplog1p (fpquotient (fptimes* (intofp 4) x
)
2368 (fpplus (fptimes* 1-x
1-x
)
2371 ;; If y = 0, then Im atanh z = %pi/2 or -%pi/2.
2372 ;; Otherwise nu = 1/2*atan2(2*y,(1-x)*(1+x)-y^2)
2374 ;; EXTRA FPMINUS HERE TO COUNTERACT FPMINUS IN RETURN VALUE
2375 (fpminus (if x-lt-minus-1
2376 (cdr ($bfloat
'((mquotient) $%pi
2)))
2378 (cdr ($bfloat
'((mminus) ((mquotient) $%pi
2))))
2379 (merror "COMPLEX-ATANH: HOW DID I GET HERE?"))))
2380 (fptimes* (cdr bfhalf
)
2381 (fpatan2 (fptimes* (intofp 2) y
)
2382 (fpdifference (fptimes* 1-x
(fpplus (fpone) x
))
2384 (values (bcons (fptimes* beta eta
))
2385 ;; WTF IS FPMINUS DOING HERE ??
2386 (bcons (fpminus (fptimes* beta nu
))))))
2388 (defun big-float-atanh (x &optional y
)
2390 (multiple-value-bind (u v
) (complex-atanh x y
)
2391 (add u
(mul '$%i v
)))
2394 ;; acos(x) for real x. X is a bigfloat, and a maxima number is returned.
2396 ;; acos(x) = %pi/2 - asin(x)
2397 ($bfloat
(add (div '$%pi
2) (mul -
1 (fpasin-core x
)))))
2399 (defun complex-acos (x y
)
2400 (let ((x (cdr (bigfloatp x
)))
2401 (y (cdr (bigfloatp y
))))
2402 (multiple-value-bind (re-sqrt-1-z im-sqrt-1-z
)
2403 (complex-sqrt (bcons (fpdifference (intofp 1) x
))
2404 (bcons (fpminus y
)))
2405 (multiple-value-bind (re-sqrt-1+z im-sqrt-1
+z
)
2406 (complex-sqrt (bcons (fpplus (intofp 1) x
))
2409 (fptimes* (intofp 2)
2410 (fpatan (fpquotient re-sqrt-1-z re-sqrt-1
+z
))))
2413 (fptimes* re-sqrt-1
+z im-sqrt-1-z
)
2414 (fptimes* im-sqrt-1
+z re-sqrt-1-z
)))))))))
2417 (defun big-float-acos (x &optional y
)
2419 (multiple-value-bind (u v
) (complex-acos x y
)
2420 (add u
(mul '$%i v
)))
2423 (defun complex-log (x y
)
2424 (let* ((x (cdr (bigfloatp x
)))
2425 (y (cdr (bigfloatp y
)))
2426 (t1 (let (($float2bf t
))
2427 ;; No warning message, please.
2430 (rho (fpplus (fptimes* x x
)
2434 (beta (fpmax abs-x abs-y
))
2435 (theta (fpmin abs-x abs-y
)))
2436 (values (if (or (fpgreaterp t1 beta
)
2438 (fpquotient (fplog1p (fpplus (fptimes* (fpdifference beta
(fpone))
2439 (fpplus beta
(fpone)))
2440 (fptimes* theta theta
)))
2442 (fpquotient (fplog rho
) (intofp 2)))
2445 (defun big-float-log (x &optional y
)
2447 (multiple-value-bind (u v
) (complex-log x y
)
2448 (add (bcons u
) (mul '$%i
(bcons v
))))
2450 ;; x is (mantissa exp), where mantissa = frac*2^fpprec,
2451 ;; with 1/2 < frac <= 1 and x is frac*2^exp. To
2452 ;; compute log(x), use log(x) = log(frac)+ exp*log(2).
2455 (fpprec (+ fpprec extra
))
2459 (cl-rat-to-maxima (/ (car x
)
2460 (ash 1 (- fpprec
8))))))
2461 (list (ash (car x
) extra
) 0)))
2462 (log-exp (fptimes* (intofp (second x
)) (fplog2)))
2463 (result (bcons (fpplus log-frac log-exp
))))
2464 (let ((fpprec (- fpprec extra
)))
2465 (bigfloatp result
))))))
2466 (let ((fp-x (cdr (bigfloatp x
))))
2468 ;; Special case for log(1). See Bug 3381301:
2469 ;; https://sourceforge.net/tracker/?func=detail&aid=3381301&group_id=4933&atid=104933
2471 ((fplessp fp-x
(intofp 0))
2472 ;; ??? Do we want to return an exact %i*%pi or a float
2474 (add (big-float-log (bcons (fpminus fp-x
)))
2475 (mul '$%i
(bcons (fppi)))))
2477 (bcons (%log fp-x
))))))))
2479 (defun big-float-sqrt (x &optional y
)
2481 (multiple-value-bind (u v
) (complex-sqrt x y
)
2482 (add (bcons u
) (mul '$%i
(bcons v
))))
2483 (let ((fp-x (cdr (bigfloatp x
))))
2484 (if (fplessp fp-x
(intofp 0))
2485 (mul '$%i
(bcons (fproot (bcons (fpminus fp-x
)) 2)))
2486 (bcons (fproot x
2))))))
2490 #-gcl
(:load-toplevel
:execute
)
2491 (fpprec1 nil $fpprec
)) ; Set up user's precision