qd-fun.lisp 45.7 KB
Newer Older
toy's avatar
toy committed
1
;;;; -*- Mode: lisp -*-
2
;;;;
3
;;;; Copyright (c) 2007, 2008, 2011 Raymond Toy
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
;;;;
;;;; Permission is hereby granted, free of charge, to any person
;;;; obtaining a copy of this software and associated documentation
;;;; files (the "Software"), to deal in the Software without
;;;; restriction, including without limitation the rights to use,
;;;; copy, modify, merge, publish, distribute, sublicense, and/or sell
;;;; copies of the Software, and to permit persons to whom the
;;;; Software is furnished to do so, subject to the following
;;;; conditions:
;;;;
;;;; The above copyright notice and this permission notice shall be
;;;; included in all copies or substantial portions of the Software.
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;;;; OTHER DEALINGS IN THE SOFTWARE.
toy's avatar
toy committed
25 26 27 28 29 30 31 32 33

;;; Basic special functions operating on %quad-double numbers.  This
;;; includes sqrt, rounding to the nearest integer, floor, exp, log,
;;; log1p, sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh,
;;; asinh, acosh, atanh, and random.
;;;
;;; These special functions only work on the main domains where the
;;; argument is real and the result is real.  Behavior is undefined if
;;; this doesn't hold.
toy's avatar
toy committed
34

