1 ;;;; This file contains all the irrational functions. (Actually, most
2 ;;;; of the work is done by calling out to C.)
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!KERNEL")
15 ;;;; miscellaneous constants, utility functions, and macros
17 (defconstant pi 3.14159265358979323846264338327950288419716939937511L0)
18 ;(defconstant e 2.71828182845904523536028747135266249775724709369996L0)
20 ;;; Make these INLINE, since the call to C is at least as compact as a
21 ;;; Lisp call, and saves number consing to boot.
22 (eval-when (:compile-toplevel :execute)
24 (sb!xc:defmacro def-math-rtn (name num-args)
25 (let ((function (symbolicate "%" (string-upcase name))))
27 (proclaim '(inline ,function))
28 (sb!alien:def-alien-routine (,name ,function) double-float
29 ,@(let ((results nil))
30 (dotimes (i num-args (nreverse results))
31 (push (list (intern (format nil "ARG-~D" i))
35 (defun handle-reals (function var)
36 `((((foreach fixnum single-float bignum ratio))
37 (coerce (,function (coerce ,var 'double-float)) 'single-float))
43 ;;;; stubs for the Unix math library
45 ;;; Please refer to the Unix man pages for details about these routines.
48 #!-x86 (def-math-rtn "sin" 1)
49 #!-x86 (def-math-rtn "cos" 1)
50 #!-x86 (def-math-rtn "tan" 1)
51 (def-math-rtn "asin" 1)
52 (def-math-rtn "acos" 1)
53 #!-x86 (def-math-rtn "atan" 1)
54 #!-x86 (def-math-rtn "atan2" 2)
55 (def-math-rtn "sinh" 1)
56 (def-math-rtn "cosh" 1)
57 (def-math-rtn "tanh" 1)
58 (def-math-rtn "asinh" 1)
59 (def-math-rtn "acosh" 1)
60 (def-math-rtn "atanh" 1)
62 ;;; exponential and logarithmic
63 #!-x86 (def-math-rtn "exp" 1)
64 #!-x86 (def-math-rtn "log" 1)
65 #!-x86 (def-math-rtn "log10" 1)
66 (def-math-rtn "pow" 2)
67 #!-x86 (def-math-rtn "sqrt" 1)
68 (def-math-rtn "hypot" 2)
69 #!-(or hpux x86) (def-math-rtn "log1p" 1)
71 #!+x86 ;; These are needed for use by byte-compiled files.
74 (declare (double-float x)
75 (values double-float))
78 (declare (double-float x)
79 (values double-float))
82 (declare (double-float x)
83 (values double-float))
86 (declare (double-float x)
87 (values double-float))
90 (declare (double-float x)
91 (values double-float))
94 (declare (double-float x)
95 (values double-float))
98 (declare (double-float x)
99 (values double-float))
102 (declare (double-float x y)
103 (values double-float))
106 (declare (double-float x)
107 (values double-float))
110 (declare (double-float x)
111 (values double-float))
114 (declare (double-float x)
115 (values double-float))
119 (declare (type (double-float 0d0) x)
121 (values (double-float 0d0)))
124 (declare (double-float x)
125 (values double-float))
127 (defun %scalbn (f ex)
128 (declare (double-float f)
129 (type (signed-byte 32) ex)
130 (values double-float))
133 (declare (double-float f ex)
134 (values double-float))
137 (declare (double-float x)
138 (values double-float))
141 (declare (double-float x)
142 (values double-float))
150 "Return e raised to the power NUMBER."
151 (number-dispatch ((number number))
152 (handle-reals %exp number)
154 (* (exp (realpart number))
155 (cis (imagpart number))))))
157 ;;; INTEXP -- Handle the rational base, integer power case.
159 ;;; FIXME: As long as the system dies on stack overflow or memory
160 ;;; exhaustion, it seems reasonable to have this, but its default
161 ;;; should be NIL, and when it's NIL, anything should be accepted.
162 (defparameter *intexp-maximum-exponent* 10000)
164 ;;; This function precisely calculates base raised to an integral
165 ;;; power. It separates the cases by the sign of power, for efficiency
166 ;;; reasons, as powers can be calculated more efficiently if power is
167 ;;; a positive integer. Values of power are calculated as positive
168 ;;; integers, and inverted if negative.
169 (defun intexp (base power)
170 (when (> (abs power) *intexp-maximum-exponent*)
171 ;; FIXME: should be ordinary error, not CERROR. (Once we set the
172 ;; default for the variable to NIL, the un-continuable error will
173 ;; be less obnoxious.)
174 (cerror "Continue with calculation."
175 "The absolute value of ~S exceeds ~S."
176 power '*intexp-maximum-exponent* base power))
177 (cond ((minusp power)
178 (/ (intexp base (- power))))
182 (do ((nextn (ash power -1) (ash power -1))
183 (total (if (oddp power) base 1)
184 (if (oddp power) (* base total) total)))
185 ((zerop nextn) total)
186 (setq base (* base base))
187 (setq power nextn)))))
189 ;;; If an integer power of a rational, use INTEXP above. Otherwise, do
190 ;;; floating point stuff. If both args are real, we try %POW right
191 ;;; off, assuming it will return 0 if the result may be complex. If
192 ;;; so, we call COMPLEX-POW which directly computes the complex
193 ;;; result. We also separate the complex-real and real-complex cases
194 ;;; from the general complex case.
195 (defun expt (base power)
197 "Returns BASE raised to the POWER."
200 (labels (;; determine if the double float is an integer.
201 ;; 0 - not an integer
205 (declare (type (unsigned-byte 31) ihi)
206 (type (unsigned-byte 32) lo)
207 (optimize (speed 3) (safety 0)))
209 (declare (type fixnum isint))
210 (cond ((>= ihi #x43400000) ; exponent >= 53
213 (let ((k (- (ash ihi -20) #x3ff))) ; exponent
214 (declare (type (mod 53) k))
216 (let* ((shift (- 52 k))
217 (j (logand (ash lo (- shift))))
219 (declare (type (mod 32) shift)
220 (type (unsigned-byte 32) j j2))
222 (setq isint (- 2 (logand j 1))))))
224 (let* ((shift (- 20 k))
225 (j (ash ihi (- shift)))
227 (declare (type (mod 32) shift)
228 (type (unsigned-byte 31) j j2))
230 (setq isint (- 2 (logand j 1))))))))))
232 (real-expt (x y rtype)
233 (let ((x (coerce x 'double-float))
234 (y (coerce y 'double-float)))
235 (declare (double-float x y))
236 (let* ((x-hi (sb!kernel:double-float-high-bits x))
237 (x-lo (sb!kernel:double-float-low-bits x))
238 (x-ihi (logand x-hi #x7fffffff))
239 (y-hi (sb!kernel:double-float-high-bits y))
240 (y-lo (sb!kernel:double-float-low-bits y))
241 (y-ihi (logand y-hi #x7fffffff)))
242 (declare (type (signed-byte 32) x-hi y-hi)
243 (type (unsigned-byte 31) x-ihi y-ihi)
244 (type (unsigned-byte 32) x-lo y-lo))
246 (when (zerop (logior y-ihi y-lo))
247 (return-from real-expt (coerce 1d0 rtype)))
249 (when (or (> x-ihi #x7ff00000)
250 (and (= x-ihi #x7ff00000) (/= x-lo 0))
252 (and (= y-ihi #x7ff00000) (/= y-lo 0)))
253 (return-from real-expt (coerce (+ x y) rtype)))
254 (let ((yisint (if (< x-hi 0) (isint y-ihi y-lo) 0)))
255 (declare (type fixnum yisint))
256 ;; special value of y
257 (when (and (zerop y-lo) (= y-ihi #x7ff00000))
259 (return-from real-expt
260 (cond ((and (= x-ihi #x3ff00000) (zerop x-lo))
262 (coerce (- y y) rtype))
263 ((>= x-ihi #x3ff00000)
264 ;; (|x|>1)**+-inf = inf,0
269 ;; (|x|<1)**-,+inf = inf,0
272 (coerce 0 rtype))))))
274 (let ((abs-x (abs x)))
275 (declare (double-float abs-x))
276 ;; special value of x
277 (when (and (zerop x-lo)
278 (or (= x-ihi #x7ff00000) (zerop x-ihi)
279 (= x-ihi #x3ff00000)))
280 ;; x is +-0,+-inf,+-1
281 (let ((z (if (< y-hi 0)
282 (/ 1 abs-x) ; z = (1/|x|)
284 (declare (double-float z))
286 (cond ((and (= x-ihi #x3ff00000) (zerop yisint))
288 (let ((y*pi (* y pi)))
289 (declare (double-float y*pi))
290 (return-from real-expt
292 (coerce (%cos y*pi) rtype)
293 (coerce (%sin y*pi) rtype)))))
295 ;; (x<0)**odd = -(|x|**odd)
297 (return-from real-expt (coerce z rtype))))
301 (coerce (sb!kernel::%pow x y) rtype)
303 (let ((pow (sb!kernel::%pow abs-x y)))
304 (declare (double-float pow))
307 (coerce (* -1d0 pow) rtype))
311 (let ((y*pi (* y pi)))
312 (declare (double-float y*pi))
314 (coerce (* pow (%cos y*pi))
316 (coerce (* pow (%sin y*pi))
318 (declare (inline real-expt))
319 (number-dispatch ((base number) (power number))
320 (((foreach fixnum (or bignum ratio) (complex rational)) integer)
322 (((foreach single-float double-float) rational)
323 (real-expt base power '(dispatch-type base)))
324 (((foreach fixnum (or bignum ratio) single-float)
325 (foreach ratio single-float))
326 (real-expt base power 'single-float))
327 (((foreach fixnum (or bignum ratio) single-float double-float)
329 (real-expt base power 'double-float))
330 ((double-float single-float)
331 (real-expt base power 'double-float))
332 (((foreach (complex rational) (complex float)) rational)
333 (* (expt (abs base) power)
334 (cis (* power (phase base)))))
335 (((foreach fixnum (or bignum ratio) single-float double-float)
337 (if (and (zerop base) (plusp (realpart power)))
339 (exp (* power (log base)))))
340 (((foreach (complex float) (complex rational))
341 (foreach complex double-float single-float))
342 (if (and (zerop base) (plusp (realpart power)))
344 (exp (* power (log base)))))))))
346 (defun log (number &optional (base nil base-p))
348 "Return the logarithm of NUMBER in the base BASE, which defaults to e."
352 (/ (log number) (log base)))
353 (number-dispatch ((number number))
354 (((foreach fixnum bignum ratio))
356 (complex (log (- number)) (coerce pi 'single-float))
357 (coerce (%log (coerce number 'double-float)) 'single-float)))
358 (((foreach single-float double-float))
359 ;; Is (log -0) -infinity (libm.a) or -infinity + i*pi (Kahan)?
360 ;; Since this doesn't seem to be an implementation issue
361 ;; I (pw) take the Kahan result.
362 (if (< (float-sign number)
363 (coerce 0 '(dispatch-type number)))
364 (complex (log (- number)) (coerce pi '(dispatch-type number)))
365 (coerce (%log (coerce number 'double-float))
366 '(dispatch-type number))))
368 (complex-log number)))))
372 "Return the square root of NUMBER."
373 (number-dispatch ((number number))
374 (((foreach fixnum bignum ratio))
376 (complex-sqrt number)
377 (coerce (%sqrt (coerce number 'double-float)) 'single-float)))
378 (((foreach single-float double-float))
380 (complex-sqrt number)
381 (coerce (%sqrt (coerce number 'double-float))
382 '(dispatch-type number))))
384 (complex-sqrt number))))
386 ;;;; trigonometic and related functions
390 "Returns the absolute value of the number."
391 (number-dispatch ((number number))
392 (((foreach single-float double-float fixnum rational))
395 (let ((rx (realpart number))
396 (ix (imagpart number)))
399 (sqrt (+ (* rx rx) (* ix ix))))
401 (coerce (%hypot (coerce rx 'double-float)
402 (coerce ix 'double-float))
407 (defun phase (number)
409 "Return the angle part of the polar representation of a complex number.
410 For complex numbers, this is (atan (imagpart number) (realpart number)).
411 For non-complex positive numbers, this is 0. For non-complex negative
416 (coerce pi 'single-float)
419 (if (minusp (float-sign number))
420 (coerce pi 'single-float)
423 (if (minusp (float-sign number))
424 (coerce pi 'double-float)
427 (atan (imagpart number) (realpart number)))))
431 "Return the sine of NUMBER."
432 (number-dispatch ((number number))
433 (handle-reals %sin number)
435 (let ((x (realpart number))
436 (y (imagpart number)))
437 (complex (* (sin x) (cosh y))
438 (* (cos x) (sinh y)))))))
442 "Return the cosine of NUMBER."
443 (number-dispatch ((number number))
444 (handle-reals %cos number)
446 (let ((x (realpart number))
447 (y (imagpart number)))
448 (complex (* (cos x) (cosh y))
449 (- (* (sin x) (sinh y))))))))
453 "Return the tangent of NUMBER."
454 (number-dispatch ((number number))
455 (handle-reals %tan number)
457 (complex-tan number))))
461 "Return cos(Theta) + i sin(Theta), AKA exp(i Theta)."
462 (declare (type real theta))
463 (complex (cos theta) (sin theta)))
467 "Return the arc sine of NUMBER."
468 (number-dispatch ((number number))
470 (if (or (> number 1) (< number -1))
471 (complex-asin number)
472 (coerce (%asin (coerce number 'double-float)) 'single-float)))
473 (((foreach single-float double-float))
474 (if (or (> number (coerce 1 '(dispatch-type number)))
475 (< number (coerce -1 '(dispatch-type number))))
476 (complex-asin number)
477 (coerce (%asin (coerce number 'double-float))
478 '(dispatch-type number))))
480 (complex-asin number))))
484 "Return the arc cosine of NUMBER."
485 (number-dispatch ((number number))
487 (if (or (> number 1) (< number -1))
488 (complex-acos number)
489 (coerce (%acos (coerce number 'double-float)) 'single-float)))
490 (((foreach single-float double-float))
491 (if (or (> number (coerce 1 '(dispatch-type number)))
492 (< number (coerce -1 '(dispatch-type number))))
493 (complex-acos number)
494 (coerce (%acos (coerce number 'double-float))
495 '(dispatch-type number))))
497 (complex-acos number))))
499 (defun atan (y &optional (x nil xp))
501 "Return the arc tangent of Y if X is omitted or Y/X if X is supplied."
504 (declare (type double-float y x)
505 (values double-float))
508 (if (plusp (float-sign x))
511 (float-sign y (/ pi 2)))
513 (number-dispatch ((y number) (x number))
515 (foreach double-float single-float fixnum bignum ratio))
516 (atan2 y (coerce x 'double-float)))
517 (((foreach single-float fixnum bignum ratio)
519 (atan2 (coerce y 'double-float) x))
520 (((foreach single-float fixnum bignum ratio)
521 (foreach single-float fixnum bignum ratio))
522 (coerce (atan2 (coerce y 'double-float) (coerce x 'double-float))
524 (number-dispatch ((y number))
525 (handle-reals %atan y)
529 ;; It seems that everyone has a C version of sinh, cosh, and
530 ;; tanh. Let's use these for reals because the original
531 ;; implementations based on the definitions lose big in round-off
532 ;; error. These bad definitions also mean that sin and cos for
533 ;; complex numbers can also lose big.
538 "Return the hyperbolic sine of NUMBER."
539 (/ (- (exp number) (exp (- number))) 2))
543 "Return the hyperbolic sine of NUMBER."
544 (number-dispatch ((number number))
545 (handle-reals %sinh number)
547 (let ((x (realpart number))
548 (y (imagpart number)))
549 (complex (* (sinh x) (cos y))
550 (* (cosh x) (sin y)))))))
555 "Return the hyperbolic cosine of NUMBER."
556 (/ (+ (exp number) (exp (- number))) 2))
560 "Return the hyperbolic cosine of NUMBER."
561 (number-dispatch ((number number))
562 (handle-reals %cosh number)
564 (let ((x (realpart number))
565 (y (imagpart number)))
566 (complex (* (cosh x) (cos y))
567 (* (sinh x) (sin y)))))))
571 "Return the hyperbolic tangent of NUMBER."
572 (number-dispatch ((number number))
573 (handle-reals %tanh number)
575 (complex-tanh number))))
577 (defun asinh (number)
579 "Return the hyperbolic arc sine of NUMBER."
580 (number-dispatch ((number number))
581 (handle-reals %asinh number)
583 (complex-asinh number))))
585 (defun acosh (number)
587 "Return the hyperbolic arc cosine of NUMBER."
588 (number-dispatch ((number number))
590 ;; acosh is complex if number < 1
592 (complex-acosh number)
593 (coerce (%acosh (coerce number 'double-float)) 'single-float)))
594 (((foreach single-float double-float))
595 (if (< number (coerce 1 '(dispatch-type number)))
596 (complex-acosh number)
597 (coerce (%acosh (coerce number 'double-float))
598 '(dispatch-type number))))
600 (complex-acosh number))))
602 (defun atanh (number)
604 "Return the hyperbolic arc tangent of NUMBER."
605 (number-dispatch ((number number))
607 ;; atanh is complex if |number| > 1
608 (if (or (> number 1) (< number -1))
609 (complex-atanh number)
610 (coerce (%atanh (coerce number 'double-float)) 'single-float)))
611 (((foreach single-float double-float))
612 (if (or (> number (coerce 1 '(dispatch-type number)))
613 (< number (coerce -1 '(dispatch-type number))))
614 (complex-atanh number)
615 (coerce (%atanh (coerce number 'double-float))
616 '(dispatch-type number))))
618 (complex-atanh number))))
620 ;;; HP-UX does not supply a C version of log1p, so
621 ;;; use the definition.
624 #!-sb-fluid (declaim (inline %log1p))
626 (defun %log1p (number)
627 (declare (double-float number)
628 (optimize (speed 3) (safety 0)))
629 (the double-float (log (the (double-float 0d0) (+ number 1d0)))))