35
(in-package #:octi)
toy's avatar
toy committed
36

37 38 39 40 41 42 43 44 45 46 47
(defun logb-finite (x)
  "Same as logb but X is not infinity and non-zero and not a NaN, so
that we can always return an integer"
  (declare (type cl:double-float x))
  (multiple-value-bind (signif expon sign)
      (cl:decode-float x)
    (declare (ignore signif sign))
    ;; decode-float is almost right, except that the exponent
    ;; is off by one
    (1- expon)))

48
#+cmu
Raymond Toy's avatar
Raymond Toy committed
49
(declaim (ext:maybe-inline sqrt-qd))
50
#+nil
51
(defun sqrt-qd (a)
toy's avatar
toy committed
52
  "Square root of the (non-negative) quad-float"
53 54 55 56 57 58 59
  (declare (type %quad-double a)
	   (optimize (speed 3) (space 0)))
  ;; Perform the following Newton iteration:
  ;;
  ;;  x' = x + (1 - a * x^2) * x / 2
  ;;
  ;; which converges to 1/sqrt(a).
toy's avatar
toy committed
60 61 62 63
  ;;
  ;; However, there appear to be round-off errors when x is either
  ;; very large or very small.  So let x = f*2^(2*k).  Then sqrt(x) =
  ;; 2^k*sqrt(f), and sqrt(f) doesn't have round-off problems.
toy's avatar
toy committed
64
  (when (zerop-qd a)
65
    (return-from sqrt-qd a))
toy's avatar
toy committed
66 67
  (when (float-infinity-p (qd-0 a))
    (return-from sqrt-qd a))
68

toy's avatar
toy committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
  (let* ((k (logandc2 (logb-finite (qd-0 a)) 1))
	 (new-a (scale-float-qd a (- k))))
    (assert (evenp k))
    (let* ((r (make-qd-d (cl:/ (sqrt (the (double-float (0d0))
				       (qd-0 new-a))))))
	   (half 0.5d0)
	   (h (mul-qd-d new-a half)))
      (declare (type %quad-double r))
      ;; Since we start with double-float precision, three more
      ;; iterations should give us full accuracy.
      (dotimes (k 3)
	(setf r (add-qd r (mul-qd r (sub-d-qd half (mul-qd h (sqr-qd r)))))))
      (scale-float-qd (mul-qd r new-a) (ash k -1)))))

83
(defun sqrt-qd (a)
84
  "Return the square root of the (non-negative) %QUAD-DOUBLE number A"
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  (declare (type %quad-double a)
	   (optimize (speed 3) (space 0)))
  ;; Perform the following Newton iteration:
  ;;
  ;;  x' = x + (1 - a * x^2) * x / 2
  ;;
  ;; which converges to 1/sqrt(a).
  ;;
  ;; However, there appear to be round-off errors when x is either
  ;; very large or very small.  So let x = f*2^(2*k).  Then sqrt(x) =
  ;; 2^k*sqrt(f), and sqrt(f) doesn't have round-off problems.
  (when (zerop-qd a)
    (return-from sqrt-qd a))
  (when (float-infinity-p (qd-0 a))
    (return-from sqrt-qd a))

  (let* ((k (logandc2 (logb-finite (qd-0 a)) 1))
	 (new-a (scale-float-qd a (- k))))
    (assert (evenp k))
    (let* ((r (make-qd-d (cl:/ (sqrt (the (double-float (0d0))
				       (qd-0 new-a))))))
	   (temp (%make-qd-d 0d0 0d0 0d0 0d0))
	   (half 0.5d0)
	   (h (mul-qd-d new-a half)))
      (declare (type %quad-double r))
      ;; Since we start with double-float precision, three more
      ;; iterations should give us full accuracy.
      (dotimes (k 3)
	#+nil
	(setf r (add-qd r (mul-qd r (sub-d-qd half (mul-qd h (sqr-qd r))))))
	(sqr-qd r temp)
	(mul-qd h temp temp)
	(sub-d-qd half temp temp)
	(mul-qd r temp temp)
	(add-qd r temp r)
	)
      (mul-qd r new-a r)
      (scale-float-qd r (ash k -1)))))

toy's avatar
toy committed
124 125 126 127 128 129 130 131 132
(defun hypot-aux-qd (x y)
  (declare (type %quad-double x y))
  (let ((k (- (logb-finite (max (cl:abs (qd-0 x))
				(cl:abs (qd-0 y)))))))
    (values (add-qd (sqr-qd (scale-float-qd x k))
		    (sqr-qd (scale-float-qd y k)))
	    (- k))))

(defun hypot-qd (x y)
133 134
  "sqrt(x^2+y^2) computed carefully without unnecessary overflow for
the %QUAD-DOUBLE numbers X and Y"
toy's avatar
toy committed
135 136 137
  (multiple-value-bind (abs^2 rho)
      (hypot-aux-qd x y)
    (scale-float-qd (sqrt-qd abs^2) rho)))
138

toy's avatar
toy committed
139
(defun nint-qd (a)
toy's avatar
toy committed
140 141
  "Round the quad-float to the nearest integer, which is returned as a
  quad-float"
toy's avatar
toy committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155
  (let ((x0 (fround (qd-0 a)))
	(x1 0d0)
	(x2 0d0)
	(x3 0d0))
    (cond ((= x0 (qd-0 a))
	   ;; First double is already an integer
	   (setf x1 (fround (qd-1 a)))
	   (cond ((= x1 (qd-1 a))
		  ;; Second is an integer
		  (setf x2 (fround (qd-2 a)))
		  (cond ((= x2 (qd-2 a))
			 ;; Third is an integer
			 (setf x3 (fround (qd-3 a))))
			(t
156
			 (when (and (zerop (abs (cl:- x2 (qd-2 a))))
toy's avatar
toy committed
157 158 159
				    (minusp (qd-3 a)))
			   (decf x2)))))
		 (t
160
		  (when (and (zerop (abs (cl:- x1 (qd-1 a))))
toy's avatar
toy committed
161 162 163
			     (minusp (qd-2 a)))
		    (decf x1)))))
	  (t
164
	   (when (and (zerop (abs (cl:- x0 (qd-0 a))))
toy's avatar
toy committed
165 166 167 168 169 170 171
		      (minusp (qd-1 a)))
	     (decf x0))))
    (multiple-value-bind (s0 s1 s2 s3)
	(renorm-4 x0 x1 x2 x3)
      (make-qd-d s0 s1 s2 s3))))

(defun ffloor-qd (a)
172
  "The floor of the %QUAD-DOUBLE A, returned as a %QUAD-DOUBLE number"
toy's avatar
toy committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  (let ((x0 (ffloor (qd-0 a)))
	(x1 0d0)
	(x2 0d0)
	(x3 0d0))
    (cond ((= x0 (qd-0 a))
	   (setf x1 (ffloor (qd-1 a)))
	   (when (= x1 (qd-1 a))
	     (setf x2 (ffloor (qd-2 a)))
	     (when (= x2 (qd-2 a))
	       (setf x3 (ffloor (qd-3 a)))))
	   (make-qd-d x0 x1 x2 x3))
	  (t
	   (%make-qd-d x0 x1 x2 x3)))))
	
			 
toy's avatar
toy committed
188
(defun exp-qd/reduce (a)
toy's avatar
toy committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  ;; Strategy:  Reduce the size of x by noting that
  ;;
  ;; exp(k*r+m) = exp(m) * exp(r)^k
  ;;
  ;; Thus, by choosing m to be a multiple of log(2) closest to x, we
  ;; can make |kr| < log(2)/2 = 0.3466.  Now we can set k = 256, so
  ;; that |r| <= 0.00136.
  ;;
  ;; Then
  ;;
  ;; exp(x) = exp(k*r+s*log(2)) = 2^s*(exp(r))^256
  ;;
  ;; We can use Taylor series to evaluate exp(r).

  (let* ((k 256)
	 (z (truncate (qd-0 (nint-qd (div-qd a +qd-log2+)))))
205 206 207 208 209
	 (r (div-qd-d (sub-qd a (mul-qd-d +qd-log2+ (float z 1d0)))
		      (float k 1d0)))
	 ;; For Taylor series.  p = r^2/2, the first term
	 (p (div-qd-d (sqr-qd r) 2d0))
	 ;; s = 1+r+p, the sum of the first 3 terms
toy's avatar
toy committed
210
	 (s (add-qd-d (add-qd r p) 1d0))
211
	 ;; Denominator of term
toy's avatar
toy committed
212
	 (m 2d0))
213 214 215 216 217 218
    ;; Taylor series until the term is small enough.
    ;;
    ;; Note that exp(x) = sinh(x) + sqrt(1+sinh(x)^2).  The Taylor
    ;; series for sinh has half as many terms as for exp, so it should
    ;; be less work to compute sinh.  Then a few additional operations
    ;; and a square root gives us exp.
toy's avatar
toy committed
219 220 221
    (loop
       (incf m)
       (setf p (mul-qd p r))
222
       (setf p (div-qd-d p m))
toy's avatar
toy committed
223
       (setf s (add-qd s p))
224
       (unless (> (abs (qd-0 p)) +qd-eps+)
toy's avatar
toy committed
225 226 227 228 229 230
	 (return)))

    (setf r (npow s k))
    (setf r (scale-float-qd r z))
    r))

toy's avatar
toy committed
231
(defun expm1-qd/duplication (a)
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  (declare (type %quad-double a))
  ;; Brent gives expm1(2*x) = expm1(x)*(2+expm1(x))
  ;;
  ;; Hence
  ;;
  ;; expm1(x) = expm1(x/2)*(2+expm1(x/2))
  ;;
  ;; Keep applying this formula until x is small enough.  Then use
  ;; Taylor series to compute expm1(x).
  (cond ((< (abs (qd-0 a)) .0001d0)
	 ;; What is the right threshold?
	 ;;
	 ;; Taylor series for exp(x)-1
	 ;; = x+x^2/2!+x^3/3!+x^4/4!+...
	 ;; = x*(1+x/2!+x^2/3!+x^3/4!+...)
247 248
	 (let ((sum +qd-one+)
	       (term +qd-one+))
249
	   (dotimes (k 28)
250
	     (setf term (div-qd-d (mul-qd term a) (float (cl:+ k 2) 1d0)))
251 252 253
	     (setf sum (add-qd sum term)))
	   (mul-qd a sum)))
	(t
toy's avatar
toy committed
254
	 (let ((d (expm1-qd/duplication (scale-float-qd a -1))))
255 256
	   (mul-qd d (add-qd-d d 2d0))))))

toy's avatar
toy committed
257
(defun expm1-qd (a)
258 259
  "Return exp(a) - 1 for the %QUAD-DOUBLE number A.  This is more
  accurate than just computing exp(a) - 1 directly."
toy's avatar
toy committed
260
  (declare (type %quad-double a))
261

toy's avatar
toy committed
262 263 264 265 266
  (when (float-infinity-p (qd-0 a))
    (return-from expm1-qd
      (if (minusp (float-sign (qd-0 a)))
	  +qd-zero+
	  a)))
toy's avatar
toy committed
267
  (expm1-qd/duplication a))
268

toy's avatar
toy committed
269
(defun exp-qd (a)
270
  "Return the expnential of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
271
  (declare (type %quad-double a))
toy's avatar
toy committed
272
  ;; Should we try to be more accurate than just 709?
273
  (when (< (qd-0 a) (log least-positive-normalized-double-float))
toy's avatar
toy committed
274
    (return-from exp-qd +qd-zero+))
275

276
  (when (> (qd-0 a) (log most-positive-double-float))
277
    #-cmu
278 279
    (error 'floating-point-overflow
	   :operation 'exp
280 281 282
	   :operands (list a))
    #+cmu
    (return-from exp-qd (%make-qd-d (/ 1d0 0d0) 0d0 0d0 0d0)))
toy's avatar
toy committed
283 284 285 286 287 288 289 290

  (when (zerop-qd a)
    (return-from exp-qd +qd-one+))

  ;; Default for now is exp-qd/reduce
  (exp-qd/reduce a))

(defun log-qd/halley (a)
291 292 293 294 295
  (declare (type %quad-double a))
  ;; Halley iteration:
  ;;
  ;; x' = x - 2*(exp(x)-a)/(exp(x)+a)
  ;;
296 297 298 299 300 301 302
  ;; But the above has problems if a is near
  ;; most-positive-double-float.  Rearrange the computation:
  ;;
  ;; x' = x - 2*(exp(x)/a-1)/(exp(x)/a+1)
  ;;
  ;; I think this works better, but it's also probably a little bit
  ;; more expensive because each iteration has two divisions.
toy's avatar
toy committed
303
  (let ((x (make-qd-d (log (qd-0 a)))))
304
    (flet ((iter (est)
305 306
	     (let ((exp (div-qd (exp-qd est)
				a)))
307 308
	       (sub-qd est
		       (scale-float-qd
309 310
			(div-qd (sub-qd-d exp 1d0)
				(add-qd-d exp 1d0))
311 312 313 314 315 316 317
			1)))))
      ;; Two iterations should be enough
      (setf x (iter x))
      (setf x (iter x))
      x)))
  

toy's avatar
toy committed
318
(defun log1p-qd/duplication (x)
319 320
  (declare (type %quad-double x)
	   (optimize (speed 3)))
toy's avatar
toy committed
321 322 323 324 325 326 327 328 329 330
  ;; Brent gives the following duplication formula for log1p(x) =
  ;; log(1+x):
  ;;
  ;; log1p(x) = 2*log1p(x/(1+sqrt(1+x)))
  ;;
  ;; So we apply the duplication formula until x is small enough, and
  ;; then use the series
  ;;
  ;; log(1+x) = 2*sum((x/(2+x))^(2*k+1)/(2*k+1),k,0,inf)
  ;;
331 332
  ;; Currently "small enough" means x < 0.005.  What is the right
  ;; cutoff?
toy's avatar
toy committed
333 334
  (cond ((> (abs (qd-0 x)) .005d0)
	 ;; log1p(x) = 2*log1p(x/(1+sqrt(1+x)))
toy's avatar
toy committed
335 336 337 338
	 (mul-qd-d (log1p-qd/duplication
		    (div-qd x
			    (add-d-qd 1d0
				      (sqrt-qd (add-d-qd 1d0 x)))))
toy's avatar
toy committed
339 340 341 342 343 344 345 346 347 348 349 350 351
		   2d0))
	(t
	 ;; Use the series
	 (let* ((term (div-qd x (add-qd-d x 2d0)))
		(mult (sqr-qd term))
		(sum term))
	   (loop for k of-type double-float from 3d0 by 2d0
	      while (> (abs (qd-0 term)) +qd-eps+)
	      do
		(setf term (mul-qd term mult))
		(setf sum (add-qd sum (div-qd-d term k))))
	   (mul-qd-d sum 2d0)))))

toy's avatar
toy committed
352
(defun log1p-qd (x)
353 354
  "Return log1p(x) = log(1+x), done more accurately than just evaluating
  log(1+x). X is a non-negative %QUAD-DOUBLE number"
355
  (declare (type %quad-double x))
356

toy's avatar
toy committed
357 358
  (when (float-infinity-p (qd-0 x))
    x)
359
  (log1p-qd/duplication x))
toy's avatar
toy committed
360 361

(defun log-qd (a)
362
  "Return the (natural) log of the non-negative %QUAD-DOUBLE number A"
toy's avatar
toy committed
363
  (declare (type %quad-double a))
toy's avatar
toy committed
364 365 366 367 368 369 370 371 372 373 374 375 376
  (cond ((onep-qd a)
	 +qd-zero+)
	((and (zerop-qd a)
	      (plusp (float-sign (qd-0 a))))
	 (%make-qd-d (/ -1d0 (qd-0 a)) 0d0 0d0 0d0))
	((float-infinity-p (qd-0 a))
	 a)
	((minusp (float-sign (qd-0 a)))
	 (error "log of negative"))
	(t
	 ;; Default is Halley's method
	 (log-qd/halley a))))
  
toy's avatar
toy committed
377

toy's avatar
toy committed
378 379 380 381 382
;; sin(a) and cos(a) using Taylor series
;;
;; Assumes |a| <= pi/2048
(defun sincos-taylor (a)
  (declare (type %quad-double a))
383
  (let ((thresh (cl:* +qd-eps+ (abs (qd-0 a)))))
toy's avatar
toy committed
384
    (when (zerop-qd a)
toy's avatar
toy committed
385
      (return-from sincos-taylor
386 387
	(values +qd-zero+
		+qd-one+)))
toy's avatar
toy committed
388 389 390 391 392
    (let* ((x (neg-qd (sqr-qd a)))
	   (s a)
	   (p a)
	   (m 1d0))
      (loop
393 394
	 (setf p (mul-qd p x))
	 (incf m 2)
toy's avatar
toy committed
395
	 (setf p (div-qd-d p (cl:* m (cl:1- m))))
toy's avatar
toy committed
396
	 (setf s (add-qd s p))
397
	 ;;(format t "p = ~A~%" (qd-0 p))
398
	 (when (<= (abs (qd-0 p)) thresh)
399
	   (return)))
toy's avatar
toy committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
      ;; cos(c) = sqrt(1-sin(c)^2).  This seems to work ok, even
      ;; though I would have expected some round-off errors in
      ;; computing this.  sqrt(1-x^2) is normally better computed as
      ;; sqrt(1-x)*sqrt(1+x) for small x.
      (values s (sqrt-qd (add-qd-d (neg-qd (sqr-qd s)) 1d0))))))

(defun drem-qd (a b)
  (declare (type %quad-double a b))
  (let ((n (nint-qd (div-qd a b))))
    (sub-qd a (mul-qd n b))))

(defun divrem-qd (a b)
  (declare (type %quad-double a b))
  (let ((n (nint-qd (div-qd a b))))
    (values n (sub-qd a (mul-qd n b)))))
415 416 417 418 419

;; Old, original routines.  These are correct, but they don't handle
;; large args because drem-qd isn't accurate enough.
#+(or)
(progn
toy's avatar
toy committed
420
(defun sin-qd (a)
toy's avatar
toy committed
421
  "Sin(a)"
toy's avatar
toy committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  (declare (type %quad-double a))
  ;; To compute sin(x), choose integers a, b so that
  ;;
  ;; x = s + a * (pi/2) + b*(pi/1024)
  ;;
  ;; with |x| <= pi/2048.  Using a precomputed table of sin(k*pi/1024)
  ;; and cos(k*pi/1024), we can compute sin(x) from sin(s) and cos(s).
  ;;
  ;; sin(x) = sin(s+k*(pi/1024) + j*pi/2)
  ;;        = sin(s+k*(pi/1024))*cos(j*pi/2)
  ;;            + cos(s+k*(pi/1024))*sin(j*pi/2)
  ;;
  ;; sin(s+k*pi/1024) = sin(s)*cos(k*pi/1024)
  ;;                     + cos(s)*sin(k*pi/1024)
  ;;
  ;; cos(s+k*pi/1024) = cos(s)*cos(k*pi/1024)
  ;;                     - sin(s)*sin(k*pi/1024)
toy's avatar
toy committed
439
  (when (zerop-qd a)
440
    (return-from sin-qd a))
toy's avatar
toy committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

  ;; Reduce modulo 2*pi
  (let ((r (drem-qd a +qd-2pi+)))
    ;; Now reduce by pi/2 and then by pi/1024 so that we obtain
    ;; numbers a, b, t
    (multiple-value-bind (j tmp)
	(divrem-qd r +qd-pi/2+)
      (let* ((j (truncate (qd-0 j)))
	     (abs-j (abs j)))
	(multiple-value-bind (k tmp)
	    (divrem-qd tmp +qd-pi/1024+)
	  (let* ((k (truncate (qd-0 k)))
		 (abs-k (abs k)))
	    (assert (<= abs-j 2))
	    (assert (<= abs-k 256))
	    ;; Compute sin(s) and cos(s)
	    (multiple-value-bind (sin-t cos-t)
		(sincos-taylor tmp)
	      (multiple-value-bind (s c)
		  (cond ((zerop abs-k)
			 (values sin-t cos-t))
			(t
			 ;; Compute sin(s+k*pi/1024), cos(s+k*pi/1024)
toy's avatar
toy committed
464 465
			 (let ((u (aref +qd-cos-table+ (cl:1- abs-k)))
			       (v (aref +qd-sin-table+ (cl:1- abs-k))))
toy's avatar
toy committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
			   (cond ((plusp k)
				  ;; sin(s) * cos(k*pi/1024)
				  ;; + cos(s) * sin(k*pi/1024)
				  ;;
				  ;; cos(s) * cos(k*pi/1024)
				  ;; - sin(s) * sin(k*pi/1024)
				  (values (add-qd (mul-qd u sin-t)
						  (mul-qd v cos-t))
					  (sub-qd (mul-qd u cos-t)
						  (mul-qd v sin-t))))
				 (t
				  ;; sin(s) * cos(k*pi/1024)
				  ;; - cos(s) * sin(|k|*pi/1024)
				  ;;
				  ;; cos(s) * cos(k*pi/1024)
				  ;; + sin(s) * sin(|k|*pi/1024)
				  (values (sub-qd (mul-qd u sin-t)
						  (mul-qd v cos-t))
					  (add-qd (mul-qd u cos-t)
						  (mul-qd v sin-t))))))))
toy's avatar
toy committed
486 487
		;;(format t "s = ~/qd::qd-format/~%" s)
		;;(format t "c = ~/qd::qd-format/~%" c)
toy's avatar
toy committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
		;; sin(x) =  sin(s+k*pi/1024) * cos(j*pi/2)
		;;         + cos(s+k*pi/1024) * sin(j*pi/2)
		(cond ((zerop abs-j)
		       ;; cos(j*pi/2) = 1, sin(j*pi/2) = 0
		       s)
		      ((= j 1)
		       ;; cos(j*pi/2) = 0, sin(j*pi/2) = 1
		       c)
		      ((= j -1)
		       ;; cos(j*pi/2) = 0, sin(j*pi/2) = -1
		       (neg-qd c))
		      (t
		       ;; cos(j*pi/2) = -1, sin(j*pi/2) = 0
		       (neg-qd s)))))))))))
		     
(defun cos-qd (a)
504
  "Return the cosine of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  ;; Just like sin-qd, but for cos.
  (declare (type %quad-double a))
  ;; To compute sin(x), choose integers a, b so that
  ;;
  ;; x = s + a * (pi/2) + b*(pi/1024)
  ;;
  ;; with |x| <= pi/2048.  Using a precomputed table of sin(k*pi/1024)
  ;; and cos(k*pi/1024), we can compute sin(x) from sin(s) and cos(s).
  ;;
  ;; sin(x) = sin(s+k*(pi/1024) + j*pi/2)
  ;;        = sin(s+k*(pi/1024))*cos(j*pi/2)
  ;;            + cos(s+k*(pi/1024))*sin(j*pi/2)
  ;;
  ;; sin(s+k*pi/1024) = sin(s)*cos(k*pi/1024)
  ;;                     + cos(s)*sin(k*pi/1024)
  ;;
  ;; cos(s+k*pi/1024) = cos(s)*cos(k*pi/1024)
  ;;                     - sin(s)*sin(k*pi/1024)
toy's avatar
toy committed
523
  (when (zerop-qd a)
524
    (return-from cos-qd +qd-one+))
toy's avatar
toy committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

  ;; Reduce modulo 2*pi
  (let ((r (drem-qd a +qd-2pi+)))
    ;; Now reduce by pi/2 and then by pi/1024 so that we obtain
    ;; numbers a, b, t
    (multiple-value-bind (j tmp)
	(divrem-qd r +qd-pi/2+)
      (let* ((j (truncate (qd-0 j)))
	     (abs-j (abs j)))
	(multiple-value-bind (k tmp)
	    (divrem-qd tmp +qd-pi/1024+)
	  (let* ((k (truncate (qd-0 k)))
		 (abs-k (abs k)))
	    (assert (<= abs-j 2))
	    (assert (<= abs-k 256))
	    ;; Compute sin(s) and cos(s)
	    (multiple-value-bind (sin-t cos-t)
		(sincos-taylor tmp)
	      (multiple-value-bind (s c)
		  (cond ((zerop abs-k)
			 (values sin-t cos-t))
			(t
			 ;; Compute sin(s+k*pi/1024), cos(s+k*pi/1024)
toy's avatar
toy committed
548 549
			 (let ((u (aref +qd-cos-table+ (cl:1- abs-k)))
			       (v (aref +qd-sin-table+ (cl:1- abs-k))))
toy's avatar
toy committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
			   (cond ((plusp k)
				  ;; sin(s) * cos(k*pi/1024)
				  ;; + cos(s) * sin(k*pi/1024)
				  ;;
				  ;; cos(s) * cos(k*pi/1024)
				  ;; - sin(s) * sin(k*pi/1024)
				  (values (add-qd (mul-qd u sin-t)
						  (mul-qd v cos-t))
					  (sub-qd (mul-qd u cos-t)
						  (mul-qd v sin-t))))
				 (t
				  ;; sin(s) * cos(k*pi/1024)
				  ;; - cos(s) * sin(|k|*pi/1024)
				  ;;
				  ;; cos(s) * cos(k*pi/1024)
				  ;; + sin(s) * sin(|k|*pi/1024)
				  (values (sub-qd (mul-qd u sin-t)
						  (mul-qd v cos-t))
					  (add-qd (mul-qd u cos-t)
						  (mul-qd v sin-t))))))))
toy's avatar
toy committed
570 571 572 573
		#+nil
		(progn
		  (format t "s = ~/qd::qd-format/~%" s)
		  (format t "c = ~/qd::qd-format/~%" c))
toy's avatar
toy committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
		;; sin(x) =  sin(s+k*pi/1024) * cos(j*pi/2)
		;;         + cos(s+k*pi/1024) * sin(j*pi/2)
		(cond ((zerop abs-j)
		       ;; cos(j*pi/2) = 1, sin(j*pi/2) = 0
		       c)
		      ((= j 1)
		       ;; cos(j*pi/2) = 0, sin(j*pi/2) = 1
		       (neg-qd s))
		      ((= j -1)
		       ;; cos(j*pi/2) = 0, sin(j*pi/2) = -1
		       s)
		      (t
		       ;; cos(j*pi/2) = -1, sin(j*pi/2) = 0
		       (neg-qd c)))))))))))

589 590
;; Compute sin and cos of a
(defun sincos-qd (a)
591 592
  "Return the sine of the %QUAD-DOUBLE number A.  The second returned value
is the cosine of A"
593
  (declare (type %quad-double a))
toy's avatar
toy committed
594
  (when (zerop-qd a)
595
    (return-from sincos-qd
596 597
      (values +qd-zero+
	      +qd-one+)))
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

  ;; Reduce modulo 2*pi
  (let ((r (drem-qd a +qd-2pi+)))
    ;; Now reduce by pi/2 and then by pi/1024 so that we obtain
    ;; numbers a, b, t
    (multiple-value-bind (j tmp)
	(divrem-qd r +qd-pi/2+)
      (let* ((j (truncate (qd-0 j)))
	     (abs-j (abs j)))
	(multiple-value-bind (k tmp)
	    (divrem-qd tmp +qd-pi/1024+)
	  (let* ((k (truncate (qd-0 k)))
		 (abs-k (abs k)))
	    (assert (<= abs-j 2))
	    (assert (<= abs-k 256))
	    ;; Compute sin(s) and cos(s)
	    (multiple-value-bind (sin-t cos-t)
		(sincos-taylor tmp)
	      (multiple-value-bind (s c)
		  (cond ((zerop abs-k)
			 (values sin-t cos-t))
			(t
			 ;; Compute sin(s+k*pi/1024), cos(s+k*pi/1024)
toy's avatar
toy committed
621 622
			 (let ((u (aref +qd-cos-table+ (cl:1- abs-k)))
			       (v (aref +qd-sin-table+ (cl:1- abs-k))))
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
			   (cond ((plusp k)
				  ;; sin(s) * cos(k*pi/1024)
				  ;; + cos(s) * sin(k*pi/1024)
				  ;;
				  ;; cos(s) * cos(k*pi/1024)
				  ;; - sin(s) * sin(k*pi/1024)
				  (values (add-qd (mul-qd u sin-t)
						  (mul-qd v cos-t))
					  (sub-qd (mul-qd u cos-t)
						  (mul-qd v sin-t))))
				 (t
				  ;; sin(s) * cos(k*pi/1024)
				  ;; - cos(s) * sin(|k|*pi/1024)
				  ;;
				  ;; cos(s) * cos(k*pi/1024)
				  ;; + sin(s) * sin(|k|*pi/1024)
				  (values (sub-qd (mul-qd u sin-t)
						  (mul-qd v cos-t))
					  (add-qd (mul-qd u cos-t)
						  (mul-qd v sin-t))))))))
toy's avatar
toy committed
643 644 645 646
		#+nil
		(progn
		  (format t "s = ~/qd::qd-format/~%" s)
		  (format t "c = ~/qd::qd-format/~%" c))
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		;; sin(x) =  sin(s+k*pi/1024) * cos(j*pi/2)
		;;         + cos(s+k*pi/1024) * sin(j*pi/2)
		(cond ((zerop abs-j)
		       ;; cos(j*pi/2) = 1, sin(j*pi/2) = 0
		       (values s c))
		      ((= j 1)
		       ;; cos(j*pi/2) = 0, sin(j*pi/2) = 1
		       (values c (neg-qd s)))
		      ((= j -1)
		       ;; cos(j*pi/2) = 0, sin(j*pi/2) = -1
		       (values (neg-qd c) s))
		      (t
		       ;; cos(j*pi/2) = -1, sin(j*pi/2) = 0
		       (values (neg-qd s)
			       (neg-qd c))))))))))))
662
)
663

664 665 666 667
;; A more accurate implementation of sin and cos.  We use a more
;; accurate argument reduction using 1584 bits of 2/pi.  This makes
;; sin and cos more expensive, of course.

Raymond Toy's avatar
Raymond Toy committed
668
#+(or)
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
(defun rem-pi/2-int (qd)
  ;; Compute qd rem pi/2 = k*pi/2+y.  So we compute k + y*2/pi =
  ;; qd*2/pi.
  ;;
  ;; First convert qd to 2^e*I.  We already have 2/pi in the form
  ;; 2^-1584*J.  Then qd*2/pi = 2^(e-1584)*I*J.  Extract out the
  ;; integer and fractional parts of this.  For the integer part we
  ;; only need it modulo 4, because of the periodicity.  For the
  ;; fractional part, we only need 212 (or so bits of fraction).
  ;;
  ;; FIXME: But we don't really need to compute all the bits of I*J.
  ;; In the product, we really only need the 2 bits to the left of the
  ;; binary point, and then 212 bits to the right.  This doesn't
  ;; require doing the full multiplication.
  (multiple-value-bind (i e s)
      (integer-decode-qd qd)
    (let* ((exp (- e 1584))
	   (prod (* (* s i) +2/pi-bits+))
	   (int (ash prod exp))
	   (mod (logand int 3))
	   (frac (ldb (byte 212 (- (- exp) 212)) prod))
	   (f (mul-qd (scale-float-qd (rational-to-qd frac) -212)
		      +qd-pi/2+)))
      ;; We want the remainder part to be <= pi/4 because the trig
      ;; functions want that.  So if the fraction is too big, adjust
      ;; it, and mod value.
      (if (<= (abs (qd-0 f)) (/ pi 4))
	  (values mod f)
	  (values (mod (1+ mod) 4) (sub-qd f +qd-pi/2+))))))

Raymond Toy's avatar
Raymond Toy committed
699 700 701 702 703
(defun split-prod-2/pi (qd)
  ;; Compute k and f such that k + f = qd*2/pi, where k is an intege
  ;; and f < 1. Since we do not need more than the the least
  ;; two bits from k, only the least two bits of k are returned.
  (multiple-value-bind (i e)
704
      (integer-decode-qd qd)
Raymond Toy's avatar
Raymond Toy committed
705 706 707 708 709 710 711 712 713 714
    ;; First convert qd to 2^e*I.  We already have 2/pi in the form
    ;; 2^-1584*J.  Then qd*2/pi = 2^(e-1584)*I*J.  Extract out the
    ;; integer and fractional parts of this.  For the integer part we
    ;; only need it modulo 4, because of the periodicity. For the
    ;; fractional bits, we return the actual fraction as a ratio.
    ;;
    ;; FIXME: We compute the entire product I*J, but we only need the
    ;; 2 bits to the left of the binary point.  But we need many of
    ;; the bits to the right of the binary point becaue we have no
    ;; lower bound on the difference between a quad-double and pi.
715
    (let* ((exp (- e (integer-length +2/pi-bits+)))
Raymond Toy's avatar
Raymond Toy committed
716 717 718 719 720 721
	   (binary-point (- exp))
	   (prod (* i +2/pi-bits+))
	   (int (ldb (byte 2 binary-point) prod))
	   (frac (/ (ldb (byte binary-point 0) prod)
		    (ash 1 binary-point))))
      (values int frac))))
722

Raymond Toy's avatar
Raymond Toy committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
(defun rem-pi/2-int (qd)
  "Compute qd rem pi/2 = k + f, where k is an integer and |f| <
  pi/4. Two values are returned: k mod 4 and f."
  (if (minusp-qd qd)
      (multiple-value-bind (k f)
	  (rem-pi/2-int (neg-qd qd))
	(values (mod (- k) 4) (neg-qd f)))
      (multiple-value-bind (k f)
	  (split-prod-2/pi qd)
	;; The trig functions want the arg to be less than pi/4.  That
	;; is f < 1/2.  Thus adjust k and f accordingly.
	(when (> f 1/2)
	  (setf k (mod (1+ k) 4))
	  (setf f (- f 1)))
	(values k (mul-qd +qd-pi/2+ (rational-to-qd f))))))
	    
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
(defun rem-pi/2-int-b (qd)
  (declare (type %quad-double qd))
  (multiple-value-bind (i e s)
      (integer-decode-qd qd)
    ;; This computes qd = k*pi/2+s.  And because of the 2*pi
    ;; periodicity of trig functions, we really only need mod(k, 4),
    ;; the least 2 bits of k.
    ;;
    ;; Write qd = 2^e*i.  Write the value 2/pi as sum f[k]*2^(-k), k =
    ;; 0, 1, 2,... and f[k] is 0 or 1.  Then the product, p, is
    ;;
    ;; p = sum i*f[k]*2^(e-k).
    ;;
    ;; Assume 2^m <= i < 2^(m+1).  Then it's obvious that
    ;; i*f[k]*2^(e-k) is greater than 2 if e-k+m > 1, or k < e+m-1.
    ;; Thus we can ignore the bits of 2/pi for k < e+m-1, because the
    ;; contribution is greater than 2.  For k >= e+m-1, we will have
    ;; the product that is between 0 and 4, which is the part we want.
    ;; We just need to select how many fraction bits we want.
    ;; 4*53=212 seems a little low, and 5*53=265 should be more than
    ;; enough.  Let's choose 220.
    ;;
    ;; This code would be faster if we knew how many bits are in i.
    ;; You might think it should be 212 (4*53), but no.  Consider #q1
    ;; + #q1q-100.  This has way more than 212 bits.
    (let* ((qd-bits 220)
	   (m (integer-length i))
	   (frac-bits (+ qd-bits (- m 2)))
	   (bits (ldb (byte qd-bits
			    (- (integer-length +2/pi-bits+) e frac-bits))
		      +2/pi-bits+))
	   ;; Multiply these together.
	   (prod (* s i bits))
	   ;; Extract out the integer and fractional parts
	   (frac (ldb (byte frac-bits 0) prod))
	   (mod (ldb (byte 2 frac-bits) prod))
	   ;;(mod (ash prod (- frac-bits)))
	   (f (mul-qd (scale-float-qd (rational-to-qd frac) (- frac-bits))
		      +qd-pi/2+)))
      ;; If we did this right, (ash prod (- frac-bits)) should be 2
      ;; bits long at most.
780
      (if (qd-<= (abs-qd f) +qd-pi/4+)
781 782 783 784
	  (values mod f)
	  (values (mod (1+ mod) 4)
		  (sub-qd f +qd-pi/2+))))))

785
(defun rem-pi/2 (a)
Raymond Toy's avatar
Raymond Toy committed
786 787 788 789
  "Compute qd rem pi/2 = k + f, where k is an integer and |f| <
  pi/4. Two values are returned: k mod 4 and f."
  (cond ((qd-<= (abs-qd a) +qd-pi/4+)
	 (values 0 a))
790 791 792 793 794
	(t
	 (rem-pi/2-int a))))
      

(defun sin-qd (a)
795
  "Return the sine of the %QUAD-DOUBLE number A"
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
  (declare (type %quad-double a))
  ;; To compute sin(x), choose integers a, b so that
  ;;
  ;; x = s + a * (pi/2) + b*(pi/1024)
  ;;
  ;; with |x| <= pi/2048.  Using a precomputed table of sin(k*pi/1024)
  ;; and cos(k*pi/1024), we can compute sin(x) from sin(s) and cos(s).
  ;;
  ;; sin(x) = sin(s+k*(pi/1024) + j*pi/2)
  ;;        = sin(s+k*(pi/1024))*cos(j*pi/2)
  ;;            + cos(s+k*(pi/1024))*sin(j*pi/2)
  ;;
  ;; sin(s+k*pi/1024) = sin(s)*cos(k*pi/1024)
  ;;                     + cos(s)*sin(k*pi/1024)
  ;;
  ;; cos(s+k*pi/1024) = cos(s)*cos(k*pi/1024)
  ;;                     - sin(s)*sin(k*pi/1024)
  (when (zerop-qd a)
814
    (return-from sin-qd a))
815 816

  (multiple-value-bind (j r)
817
      (rem-pi/2 a)
818 819 820 821 822 823 824 825 826 827 828 829
    (multiple-value-bind (k tmp)
	(divrem-qd r +qd-pi/1024+)
      (let* ((k (truncate (qd-0 k)))
	     (abs-k (abs k)))
	(assert (<= 0 j 3))
	(assert (<= abs-k 256))
	;; Compute sin(s) and cos(s)
	(multiple-value-bind (sin-t cos-t)
	    (sincos-taylor tmp)
	  (multiple-value-bind (s c)
	      (cond ((zerop abs-k)
		     (values sin-t cos-t))
830
		    (t
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
		     ;; Compute sin(s+k*pi/1024), cos(s+k*pi/1024)
		     (let ((u (aref +qd-cos-table+ (cl:1- abs-k)))
			   (v (aref +qd-sin-table+ (cl:1- abs-k))))
		       (cond ((plusp k)
			      ;; sin(s) * cos(k*pi/1024)
			      ;; + cos(s) * sin(k*pi/1024)
			      ;;
			      ;; cos(s) * cos(k*pi/1024)
			      ;; - sin(s) * sin(k*pi/1024)
			      (values (add-qd (mul-qd u sin-t)
					      (mul-qd v cos-t))
				      (sub-qd (mul-qd u cos-t)
					      (mul-qd v sin-t))))
			     (t
			      ;; sin(s) * cos(k*pi/1024)
			      ;; - cos(s) * sin(|k|*pi/1024)
			      ;;
			      ;; cos(s) * cos(k*pi/1024)
			      ;; + sin(s) * sin(|k|*pi/1024)
			      (values (sub-qd (mul-qd u sin-t)
					      (mul-qd v cos-t))
				      (add-qd (mul-qd u cos-t)
					      (mul-qd v sin-t))))))))
	    ;;(format t "s = ~/qd::qd-format/~%" s)
	    ;;(format t "c = ~/qd::qd-format/~%" c)
	    ;; sin(x) =  sin(s+k*pi/1024) * cos(j*pi/2)
	    ;;         + cos(s+k*pi/1024) * sin(j*pi/2)
	    (cond ((zerop j)
		   ;; cos(j*pi/2) = 1, sin(j*pi/2) = 0
		   s)
		  ((= j 1)
		   ;; cos(j*pi/2) = 0, sin(j*pi/2) = 1
		   c)
		  ((= j 2)
		   ;; cos(j*pi/2) = -1, sin(j*pi/2) = 0
		   (neg-qd s))
		  ((= j 3)
		   ;; cos(j*pi/2) = 0, sin(j*pi/2) = -1
		   (neg-qd c)))))))))
870

871
(defun cos-qd (a)
872
  "Return the cosine of the %QUAD-DOUBLE number A"
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
  ;; Just like sin-qd, but for cos.
  (declare (type %quad-double a))
  ;; To compute sin(x), choose integers a, b so that
  ;;
  ;; x = s + a * (pi/2) + b*(pi/1024)
  ;;
  ;; with |x| <= pi/2048.  Using a precomputed table of sin(k*pi/1024)
  ;; and cos(k*pi/1024), we can compute sin(x) from sin(s) and cos(s).
  ;;
  ;; sin(x) = sin(s+k*(pi/1024) + j*pi/2)
  ;;        = sin(s+k*(pi/1024))*cos(j*pi/2)
  ;;            + cos(s+k*(pi/1024))*sin(j*pi/2)
  ;;
  ;; sin(s+k*pi/1024) = sin(s)*cos(k*pi/1024)
  ;;                     + cos(s)*sin(k*pi/1024)
  ;;
  ;; cos(s+k*pi/1024) = cos(s)*cos(k*pi/1024)
  ;;                     - sin(s)*sin(k*pi/1024)
  (when (zerop-qd a)
892
    (return-from cos-qd +qd-one+))
893 894

  (multiple-value-bind (j r)
895
      (rem-pi/2 a)
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
    (multiple-value-bind (k tmp)
	(divrem-qd r +qd-pi/1024+)
      (let* ((k (truncate (qd-0 k)))
	     (abs-k (abs k)))
	(assert (<= 0 j 3))
	(assert (<= abs-k 256))
	;; Compute sin(s) and cos(s)
	(multiple-value-bind (sin-t cos-t)
	    (sincos-taylor tmp)
	  (multiple-value-bind (s c)
	      (cond ((zerop abs-k)
		     (values sin-t cos-t))
		    (t
		     ;; Compute sin(s+k*pi/1024), cos(s+k*pi/1024)
		     (let ((u (aref +qd-cos-table+ (cl:1- abs-k)))
			   (v (aref +qd-sin-table+ (cl:1- abs-k))))
		       (cond ((plusp k)
			      ;; sin(s) * cos(k*pi/1024)
			      ;; + cos(s) * sin(k*pi/1024)
			      ;;
			      ;; cos(s) * cos(k*pi/1024)
			      ;; - sin(s) * sin(k*pi/1024)
			      (values (add-qd (mul-qd u sin-t)
					      (mul-qd v cos-t))
				      (sub-qd (mul-qd u cos-t)
					      (mul-qd v sin-t))))
			     (t
			      ;; sin(s) * cos(k*pi/1024)
			      ;; - cos(s) * sin(|k|*pi/1024)
			      ;;
			      ;; cos(s) * cos(k*pi/1024)
			      ;; + sin(s) * sin(|k|*pi/1024)
			      (values (sub-qd (mul-qd u sin-t)
					      (mul-qd v cos-t))
				      (add-qd (mul-qd u cos-t)
					      (mul-qd v sin-t))))))))
	    #+nil
	    (progn
	      (format t "s = ~/qd::qd-format/~%" s)
	      (format t "c = ~/qd::qd-format/~%" c))
	    ;; sin(x) =  sin(s+k*pi/1024) * cos(j*pi/2)
	    ;;         + cos(s+k*pi/1024) * sin(j*pi/2)
	    (cond ((zerop j)
		   ;; cos(j*pi/2) = 1, sin(j*pi/2) = 0
		   c)
		  ((= j 1)
		   ;; cos(j*pi/2) = 0, sin(j*pi/2) = 1
		   (neg-qd s))
		  ((= j 2)
		   ;; cos(j*pi/2) = -1, sin(j*pi/2) = 0
		   (neg-qd c))
		  ((= j 3)
		   ;; cos(j*pi/2) = 0, sin(j*pi/2) = -1
		   s))))))))

951
(defun sincos-qd (a)
952 953
  (declare (type %quad-double a))
  (when (zerop-qd a)
954
    (return-from sincos-qd
955 956 957 958
      (values +qd-zero+
	      +qd-one+)))

  (multiple-value-bind (j r)
959
      (rem-pi/2 a)
960
    (multiple-value-bind (k tmp)
961
	(divrem-qd r +qd-pi/1024+)
962 963
      (let* ((k (truncate (qd-0 k)))
	     (abs-k (abs k)))
964
	(assert (<= 0 j 3))
965 966 967 968 969 970 971
	(assert (<= abs-k 256))
	;; Compute sin(s) and cos(s)
	(multiple-value-bind (sin-t cos-t)
	    (sincos-taylor tmp)
	  (multiple-value-bind (s c)
	      (cond ((zerop abs-k)
		     (values sin-t cos-t))
972
		    (t
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
		     ;; Compute sin(s+k*pi/1024), cos(s+k*pi/1024)
		     (let ((u (aref +qd-cos-table+ (cl:1- abs-k)))
			   (v (aref +qd-sin-table+ (cl:1- abs-k))))
		       (cond ((plusp k)
			      ;; sin(s) * cos(k*pi/1024)
			      ;; + cos(s) * sin(k*pi/1024)
			      ;;
			      ;; cos(s) * cos(k*pi/1024)
			      ;; - sin(s) * sin(k*pi/1024)
			      (values (add-qd (mul-qd u sin-t)
					      (mul-qd v cos-t))
				      (sub-qd (mul-qd u cos-t)
					      (mul-qd v sin-t))))
			     (t
			      ;; sin(s) * cos(k*pi/1024)
			      ;; - cos(s) * sin(|k|*pi/1024)
			      ;;
			      ;; cos(s) * cos(k*pi/1024)
			      ;; + sin(s) * sin(|k|*pi/1024)
			      (values (sub-qd (mul-qd u sin-t)
					      (mul-qd v cos-t))
				      (add-qd (mul-qd u cos-t)
					      (mul-qd v sin-t))))))))
	    #+nil
	    (progn
	      (format t "s = ~/qd::qd-format/~%" s)
	      (format t "c = ~/qd::qd-format/~%" c))
	    ;; sin(x) =  sin(s+k*pi/1024) * cos(j*pi/2)
	    ;;         + cos(s+k*pi/1024) * sin(j*pi/2)
1002
	    (cond ((zerop j)
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
		   ;; cos(j*pi/2) = 1, sin(j*pi/2) = 0
		   (values s c))
		  ((= j 1)
		   ;; cos(j*pi/2) = 0, sin(j*pi/2) = 1
		   (values c (neg-qd s)))
		  ((= j 2)
		   ;; cos(j*pi/2) = -1, sin(j*pi/2) = 0
		   (values (neg-qd s)
			   (neg-qd c)))
		  ((= j 3)
		   ;; cos(j*pi/2) = 0, sin(j*pi/2) = -1
1014
		   (values (neg-qd c) s)))))))))
1015

toy's avatar
toy committed
1016
(defun atan2-qd/newton (y x)
1017 1018 1019
  (declare (type %quad-double y x)
	   #+nil
	   (optimize (speed 3) (space 0)))
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  ;; Instead of using Taylor series to compute atan, we instead use
  ;; Newton's iteration to solve the equation
  ;;
  ;;   sin(z) = y/r or cos(z) = x/r
  ;;
  ;; where r = sqrt(x^2+y^2)
  ;;
  ;; The iteration is
  ;;
  ;;   z' = z + (y - sin(z))/cos(z)       (for sin)
  ;;   z' = z + (x - cos(z))/sin(z)       (for cos)
  ;;
  ;; Here, x and y are normalized so that x^2 + y^2 = 1.
  ;;
  ;; If |x| > |y|, then the first iteration is used since the
  ;; denominator is larger.  Otherwise the second is used.
toy's avatar
toy committed
1036
  (cond ((zerop-qd x)
1037
	 ;; x = 0
toy's avatar
toy committed
1038
	 (cond ((zerop-qd y)
1039 1040
		;; Both x and y are zero.  Use the signs of x and y to
		;; determine the result
1041 1042
		(error "atan2(0,0)"))
	       (t
1043
		;; x = 0, but y is not.  Use the sign of y.
toy's avatar
toy committed
1044
		(return-from atan2-qd/newton
1045
		  (cond ((plusp (float-sign (qd-0 y)))
1046 1047 1048
			 +qd-pi/2+)
			(t
			 (neg-qd +qd-pi/2+)))))))
toy's avatar
toy committed
1049
	((zerop-qd y)
1050
	 ;; y = 0.
toy's avatar
toy committed
1051
	 (return-from atan2-qd/newton
1052 1053
	   ;; Use the sign of x and y to figure out the result.
	   (cond ((plusp (float-sign (qd-0 x)))
toy's avatar
toy committed
1054
		  +qd-zero+)
1055 1056
		 ((plusp (float-sign (qd-0 y)))
		  +qd-pi+)
1057
		 (t
1058
		  (neg-qd +qd-pi+))))))
1059

toy's avatar
toy committed
1060
  (when (qd-= x y)
toy's avatar
toy committed
1061
    (return-from atan2-qd/newton
toy's avatar
toy committed
1062
      (if (plusp-qd y)
1063 1064 1065
	  +qd-pi/4+
	  +qd-3pi/4+)))

toy's avatar
toy committed
1066
  (when (qd-= x (neg-qd y))
toy's avatar
toy committed
1067
    (return-from atan2-qd/newton
toy's avatar
toy committed
1068
      (if (plusp-qd y)
1069 1070 1071
	  +qd-3pi/4+
	  (neg-qd +qd-pi/4+))))

toy's avatar
toy committed
1072
  (let* ((r (hypot-qd x y))
1073 1074
	 (xx (div-qd x r))
	 (yy (div-qd y r)))
toy's avatar
toy committed
1075
    #+nil
toy's avatar
toy committed
1076
    (progn
1077 1078 1079
      (format t "r  = ~/octi::qd-format/~%" r)
      (format t "xx = ~/octi::qd-format/~%" xx)
      (format t "yy = ~/octi::qd-format/~%" yy))
1080 1081
    
    ;; Compute double-precision approximation to atan
toy's avatar
toy committed
1082
    (let ((z (make-qd-d (atan (qd-0 y) (qd-0 x))))
toy's avatar
toy committed
1083 1084
	  (sinz +qd-zero+)
	  (cosz +qd-zero+))
1085 1086
      (cond ((> (abs (qd-0 xx))
		(abs (qd-0 yy)))
1087 1088 1089 1090 1091 1092 1093 1094 1095
	     ;; Newton iteration  z' = z + (y - sin(z))/cos(z)
	     (dotimes (k 3)
	       (multiple-value-setq (sinz cosz) (sincos-qd z))
	       (setf z (add-qd z (div-qd (sub-qd yy sinz)
					 cosz)))))
	    (t
	     ;; Newton iteration z' = z - (x - cos(z))/sin(z)
	     (dotimes (k 3)
	       (multiple-value-setq (sinz cosz) (sincos-qd z))
1096
	       (setf z (sub-qd z (div-qd (sub-qd xx cosz)
1097 1098 1099
					 sinz))))))
      z)))

toy's avatar
toy committed
1100
(defun atan-qd/newton (y)
1101 1102
  (declare (type %quad-double y)
	   #+nil (optimize (speed 3) (space 0)))
toy's avatar
toy committed
1103
  (atan2-qd/newton y +qd-one+))
1104

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
;; Computation of atan using a table and taylor series.

(defun find-atan-partition-linear (x)
  ;; Simple linear search. Search through the qd-atan-partition table
  ;; to find the entry k such that x > X[k].  Return k. 
  (loop for k from 0 below octi::+qd-atan-partition-size+
	;; do (format t "k = ~D, atan = ~S~%" k (aref octi::+qd-atan-partition+ k))
	when (qd-< x (aref octi::+qd-atan-partition+ k))
	  return k))

(defun find-atan-partition (x)
  ;; Perform binary search through the atan partition table to find
  ;; the smallest entry greater than x.  Return the index of the
  ;; entry.
  (let* ((low 0)
1120
	 (high (length octi::+qd-atan-partition+))
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	 (mid (ash (+ low high) -1)))
    (loop
      (if (qd-< x (aref octi::+qd-atan-partition+ mid))
	  (setf high mid)
	  (setf low mid))
      (setf mid (ash (+ low high) -1))
      ;;(format t "low, mid, high = ~A ~A ~A~%" low mid high)
      (when (<= (- high low) 1)
	(return)))
    ;;(format t "mid = ~A~%" mid)
    ;;(format t "X[mid] = ~A~%" (aref octi::+qd-atan-partition+ mid))
    (if (qd-< x (aref octi::+qd-atan-partition+ mid))
	mid
	(1+ mid))))
    

(defun atan-taylor (a)
  ;; Taylor series for atan(x).
  ;;
  ;; atan(x) = sum((-1)^k*x^(2*k+1)/(2*k+1), k, 0, inf)
  ;;         = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - ...
  ;;         = x*(1 - x^2/3 + x^4/5 - x^6/7 + x^8/9 - ...)
  ;;
  ;; x should be small for this to be effective, but no check is made
  ;; for this.  The series on converges for |x| < 1.
  (let* ((x2 (neg-qd (sqr-qd a)))
	 (term x2)
	 (s (make-qd-d 1d0))
	 (m 3))
    (loop
      (setf s (add-qd s (div-qd-d term (float m 1d0))))
      (setf term (mul-qd term x2))
      (setf m (+ m 2))
      (when (<= (abs (qd-0 term)) +qd-eps+)
	;;(format t "terms = ~A~%" m)
	(return)))
    (mul-qd s a)))

(defun atan-qd/taylor (y)
  ;; To compute atan(x) for x in [0,inf], we partition the interval
  ;; [0,inf] such that partition points X[i] and the evaluation nodes
  ;; x[i] are defined by
  ;;
  ;;   x[i] = tan((2*i-2)*pi/(4*s)), i = 2, ..., s + 1
  ;;   X[i] = tan((2*i-1)*pi/(4*s)), i = 1, ..., s
  ;;
  ;; where 0 = X[0] < X[1] < x[2] < X[2] < ... < x[s] < X[s] < x[s+1]
  ;; = X[s+1] = inf.
  ;;
  ;; So if x is in the interval [X[i-1], X[i]}, the computation of
  ;; atan(x) is given by
  ;;
  ;;  atan(x) = atan(x[i]) + atan(e)
  ;;
  ;; where e = 1/x[i] - (1+1/x[i]^2)/(x+1/x[i])
  ;;
  ;; But since x[i] = tan((2*i-1)*pi/(4*s)), atan(x[i]) =
  ;; (2*i-1)*pi/(4*s), so
  ;;
  ;;  atan(x) = (2*i-1)*pi/(4*s) + atan(e)
  ;;
  ;; Note that qd-atan-partition array starts with X[2].
1183 1184 1185 1186 1187 1188 1189 1190
  (cond
    ((minusp-qd y)
     ;; atan(-x) = -atan(x)
     (neg-qd (atan-qd/taylor (neg-qd y))))
    ((qd-> y +qd-one+)
     ;; atan(x) = pi/2-atan(1/x), for x > 1
     (sub-qd +qd-pi/2+
	     (atan-qd/taylor (div-qd +qd-one+ y))))
1191 1192
    ((qd-<= y (aref octi::+qd-atan-partition+ 0))
     (atan-taylor y))
1193
    (t
1194
     ;; The general case where the table is needed.
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
     (let* ((i (find-atan-partition y))
	    (atan-xi (mul-qd +qd-pi/2+
			     (rational-to-qd (/ (1- (+ i 2))
						octi::+qd-atan-partition-size+))))
	    (1/xi (div-qd +qd-one+ (aref octi::+qd-atan-nodes+ i)))
	    (e1 (div-qd (add-qd-d (sqr-qd 1/xi) 1d0)
			(add-qd 1/xi y)))
	    (e (sub-qd 1/xi
		       e1)))
       ;;(format t "partition ~D~%" i)
       ;;(format t "atan(xi) = ~S~%" atan-xi)
       ;;(format t "e = ~S~%" e)
       (add-qd atan-xi (atan-taylor e))))))

(defun atan2-qd/taylor (y x)
  (declare (type %quad-double y x)
	   #+nil
	   (optimize (speed 3) (space 0)))
  (cond ((zerop-qd x)
	 ;; x = 0
	 (cond ((zerop-qd y)
		;; Both x and y are zero.  Use the signs of x and y to
		;; determine the result
		(error "atan2(0,0)"))
	       (t
		;; x = 0, but y is not.  Use the sign of y.
		(return-from atan2-qd/taylor
		  (cond ((plusp (float-sign (qd-0 y)))
			 +qd-pi/2+)
			(t
			 (neg-qd +qd-pi/2+)))))))
	((zerop-qd y)
	 ;; y = 0.
	 (return-from atan2-qd/taylor
	   ;; Use the sign of x and y to figure out the result.
	   (cond ((plusp (float-sign (qd-0 x)))
		  +qd-zero+)
		 ((plusp (float-sign (qd-0 y)))
		  +qd-pi+)
		 (t
		  (neg-qd +qd-pi+))))))

  (when (qd-= x y)
    (return-from atan2-qd/taylor
      (if (plusp-qd y)
	  +qd-pi/4+
	  +qd-3pi/4+)))

  (when (qd-= x (neg-qd y))
    (return-from atan2-qd/taylor
      (if (plusp-qd y)
	  +qd-3pi/4+
	  (neg-qd +qd-pi/4+))))

  (let ((arg (atan-qd/taylor (div-qd y (abs-qd x)))))
1250 1251
    ;; arg = atan2(y,|x|), so |arg| <= pi/2.  Handle the case when x
    ;; is negative.
1252 1253 1254 1255 1256 1257
    (cond ((minusp-qd x)
	   (if (minusp-qd y)
	       (- (neg-qd +qd-pi+) arg)
	       (- +qd-pi+ arg)))
	  (t
	   arg))))
1258

toy's avatar
toy committed
1259
(defun atan2-qd (y x)
toy's avatar
toy committed
1260
  "atan2(y, x) = atan(y/x), but carefully handling the quadrant"
toy's avatar
toy committed
1261
  (declare (type %quad-double y x))
1262
  (atan2-qd/taylor y x))
toy's avatar
toy committed
1263 1264

(defun atan-qd (y)
1265
  "Return the arc tangent of the %QUAD-DOUBLE number Y"
toy's avatar
toy committed
1266
  (declare (type %quad-double y))
1267
  (atan-qd/taylor y))
toy's avatar
toy committed
1268

1269
(defun asin-qd (a)
1270
  "Return the arc sine of the %QUAD-DOUBLE number A"
1271
  (declare (type %quad-double a))
1272
  (atan2-qd a (sqrt-qd (sub-d-qd 1d0
1273 1274 1275
			       (sqr-qd a)))))

(defun acos-qd (a)
1276
  "Return the arc cosine of the %QUAD-DOUBLE number A"
1277
  (declare (type %quad-double a))
1278
  (atan2-qd (sqrt-qd (sub-d-qd 1d0
1279 1280 1281
			     (sqr-qd a)))
	    a))
  
toy's avatar
toy committed
1282

toy's avatar
toy committed
1283 1284 1285 1286
(defun tan-qd/sincos (r)
  (declare (type %quad-double r))
  (multiple-value-bind (s c)
      (sincos-qd r)
1287 1288 1289
    ;; What to do, what do?  If C is zero, we get divide by zero
    ;; error.  We could return infinity, but quad-double stuff doesn't
    ;; handle infinities very well.
toy's avatar
toy committed
1290 1291 1292
    (div-qd s c)))

(defun tan-qd (r)
1293
  "Return the tangent of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1294
  (declare (type %quad-double r))
Raymond Toy's avatar
Raymond Toy committed
1295
  (if (zerop-qd r)
1296 1297
      r
      (tan-qd/sincos r)))
toy's avatar
toy committed
1298
  
toy's avatar
toy committed
1299 1300

(defun sinh-qd (a)
1301
  "Return the hyperbolic sine of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1302 1303
  (declare (type %quad-double a))
  ;; Hart et al. suggests sinh(x) = 1/2*(D(x) + D(x)/(D(x)+1))
1304
  ;; where D(x) = exp(x) - 1.  This helps for x near 0.
1305
  (cond ((zerop (qd-0 a))
toy's avatar
toy committed
1306 1307 1308 1309 1310
	 a)
	((float-infinity-p (qd-0 a))
	 a)
	(t
	 (let ((d (expm1-qd a)))
1311 1312
	   (when (float-infinity-p (qd-0 d))
	     (return-from sinh-qd d))
toy's avatar
toy committed
1313 1314 1315
	   (scale-float-qd (add-qd d
				   (div-qd d (add-qd-d d 1d0)))
			   -1)))))
toy's avatar
toy committed
1316 1317

(defun cosh-qd (a)
1318
  "Return the hyperbolic cosine of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1319 1320 1321
  (declare (type %quad-double a))
  ;; cosh(x) = 1/2*(exp(x)+exp(-x))
  (let ((e (exp-qd a)))
1322 1323
    (when (float-infinity-p (qd-0 e))
      (return-from cosh-qd e))
1324
    (scale-float-qd (add-qd e (div-qd +qd-one+ e))
toy's avatar
toy committed
1325 1326 1327
		    -1)))

(defun tanh-qd (a)
1328
  "Return the hyperbolic tangent of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1329 1330
  (declare (type %quad-double a))
  ;; Hart et al. suggests tanh(x) = D(2*x)/(2+D(2*x))
1331
  (cond ((zerop (qd-0 a))
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	 a)
	((> (abs (qd-0 a)) (/ (+ (log most-positive-double-float)
				 (log 2d0))
			      4d0))
	 ;; For this range of A, we know the answer is +/- 1.
	 ;;
	 ;; However, we could do better if we wanted.  Assume x > 0
	 ;; and very large.
	 ;;
	 ;; tanh(x) = sinh(x)/cosh(x)
	 ;;         = (1-exp(-2*x))/(1+exp(-2*x))
	 ;;         = 1 - 2*exp(-2*x)/(1+exp(-2*x))
	 ;;
	 ;; So tanh(x) is 1 if the other term is small enough, say,
	 ;; eps.  So for x large enough we can compute tanh(x) very
	 ;; accurately, thanks to how quad-double addition works.
	 ;; (The first component is, basically 1d0, and the second is
	 ;; some very small double-float.)
	 #+nil
	 (let* ((e (exp (* -2 a)))
		(res (- 1 (/ (* 2 e) (1+ e)))))
	   (if (minusp (float-sign (qd-0 a)))
	       (neg-qd res)
	       res))
	 (make-qd-d (float-sign (qd-0 a))))
	(t
	 (let* ((a2 (mul-qd-d a 2d0))
		(d (expm1-qd a2)))
	   (div-qd d (add-qd-d d 2d0))))))
toy's avatar
toy committed
1361

Raymond Toy's avatar
Raymond Toy committed
1362
#+(or)
toy's avatar
toy committed
1363
(defun asinh-qd (a)
toy's avatar
toy committed
1364
  "Asinh(a)"
toy's avatar
toy committed
1365 1366
  (declare (type %quad-double a))
  ;; asinh(x) = log(x + sqrt(1+x^2))
1367 1368 1369 1370 1371 1372 1373
  ;;
  ;; But this doesn't work well when x is small.
  ;;
  ;; log(x + sqrt(1+x^2)) = log(sqrt(1+x^2)*(1+x/sqrt(1+x^2)))
  ;;   = log(sqrt(1+x^2)) + log(1+x/sqrt(1+x^2))
  ;;   = 1/2*log(1+x^2) + log(1+x/sqrt(1+x^2))
  ;;
1374 1375 1376 1377 1378 1379
  ;; However that doesn't work well when x is large because x^2
  ;; overflows.
  ;;
  ;; log(x + sqrt(1+x^2)) = log(x + x*sqrt(1+1/x^2))
  ;;   = log(x) + log(1+sqrt(1+1/x^2))
  ;;   = log(x) + log1p(sqrt(1+1/x^2))
1380
  #+nil
toy's avatar
toy committed
1381 1382
  (log-qd (add-qd a
		  (sqrt-qd (add-qd-d (sqr-qd a)
1383
				     1d0))))
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
  (if (< (abs (qd-0 a)) (sqrt most-positive-double-float))
      (let ((a^2 (sqr-qd a)))
	(add-qd (scale-float-qd (log1p-qd a^2) -1)
		(log1p-qd (div-qd a
				  (sqrt-qd (add-qd-d a^2 1d0))))))
      (if (minusp-qd a)
	  (neg-qd (asinh-qd (neg-qd a)))
	  (let ((1/a (div-qd (make-qd-d 1d0) a)))
	    (+ (log-qd a)
	       (log1p-qd (sqrt-qd (add-qd-d (sqr-qd 1/a) 1d0))))))))
toy's avatar
toy committed
1394

toy's avatar
toy committed
1395
(defun asinh-qd (a)
1396
  "Return the inverse hyperbolic sine of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  (declare (type %quad-double a))
  ;; asinh(x) = log(x + sqrt(1+x^2))
  ;;
  ;; But this doesn't work well when x is small.
  ;;
  ;; log(x + sqrt(1+x^2)) = log(sqrt(1+x^2)*(1+x/sqrt(1+x^2)))
  ;;   = log(sqrt(1+x^2)) + log(1+x/sqrt(1+x^2))
  ;;   = 1/2*log(1+x^2) + log(1+x/sqrt(1+x^2))
  ;;
  ;; However that doesn't work well when x is large because x^2
  ;; overflows.
  ;;
  ;; log(x + sqrt(1+x^2)) = log(x + x*sqrt(1+1/x^2))
  ;;   = log(x) + log(1+sqrt(1+1/x^2))
  ;;   = log(x) + log1p(sqrt(1+1/x^2))
  #+nil
  (log-qd (add-qd a
		  (sqrt-qd (add-qd-d (sqr-qd a)
				     1d0))))
  (cond ((< (abs (qd-0 a)) (sqrt most-positive-double-float))
	 (let ((a^2 (sqr-qd a)))
	   (add-qd (scale-float-qd (log1p-qd a^2) -1)
		   (log1p-qd (div-qd a
				     (sqrt-qd (add-qd-d a^2 1d0)))))))
	((float-infinity-p (qd-0 a))
	 a)
	(t
	 (if (minusp-qd a)
	     (neg-qd (asinh-qd (neg-qd a)))
	     (let ((1/a (div-qd (make-qd-d 1d0) a)))
1427 1428
	       (add-qd (log-qd a)
		       (log1p-qd (sqrt-qd (add-qd-d (sqr-qd 1/a) 1d0)))))))))
toy's avatar
toy committed
1429

toy's avatar
toy committed
1430
(defun acosh-qd (a)
1431
  "Return the inverse hyperbolic cosine of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1432 1433
  (declare (type %quad-double a))
  ;; acosh(x) = log(x + sqrt(x^2-1))
1434
  #+nil
toy's avatar
toy committed
1435 1436
  (log-qd (add-qd a
		  (sqrt-qd (sub-qd-d (sqr-qd a)
1437 1438 1439 1440 1441 1442 1443 1444
				     1d0))))
  ;; log(x+sqrt(x^2-1)) = log(x+sqrt((x-1)*(x+1)))
  ;;  = log(x+sqrt(x-1)*sqrt(x+1))
  #+nil
  (log-qd (add-qd a
		  (mul-qd
		   (sqrt-qd (sub-qd-d a 1d0))
		   (sqrt-qd (add-qd-d a 1d0)))))
1445
  ;; Let x = 1 + y
1446
  ;; log(1 + y + sqrt(y)*sqrt(y + 2))
1447 1448 1449 1450 1451 1452 1453 1454
  ;;   = log1p(y + sqrt(y)*sqrt(y + 2))
  ;;
  ;; However, that doesn't work well if x is large.
  ;;
  ;; log(x+sqrt(x^2-1)) = log(x+x*sqrt(1-1/x^2))
  ;;   = log(x) + log(1+sqrt(1-1/x^2))
  ;;   = log(x) + log1p(sqrt(1-1/x)*sqrt(1+1/x))
  ;;
1455 1456 1457 1458 1459 1460 1461
  (cond ((< (abs (qd-0 a)) (sqrt most-positive-double-float))
	 (let ((y (sub-qd-d a 1d0)))
	   (log1p-qd (add-qd y (sqrt-qd (mul-qd y (add-qd-d y 2d0)))))))
	((float-infinity-p (qd-0 a))
	 a)
	(t
	 (let ((1/a (div-qd (make-qd-d 1d0) a)))
1462 1463 1464
	   (add-qd (log-qd a)
		   (log1p-qd (mul-qd (sqrt-qd (sub-d-qd 1d0 1/a))
				     (sqrt-qd (add-d-qd 1d0 1/a)))))))))
toy's avatar
toy committed
1465 1466

(defun atanh-qd (a)
1467
  "Return the inverse hyperbolic tangent of the %QUAD-DOUBLE number A"
toy's avatar
toy committed
1468 1469 1470 1471
  (declare (type %quad-double a))
  ;; atanh(x) = 1/2*log((1+x)/(1-x))
  ;;          = 1/2*log(1+(2*x)/(1-x))
  ;; This latter expression works better for small x
1472
  #+nil
toy's avatar
toy committed
1473 1474
  (scale-float-qd (log-qd (div-qd (add-d-qd 1d0 a)
				  (sub-d-qd 1d0 a)))
1475
		  -1)
1476 1477 1478 1479 1480 1481 1482 1483
  ;; atanh(+/-1) = +/- infinity.  Signal a division by zero or return
  ;; infinity if the division-by-zero trap is disabled.
  (if (qd-= (abs-qd a) +qd-one+)
      (div-qd (make-qd-d (float-sign (qd-0 a)))
	      +qd-zero+)
      (scale-float-qd (log1p-qd (div-qd (scale-float-qd a 1)
					(sub-d-qd 1d0 a)))
		      -1)))
toy's avatar
toy committed
1484
  
1485 1486

(defun random-qd (&optional (state *random-state*))
1487
  "Generate a %QUAD-DOUBLE random number in the range [0,1)"
1488 1489 1490 1491 1492 1493 1494 1495
  (declare (optimize (speed 3)))
  ;; Strategy:  Generate 31 bits at a time, shift the bits and repeat 7 times.
  (let* ((r +qd-zero+)
	 (m-const (scale-float 1d0 -31))
	 (m m-const))
    (declare (type %quad-double r)
	     (double-float m-const m))
    (dotimes (k 7)
1496
      (let ((d (cl:* m (random #x7fffffff state))))
1497
	(setf r (add-qd-d r d))
1498
	(setf m (cl:* m m-const))))
1499 1500
    r))