]> code.delx.au - gnu-emacs/blobdiff - lisp/calc/calc-math.el
(gnus-newsrc-file-version): Add defvar.
[gnu-emacs] / lisp / calc / calc-math.el
index c7b841851e1f2223faa6d54c05d0a16c0ca3e42c..53d80350834c0daa6b619bc2f6ae28177f12af5a 100644 (file)
@@ -1,6 +1,10 @@
-;; Calculator for GNU Emacs, part II [calc-math.el]
-;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
-;; Written by Dave Gillespie, daveg@synaptics.com.
+;;; calc-math.el --- mathematical functions for Calc
+
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
+;;   2005 Free Software Foundation, Inc.
+
+;; Author: David Gillespie <daveg@synaptics.com>
+;; Maintainer: Jay Belanger <belanger@truman.edu>
 
 ;; This file is part of GNU Emacs.
 
 ;; file named COPYING.  Among other things, the copyright notice
 ;; and this notice must be preserved on all copies.
 
+;;; Commentary:
 
+;;; Code:
 
 ;; This file is autoloaded from calc-ext.el.
-(require 'calc-ext)
 
+(require 'calc-ext)
 (require 'calc-macs)
 
-(defun calc-Need-calc-math () nil)
-
-
 (defun calc-sqrt (arg)
   (interactive "P")
   (calc-slow-wrapper
    (if (calc-is-inverse)
        (calc-unary-op "^2" 'calcFunc-sqr arg)
-     (calc-unary-op "sqrt" 'calcFunc-sqrt arg)))
-)
+     (calc-unary-op "sqrt" 'calcFunc-sqrt arg))))
 
 (defun calc-isqrt (arg)
   (interactive "P")
   (calc-slow-wrapper
    (if (calc-is-inverse)
        (calc-unary-op "^2" 'calcFunc-sqr arg)
-     (calc-unary-op "isqt" 'calcFunc-isqrt arg)))
-)
+     (calc-unary-op "isqt" 'calcFunc-isqrt arg))))
 
 
 (defun calc-hypot (arg)
   (interactive "P")
   (calc-slow-wrapper
-   (calc-binary-op "hypt" 'calcFunc-hypot arg))
-)
+   (calc-binary-op "hypt" 'calcFunc-hypot arg)))
 
 (defun calc-ln (arg)
   (interactive "P")
   (calc-invert-func)
-  (calc-exp arg)
-)
+  (calc-exp arg))
 
 (defun calc-log10 (arg)
   (interactive "P")
   (calc-hyperbolic-func)
-  (calc-ln arg)
-)
+  (calc-ln arg))
 
 (defun calc-log (arg)
   (interactive "P")
   (calc-slow-wrapper
    (if (calc-is-inverse)
        (calc-binary-op "alog" 'calcFunc-alog arg)
-     (calc-binary-op "log" 'calcFunc-log arg)))
-)
+     (calc-binary-op "log" 'calcFunc-log arg))))
 
 (defun calc-ilog (arg)
   (interactive "P")
   (calc-slow-wrapper
    (if (calc-is-inverse)
        (calc-binary-op "alog" 'calcFunc-alog arg)
-     (calc-binary-op "ilog" 'calcFunc-ilog arg)))
-)
+     (calc-binary-op "ilog" 'calcFunc-ilog arg))))
 
 (defun calc-lnp1 (arg)
   (interactive "P")
   (calc-invert-func)
-  (calc-expm1 arg)
-)
+  (calc-expm1 arg))
 
 (defun calc-exp (arg)
   (interactive "P")
         (calc-unary-op "10^" 'calcFunc-exp10 arg))
      (if (calc-is-inverse)
         (calc-unary-op "ln" 'calcFunc-ln arg)
-       (calc-unary-op "exp" 'calcFunc-exp arg))))
-)
+       (calc-unary-op "exp" 'calcFunc-exp arg)))))
 
 (defun calc-expm1 (arg)
   (interactive "P")
   (calc-slow-wrapper
    (if (calc-is-inverse)
        (calc-unary-op "ln+1" 'calcFunc-lnp1 arg)
-     (calc-unary-op "ex-1" 'calcFunc-expm1 arg)))
-)
+     (calc-unary-op "ex-1" 'calcFunc-expm1 arg))))
 
 (defun calc-pi ()
   (interactive)
           (calc-pop-push-record 0 "e" (math-e)))
        (if calc-symbolic-mode
           (calc-pop-push-record 0 "pi" '(var pi var-pi))
-        (calc-pop-push-record 0 "pi" (math-pi))))))
-)
+        (calc-pop-push-record 0 "pi" (math-pi)))))))
 
 (defun calc-sin (arg)
   (interactive "P")
         (calc-unary-op "sinh" 'calcFunc-sinh arg))
      (if (calc-is-inverse)
         (calc-unary-op "asin" 'calcFunc-arcsin arg)
-       (calc-unary-op "sin" 'calcFunc-sin arg))))
-)
+       (calc-unary-op "sin" 'calcFunc-sin arg)))))
 
 (defun calc-arcsin (arg)
   (interactive "P")
   (calc-invert-func)
-  (calc-sin arg)
-)
+  (calc-sin arg))
 
 (defun calc-sinh (arg)
   (interactive "P")
   (calc-hyperbolic-func)
-  (calc-sin arg)
-)
+  (calc-sin arg))
 
 (defun calc-arcsinh (arg)
   (interactive "P")
   (calc-invert-func)
   (calc-hyperbolic-func)
-  (calc-sin arg)
-)
+  (calc-sin arg))
+
+(defun calc-sec (arg)
+  (interactive "P")
+  (calc-slow-wrapper
+   (if (calc-is-hyperbolic)
+       (calc-unary-op "sech" 'calcFunc-sech arg)
+     (calc-unary-op "sec" 'calcFunc-sec arg))))
+
+(defun calc-sech (arg)
+  (interactive "P")
+  (calc-hyperbolic-func)
+  (calc-sec arg))
 
 (defun calc-cos (arg)
   (interactive "P")
         (calc-unary-op "cosh" 'calcFunc-cosh arg))
      (if (calc-is-inverse)
         (calc-unary-op "acos" 'calcFunc-arccos arg)
-       (calc-unary-op "cos" 'calcFunc-cos arg))))
-)
+       (calc-unary-op "cos" 'calcFunc-cos arg)))))
 
 (defun calc-arccos (arg)
   (interactive "P")
   (calc-invert-func)
-  (calc-cos arg)
-)
+  (calc-cos arg))
 
 (defun calc-cosh (arg)
   (interactive "P")
   (calc-hyperbolic-func)
-  (calc-cos arg)
-)
+  (calc-cos arg))
 
 (defun calc-arccosh (arg)
   (interactive "P")
   (calc-invert-func)
   (calc-hyperbolic-func)
-  (calc-cos arg)
-)
+  (calc-cos arg))
+
+(defun calc-csc (arg)
+  (interactive "P")
+  (calc-slow-wrapper
+   (if (calc-is-hyperbolic)
+       (calc-unary-op "csch" 'calcFunc-csch arg)
+     (calc-unary-op "csc" 'calcFunc-csc arg))))
+
+(defun calc-csch (arg)
+  (interactive "P")
+  (calc-hyperbolic-func)
+  (calc-csc arg))
 
 (defun calc-sincos ()
   (interactive)
   (calc-slow-wrapper
    (if (calc-is-inverse)
        (calc-enter-result 1 "asnc" (list 'calcFunc-arcsincos (calc-top-n 1)))
-     (calc-enter-result 1 "sncs" (list 'calcFunc-sincos (calc-top-n 1)))))
-)
+     (calc-enter-result 1 "sncs" (list 'calcFunc-sincos (calc-top-n 1))))))
 
 (defun calc-tan (arg)
   (interactive "P")
         (calc-unary-op "tanh" 'calcFunc-tanh arg))
      (if (calc-is-inverse)
         (calc-unary-op "atan" 'calcFunc-arctan arg)
-       (calc-unary-op "tan" 'calcFunc-tan arg))))
-)
+       (calc-unary-op "tan" 'calcFunc-tan arg)))))
 
 (defun calc-arctan (arg)
   (interactive "P")
   (calc-invert-func)
-  (calc-tan arg)
-)
+  (calc-tan arg))
 
 (defun calc-tanh (arg)
   (interactive "P")
   (calc-hyperbolic-func)
-  (calc-tan arg)
-)
+  (calc-tan arg))
 
 (defun calc-arctanh (arg)
   (interactive "P")
   (calc-invert-func)
   (calc-hyperbolic-func)
-  (calc-tan arg)
-)
+  (calc-tan arg))
+
+(defun calc-cot (arg)
+  (interactive "P")
+  (calc-slow-wrapper
+   (if (calc-is-hyperbolic)
+       (calc-unary-op "coth" 'calcFunc-coth arg)
+     (calc-unary-op "cot" 'calcFunc-cot arg))))
+
+(defun calc-coth (arg)
+  (interactive "P")
+  (calc-hyperbolic-func)
+  (calc-cot arg))
 
 (defun calc-arctan2 ()
   (interactive)
   (calc-slow-wrapper
-   (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2))))
-)
+   (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2)))))
 
 (defun calc-conj (arg)
   (interactive "P")
   (calc-wrapper
-   (calc-unary-op "conj" 'calcFunc-conj arg))
-)
+   (calc-unary-op "conj" 'calcFunc-conj arg)))
 
 (defun calc-imaginary ()
   (interactive)
   (calc-slow-wrapper
-   (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1))))
-)
-
-
+   (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1)))))
 
 (defun calc-to-degrees (arg)
   (interactive "P")
   (calc-wrapper
-   (calc-unary-op ">deg" 'calcFunc-deg arg))
-)
+   (calc-unary-op ">deg" 'calcFunc-deg arg)))
 
 (defun calc-to-radians (arg)
   (interactive "P")
   (calc-wrapper
-   (calc-unary-op ">rad" 'calcFunc-rad arg))
-)
+   (calc-unary-op ">rad" 'calcFunc-rad arg)))
 
 
 (defun calc-degrees-mode (arg)
   (cond ((= arg 1)
         (calc-wrapper
          (calc-change-mode 'calc-angle-mode 'deg)
-         (message "Angles measured in degrees.")))
+         (message "Angles measured in degrees")))
        ((= arg 2) (calc-radians-mode))
        ((= arg 3) (calc-hms-mode))
-       (t (error "Prefix argument out of range")))
-)
+       (t (error "Prefix argument out of range"))))
 
 (defun calc-radians-mode ()
   (interactive)
   (calc-wrapper
    (calc-change-mode 'calc-angle-mode 'rad)
-   (message "Angles measured in radians."))
-)
+   (message "Angles measured in radians")))
 
 
 ;;; Compute the integer square-root floor(sqrt(A)).  A > 0.  [I I] [Public]
        ((integerp a)
         (math-isqrt-small a))
        (t
-        (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a)))))))
-)
+        (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a))))))))
 
 (defun calcFunc-isqrt (a)
   (if (math-realp a)
       (math-isqrt (math-floor a))
-    (math-floor (math-sqrt a)))
-)
+    (math-floor (math-sqrt a))))
 
 
-;;; This returns (flag . result) where the flag is T if A is a perfect square.
+;;; This returns (flag . result) where the flag is t if A is a perfect square.
 (defun math-isqrt-bignum (a)   ; [P.l L]
   (let ((len (length a)))
     (if (= (% len 2) 0)
         a
         (math-scale-bignum-3
          (list (1+ (math-isqrt-small top)))
-         (/ len 2))))))
-)
+         (/ len 2)))))))
 
 (defun math-isqrt-bignum-iter (a guess)   ; [l L l]
   (math-working "isqrt" (cons 'bigpos guess))
       (cons (and (= comp 0)
                 (math-zerop-bignum (cdr q))
                 (= (% (car s) 2) 0))
-           guess)))
-)
+           guess))))
 
 (defun math-zerop-bignum (a)
   (and (eq (car a) 0)
        (progn
         (while (eq (car (setq a (cdr a))) 0))
-        (null a)))
-)
+        (null a))))
 
 (defun math-scale-bignum-3 (a n)   ; [L L S]
   (while (> n 0)
     (setq a (cons 0 a)
          n (1- n)))
-  a
-)
+  a)
 
 (defun math-isqrt-small (a)   ; A > 0.  [S S]
   (let ((g (cond ((>= a 10000) 1000)
        g2)
     (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
       (setq g g2))
-    g)
-)
+    g))
 
 
 
          (math-mul (math-sqrt (math-infinite-dir a inf)) inf)))
    (progn
      (calc-record-why 'numberp a)
-     (list 'calcFunc-sqrt a)))
-)
-(fset 'calcFunc-sqrt (symbol-function 'math-sqrt))
+     (list 'calcFunc-sqrt a))))
+(defalias 'calcFunc-sqrt 'math-sqrt)
 
 (defun math-infinite-dir (a &optional inf)
   (or inf (setq inf (math-infinitep a)))
-  (math-normalize (math-expr-subst a inf 1))
-)
+  (math-normalize (math-expr-subst a inf 1)))
 
 (defun math-sqrt-float (a &optional guess)   ; [F F F]
   (if calc-symbolic-mode
       (signal 'inexact-result nil)
-    (math-with-extra-prec 1 (math-sqrt-raw a guess)))
-)
+    (math-with-extra-prec 1 (math-sqrt-raw a guess))))
 
 (defun math-sqrt-raw (a &optional guess)   ; [F F F]
   (if (not (Math-posp a))
          (setq guess (math-make-float (math-isqrt-small
                                        (math-scale-int (nth 1 a) (- ldiff)))
                                       (/ (+ (nth 2 a) ldiff) 2)))))
-    (math-sqrt-float-iter a guess))
-)
+    (math-sqrt-float-iter a guess)))
 
 (defun math-sqrt-float-iter (a guess)   ; [F F F]
   (math-working "sqrt" guess)
                            '(float 5 -1))))
      (if (math-nearly-equal-float g2 guess)
         g2
-       (math-sqrt-float-iter a g2)))
-)
+       (math-sqrt-float-iter a g2))))
 
 ;;; True if A and B differ only in the last digit of precision.  [P F F]
 (defun math-nearly-equal-float (a b)
           (and (not (consp ediff))
                (< ediff 10)
                (> ediff -10)
-               (= (math-numdigs (nth 1 a)) calc-internal-prec)))))
-)
+               (= (math-numdigs (nth 1 a)) calc-internal-prec))))))
 
 (defun math-nearly-equal (a b)   ;  [P N N] [Public]
   (setq a (math-float a))
       (if (eq (car b) 'cplx)
          (and (math-nearly-equal-float a (nth 1 b))
               (math-nearly-zerop-float a (nth 2 b)))
-       (math-nearly-equal-float a b)))
-)
+       (math-nearly-equal-float a b))))
 
 ;;; True if A is nearly zero compared to B.  [P F F]
 (defun math-nearly-zerop-float (a b)
   (or (eq (nth 1 a) 0)
       (<= (+ (math-numdigs (nth 1 a)) (nth 2 a))
-         (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec))))
-)
+         (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec)))))
 
 (defun math-nearly-zerop (a b)   ; [P N R] [Public]
   (setq a (math-float a))
           (math-nearly-zerop-float (nth 2 a) b))
     (if (eq (car a) 'polar)
        (math-nearly-zerop-float (nth 1 a) b)
-      (math-nearly-zerop-float a b)))
-)
+      (math-nearly-zerop-float a b))))
 
 ;;; This implementation could be improved, accuracy-wise.
 (defun math-hypot (a b)
           (math-to-hms (math-hypot (math-from-hms a 'deg) b))))
        ((eq (car-safe b) 'hms)
         (math-to-hms (math-hypot a (math-from-hms b 'deg))))
-       (t nil))
-)
-(fset 'calcFunc-hypot (symbol-function 'math-hypot))
+       (t nil)))
+(defalias 'calcFunc-hypot 'math-hypot)
 
 (defun calcFunc-sqr (x)
-  (math-pow x 2)
-)
+  (math-pow x 2))
 
 
 
        ((eq (car-safe a) 'polar)
         (let ((root (math-nth-root (nth 1 a) n)))
           (and root (list 'polar root (math-div (nth 2 a) n)))))
-       (t nil))
-)
+       (t nil)))
+
+;; The variables math-nrf-n, math-nrf-nf and math-nrf-nfm1 are local
+;; to math-nth-root-float, but are used by math-nth-root-float-iter,
+;; which is called by math-nth-root-float.
+(defvar math-nrf-n)
+(defvar math-nrf-nf)
+(defvar math-nrf-nfm1)
 
-(defun math-nth-root-float (a n &optional guess)
+(defun math-nth-root-float (a math-nrf-n &optional guess)
   (math-inexact-result)
   (math-with-extra-prec 1
-    (let ((nf (math-float n))
-         (nfm1 (math-float (1- n))))
+    (let ((math-nrf-nf (math-float math-nrf-n))
+         (math-nrf-nfm1 (math-float (1- math-nrf-n))))
       (math-nth-root-float-iter a (or guess
                                      (math-make-float
                                       1 (/ (+ (math-numdigs (nth 1 a))
                                               (nth 2 a)
-                                              (/ n 2))
-                                           n))))))
-)
+                                              (/ math-nrf-n 2))
+                                           math-nrf-n)))))))
 
-(defun math-nth-root-float-iter (a guess)   ; uses "n", "nf", "nfm1"
+(defun math-nth-root-float-iter (a guess)
   (math-working "root" guess)
-  (let ((g2 (math-div-float (math-add-float (math-mul nfm1 guess)
+  (let ((g2 (math-div-float (math-add-float (math-mul math-nrf-nfm1 guess)
                                            (math-div-float
-                                            a (math-ipow guess (1- n))))
-                           nf)))
+                                            a (math-ipow guess (1- math-nrf-n))))
+                           math-nrf-nf)))
     (if (math-nearly-equal-float g2 guess)
        g2
-      (math-nth-root-float-iter a g2)))
-)
+      (math-nth-root-float-iter a g2))))
 
-(defun math-nth-root-integer (a n &optional guess)   ; [I I S]
+;; The variable math-nri-n is local to math-nth-root-integer, but
+;; is used by math-nth-root-int-iter, which is called by
+;; math-nth-root-int.
+(defvar math-nri-n)
+
+(defun math-nth-root-integer (a math-nri-n &optional guess)   ; [I I S]
   (math-nth-root-int-iter a (or guess
                                (math-scale-int 1 (/ (+ (math-numdigs a)
-                                                       (1- n))
-                                                    n))))
-)
+                                                       (1- math-nri-n))
+                                                    math-nri-n)))))
 
-(defun math-nth-root-int-iter (a guess)   ; uses "n"
+(defun math-nth-root-int-iter (a guess)
   (math-working "root" guess)
-  (let* ((q (math-idivmod a (math-ipow guess (1- n))))
-        (s (math-add (car q) (math-mul (1- n) guess)))
-        (g2 (math-idivmod s n)))
+  (let* ((q (math-idivmod a (math-ipow guess (1- math-nri-n))))
+        (s (math-add (car q) (math-mul (1- math-nri-n) guess)))
+        (g2 (math-idivmod s math-nri-n)))
     (if (Math-natnum-lessp (car g2) guess)
        (math-nth-root-int-iter a (car g2))
       (cons (and (equal (car g2) guess)
                 (eq (cdr q) 0)
                 (eq (cdr g2) 0))
-           guess)))
-)
+           guess))))
 
 (defun calcFunc-nroot (x n)
   (calcFunc-pow x (if (integerp n)
                      (math-make-frac 1 n)
-                   (math-div 1 n)))
-)
+                   (math-div 1 n))))
 
 
 
         (math-from-hms a 'rad))
        ((memq calc-angle-mode '(deg hms))
         (math-mul a (math-pi-over-180)))
-       (t a))
-)
+       (t a)))
 
 (defun math-from-radians (a)   ; [N N]
   (cond ((eq calc-angle-mode 'deg)
           (list 'calcFunc-deg a)))
        ((eq calc-angle-mode 'hms)
         (math-to-hms a 'rad))
-       (t a))
-)
+       (t a)))
 
 (defun math-to-radians-2 (a)   ; [N N]
   (cond ((eq (car-safe a) 'hms)
         (if calc-symbolic-mode
             (math-div (math-mul a '(var pi var-pi)) 180)
           (math-mul a (math-pi-over-180))))
-       (t a))
-)
+       (t a)))
 
 (defun math-from-radians-2 (a)   ; [N N]
   (cond ((memq calc-angle-mode '(deg hms))
         (if calc-symbolic-mode
             (math-div (math-mul 180 a) '(var pi var-pi))
           (math-div a (math-pi-over-180))))
-       (t a))
-)
+       (t a)))
 
 
 
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'scalarp x)
-          (list 'calcFunc-sin x)))
-)
+          (list 'calcFunc-sin x))))
 
 (defun calcFunc-cos (x)   ; [N N] [Public]
   (cond ((and (integerp x)
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'scalarp x)
-          (list 'calcFunc-cos x)))
-)
+          (list 'calcFunc-cos x))))
 
 (defun calcFunc-sincos (x)   ; [V N] [Public]
   (if (Math-scalarp x)
       (math-with-extra-prec 2
        (let ((sc (math-sin-cos-raw (math-to-radians (math-float x)))))
          (list 'vec (cdr sc) (car sc))))    ; the vector [cos, sin]
-    (list 'vec (calcFunc-sin x) (calcFunc-cos x)))
-)
+    (list 'vec (calcFunc-sin x) (calcFunc-cos x))))
 
 (defun calcFunc-tan (x)   ; [N N] [Public]
   (cond ((and (integerp x)
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'scalarp x)
-          (list 'calcFunc-tan x)))
-)
+          (list 'calcFunc-tan x))))
+
+(defun calcFunc-sec (x)
+  (cond ((and (integerp x)
+              (eq calc-angle-mode 'deg)
+              (= (% x 180) 0))
+         (if (= (% x 360) 0)
+             1
+           -1))
+        ((and (integerp x)
+              (eq calc-angle-mode 'rad)
+              (= x 0))
+         1)
+        ((Math-scalarp x)
+         (math-with-extra-prec 2
+           (math-sec-raw (math-to-radians (math-float x)))))
+        ((eq (car x) 'sdev)
+         (if (math-constp x)
+             (math-with-extra-prec 2
+               (let* ((xx (math-to-radians (math-float (nth 1 x))))
+                      (xs (math-to-radians (math-float (nth 2 x))))
+                      (sc (math-sin-cos-raw xx)))
+                 (if (and (math-zerop (cdr sc))
+                          (not calc-infinite-mode))
+                     (progn
+                       (calc-record-why "*Division by zero")
+                       (list 'calcFunc-sec x))
+                   (math-make-sdev (math-div-float '(float 1 0) (cdr sc))
+                                   (math-div-float
+                                    (math-mul xs (car sc))
+                                    (math-sqr (cdr sc)))))))
+           (math-make-sdev (calcFunc-sec (nth 1 x))
+                           (math-div 
+                            (math-mul (nth 2 x)
+                                      (calcFunc-sin (nth 1 x)))
+                            (math-sqr (calcFunc-cos (nth 1 x)))))))
+        ((and (eq (car x) 'intv)
+              (math-intv-constp x))
+         (math-with-extra-prec 2
+           (let* ((xx (math-to-radians (math-float x)))
+                  (na (math-floor (math-div (math-sub (nth 2 xx)
+                                                      (math-pi-over-2))
+                                            (math-pi))))
+                  (nb (math-floor (math-div (math-sub (nth 3 xx)
+                                                      (math-pi-over-2))
+                                            (math-pi))))
+                  (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
+                  (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
+                  (span (math-sub nbb naa)))
+             (if (not (equal na nb))
+                 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
+               (let ((int (math-sort-intv (nth 1 x)
+                                          (math-sec-raw (nth 2 xx))
+                                          (math-sec-raw (nth 3 xx)))))
+                 (if (eq span 1)
+                     (if (math-evenp (math-div (math-add naa 1) 2))
+                         (math-make-intv (logior (nth 1 int) 2)
+                                         1
+                                         (nth 3 int))
+                       (math-make-intv (logior (nth 1 int) 1)
+                                       (nth 2 int)
+                                       -1))
+                   int))))))
+        ((equal x '(var nan var-nan))
+         x)
+        (t (calc-record-why 'scalarp x)
+           (list 'calcFunc-sec x))))
+
+(defun calcFunc-csc (x)
+  (cond ((and (integerp x)
+              (eq calc-angle-mode 'deg)
+              (= (% (- x 90) 180) 0))
+         (if (= (% (- x 90) 360) 0)
+             1
+           -1))
+        ((Math-scalarp x)
+         (math-with-extra-prec 2
+           (math-csc-raw (math-to-radians (math-float x)))))
+        ((eq (car x) 'sdev)
+         (if (math-constp x)
+             (math-with-extra-prec 2
+               (let* ((xx (math-to-radians (math-float (nth 1 x))))
+                      (xs (math-to-radians (math-float (nth 2 x))))
+                      (sc (math-sin-cos-raw xx)))
+                 (if (and (math-zerop (car sc))
+                          (not calc-infinite-mode))
+                     (progn
+                       (calc-record-why "*Division by zero")
+                       (list 'calcFunc-csc x))
+                   (math-make-sdev (math-div-float '(float 1 0) (car sc))
+                                   (math-div-float
+                                    (math-mul xs (cdr sc))
+                                    (math-sqr (car sc)))))))
+           (math-make-sdev (calcFunc-csc (nth 1 x))
+                           (math-div 
+                            (math-mul (nth 2 x)
+                                      (calcFunc-cos (nth 1 x)))
+                            (math-sqr (calcFunc-sin (nth 1 x)))))))
+        ((and (eq (car x) 'intv)
+              (math-intv-constp x))
+         (math-with-extra-prec 2
+           (let* ((xx (math-to-radians (math-float x)))
+                  (na (math-floor (math-div (nth 2 xx) (math-pi))))
+                  (nb (math-floor (math-div (nth 3 xx) (math-pi))))
+                  (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
+                  (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
+                  (span (math-sub nbb naa)))
+             (if (not (equal na nb))
+                 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
+               (let ((int (math-sort-intv (nth 1 x)
+                                          (math-csc-raw (nth 2 xx))
+                                          (math-csc-raw (nth 3 xx)))))
+                 (if (eq span 1)
+                     (if (math-evenp (math-div naa 2))
+                         (math-make-intv (logior (nth 1 int) 2)
+                                         1
+                                         (nth 3 int))
+                       (math-make-intv (logior (nth 1 int) 1)
+                                       (nth 2 int)
+                                       -1))
+                   int))))))
+        ((equal x '(var nan var-nan))
+         x)
+        (t (calc-record-why 'scalarp x)
+           (list 'calcFunc-csc x))))
+
+(defun calcFunc-cot (x)   ; [N N] [Public]
+  (cond ((and (integerp x)
+             (if (eq calc-angle-mode 'deg)
+                 (= (% (- x 90) 180) 0)
+               (= x 0)))
+        0)
+       ((Math-scalarp x)
+        (math-with-extra-prec 2
+          (math-cot-raw (math-to-radians (math-float x)))))
+       ((eq (car x) 'sdev)
+        (if (math-constp x)
+            (math-with-extra-prec 2
+              (let* ((xx (math-to-radians (math-float (nth 1 x))))
+                     (xs (math-to-radians (math-float (nth 2 x))))
+                     (sc (math-sin-cos-raw xx)))
+                (if (and (math-zerop (car sc)) (not calc-infinite-mode))
+                    (progn
+                      (calc-record-why "*Division by zero")
+                      (list 'calcFunc-cot x))
+                  (math-make-sdev (math-div-float (cdr sc) (car sc))
+                                  (math-div-float xs (math-sqr (car sc)))))))
+          (math-make-sdev (calcFunc-cot (nth 1 x))
+                          (math-div (nth 2 x)
+                                    (math-sqr (calcFunc-sin (nth 1 x)))))))
+       ((and (eq (car x) 'intv) (math-intv-constp x))
+        (or (math-with-extra-prec 2
+              (let* ((xx (math-to-radians (math-float x)))
+                     (na (math-floor (math-div (nth 2 xx) (math-pi))))
+                     (nb (math-floor (math-div (nth 3 xx) (math-pi)))))
+                (and (equal na nb)
+                     (math-sort-intv (nth 1 x)
+                                     (math-cot-raw (nth 2 xx))
+                                     (math-cot-raw (nth 3 xx))))))
+            '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
+       ((equal x '(var nan var-nan))
+        x)
+       (t (calc-record-why 'scalarp x)
+          (list 'calcFunc-cot x))))
 
 (defun math-sin-raw (x)   ; [N N]
   (cond ((eq (car x) 'cplx)
         (math-neg-float (math-sin-raw (math-neg-float x))))
        ((math-lessp-float '(float 7 0) x)  ; avoid inf loops due to roundoff
         (math-sin-raw (math-mod x (math-two-pi))))
-       (t (math-sin-raw-2 x x)))
-)
+       (t (math-sin-raw-2 x x))))
 
 (defun math-cos-raw (x)   ; [N N]
   (if (eq (car-safe x) 'polar)
       (math-polar (math-cos-raw (math-complex x)))
-    (math-sin-raw (math-sub (math-pi-over-2) x)))
-)
+    (math-sin-raw (math-sub (math-pi-over-2) x))))
+
+(defun math-sec-raw (x)   ; [N N]
+  (cond ((eq (car x) 'cplx)
+        (let* ((x (math-mul x '(float 1 0)))
+                (expx (math-exp-raw (nth 2 x)))
+               (expmx (math-div-float '(float 1 0) expx))
+                (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
+                (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
+               (sc (math-sin-cos-raw (nth 1 x)))
+               (d (math-add-float 
+                    (math-mul-float (math-sqr (car sc))
+                                    (math-sqr sh))
+                    (math-mul-float (math-sqr (cdr sc))
+                                    (math-sqr ch)))))
+          (and (not (eq (nth 1 d) 0))
+               (list 'cplx
+                     (math-div-float (math-mul-float (cdr sc) ch) d)
+                     (math-div-float (math-mul-float (car sc) sh) d)))))
+       ((eq (car x) 'polar)
+        (math-polar (math-sec-raw (math-complex x))))
+       (t
+        (let ((cs (math-cos-raw x)))
+           (if (eq cs 0)
+               (math-div 1 0)
+            (math-div-float '(float 1 0) cs))))))
+
+(defun math-csc-raw (x)   ; [N N]
+  (cond ((eq (car x) 'cplx)
+        (let* ((x (math-mul x '(float 1 0)))
+                (expx (math-exp-raw (nth 2 x)))
+               (expmx (math-div-float '(float 1 0) expx))
+                (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
+                (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
+               (sc (math-sin-cos-raw (nth 1 x)))
+               (d (math-add-float 
+                    (math-mul-float (math-sqr (car sc))
+                                    (math-sqr ch))
+                    (math-mul-float (math-sqr (cdr sc))
+                                    (math-sqr sh)))))
+          (and (not (eq (nth 1 d) 0))
+               (list 'cplx
+                     (math-div-float (math-mul-float (car sc) ch) d)
+                     (math-div-float (math-mul-float (cdr sc) sh) d)))))
+       ((eq (car x) 'polar)
+        (math-polar (math-csc-raw (math-complex x))))
+       (t
+        (let ((sn (math-sin-raw x)))
+           (if (eq sn 0)
+               (math-div 1 0)
+            (math-div-float '(float 1 0) sn))))))
+
+(defun math-cot-raw (x)   ; [N N]
+  (cond ((eq (car x) 'cplx)
+        (let* ((x (math-mul x '(float 1 0)))
+                (expx (math-exp-raw (nth 2 x)))
+               (expmx (math-div-float '(float 1 0) expx))
+                (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
+                (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
+               (sc (math-sin-cos-raw (nth 1 x)))
+               (d (math-add-float 
+                    (math-sqr (car sc))
+                    (math-sqr sh))))
+          (and (not (eq (nth 1 d) 0))
+               (list 'cplx
+                     (math-div-float 
+                       (math-mul-float (car sc) (cdr sc))
+                       d)
+                      (math-neg
+                       (math-div-float 
+                        (math-mul-float sh ch) 
+                        d))))))
+       ((eq (car x) 'polar)
+        (math-polar (math-cot-raw (math-complex x))))
+       (t
+        (let ((sc (math-sin-cos-raw x)))
+          (if (eq (nth 1 (car sc)) 0)
+              (math-div (cdr sc) 0)
+            (math-div-float (cdr sc) (car sc)))))))
+
 
 ;;; This could use a smarter method:  Reduce x as in math-sin-raw, then
 ;;;   compute either sin(x) or cos(x), whichever is smaller, and compute
 ;;;   the other using the identity sin(x)^2 + cos(x)^2 = 1.
 (defun math-sin-cos-raw (x)   ; [F.F F]  (result is (sin x . cos x))
-  (cons (math-sin-raw x) (math-cos-raw x))
-)
+  (cons (math-sin-raw x) (math-cos-raw x)))
 
 (defun math-tan-raw (x)   ; [N N]
   (cond ((eq (car x) 'cplx)
         (let ((sc (math-sin-cos-raw x)))
           (if (eq (nth 1 (cdr sc)) 0)
               (math-div (car sc) 0)
-            (math-div-float (car sc) (cdr sc))))))
-)
+            (math-div-float (car sc) (cdr sc)))))))
 
 (defun math-sin-raw-2 (x orgx)   ; This avoids poss of inf recursion.  [F F]
   (let ((xmpo2 (math-sub-float (math-pi-over-2) x)))
           (math-neg (math-cos-raw-2 (math-add (math-pi-over-2) x) orgx)))
          ((math-nearly-zerop-float x orgx) '(float 0 0))
          (calc-symbolic-mode (signal 'inexact-result nil))
-         (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x))))))
-)
+         (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x)))))))
 
 (defun math-cos-raw-2 (x orgx)   ; [F F]
   (cond ((math-nearly-zerop-float x orgx) '(float 1 0))
             (math-sin-series
              (math-add-float '(float 1 0)
                              (math-mul-float xnegsqr '(float 5 -1)))
-             24 5 xnegsqr xnegsqr))))
-)
+             24 5 xnegsqr xnegsqr)))))
 
 (defun math-sin-series (sum nfac n x xnegsqr)
   (math-working "sin" sum)
     (if (math-nearly-equal-float sum nextsum)
        sum
       (math-sin-series nextsum (math-mul nfac (* n (1+ n)))
-                      (+ n 2) nextx xnegsqr)))
-)
+                      (+ n 2) nextx xnegsqr))))
 
 
 ;;; Inverse sine, cosine, tangent.
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-arcsin x)))
-)
+          (list 'calcFunc-arcsin x))))
 
 (defun calcFunc-arccos (x)   ; [N N] [Public]
   (cond ((eq x 1) 0)
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-arccos x)))
-)
+          (list 'calcFunc-arccos x))))
 
 (defun calcFunc-arctan (x)   ; [N N] [Public]
   (cond ((eq x 0) 0)
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-arctan x)))
-)
+          (list 'calcFunc-arctan x))))
 
 (defun math-arcsin-raw (x)   ; [N N]
   (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
        (math-with-extra-prec 2   ; use extra precision for difficult case
          (math-mul '(cplx 0 -1)
                    (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
-      (math-arctan2-raw x a)))
-)
+      (math-arctan2-raw x a))))
 
 (defun math-arccos-raw (x)   ; [N N]
-  (math-sub (math-pi-over-2) (math-arcsin-raw x))
-)
+  (math-sub (math-pi-over-2) (math-arcsin-raw x)))
 
 (defun math-arctan-raw (x)   ; [N N]
   (cond ((memq (car x) '(cplx polar))
                                             (math-sub-float '(float 1 0) x)
                                             (math-add-float '(float 1 0)
                                                             x))))))
-       (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x)))))
-)
+       (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x))))))
 
 (defun math-arctan-series (sum n x xnegsqr)
   (math-working "arctan" sum)
         (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
     (if (math-nearly-equal-float sum nextsum)
        sum
-      (math-arctan-series nextsum (+ n 2) nextx xnegsqr)))
-)
+      (math-arctan-series nextsum (+ n 2) nextx xnegsqr))))
 
 (defun calcFunc-arctan2 (y x)   ; [F R R] [Public]
   (if (Math-anglep y)
              (calcFunc-arctan2 y x)
            '(var nan var-nan)))
       (calc-record-why 'anglep y)
-      (list 'calcFunc-arctan2 y x)))
-)
+      (list 'calcFunc-arctan2 y x))))
 
 (defun math-arctan2-raw (y x)   ; [F R R]
   (cond ((math-zerop y)
                         (math-pi)))
        (t
         (math-sub-float (math-arctan-raw (math-div-float y x))
-                        (math-pi))))
-)
+                        (math-pi)))))
 
 (defun calcFunc-arcsincos (x)   ; [V N] [Public]
   (if (and (Math-vectorp x)
           (= (length x) 3))
       (calcFunc-arctan2 (nth 2 x) (nth 1 x))
-    (math-reject-arg x "*Two-element vector expected"))
-)
+    (math-reject-arg x "*Two-element vector expected")))
 
 
 
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-exp x)))
-)
+          (list 'calcFunc-exp x))))
 
 (defun calcFunc-expm1 (x)   ; [N N] [Public]
   (cond ((eq x 0) 0)
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-expm1 x)))
-)
+          (list 'calcFunc-expm1 x))))
 
 (defun calcFunc-exp10 (x)   ; [N N] [Public]
   (if (eq x 0)
       1
-    (math-pow '(float 1 1) x))
-)
+    (math-pow '(float 1 1) x)))
 
 (defun math-exp-raw (x)   ; [N N]
   (cond ((math-zerop x) '(float 1 0))
           (math-mul-float (math-ipow (math-sqrt-e) hint)
                           (math-add-float '(float 1 0)
                                           (math-exp-minus-1-raw hfrac)))))
-       (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x))))
-)
+       (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x)))))
 
 (defun math-exp-minus-1-raw (x)   ; [F F]
-  (math-exp-series x 2 3 x x)
-)
+  (math-exp-series x 2 3 x x))
 
 (defun math-exp-series (sum nfac n xpow x)
   (math-working "exp" sum)
                                                      (math-float nfac)))))
     (if (math-nearly-equal-float sum nextsum)
        sum
-      (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x)))
-)
+      (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x))))
 
 
 
             x
           '(var inf var-inf)))
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-ln x)))
-)
+          (list 'calcFunc-ln x))))
 
 (defun calcFunc-log10 (x)   ; [N N] [Public]
   (cond ((math-equal-int x 1)
             x
           '(var inf var-inf)))
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-log10 x)))
-)
+          (list 'calcFunc-log10 x))))
 
 (defun calcFunc-log (x &optional b)   ; [N N N] [Public]
   (cond ((or (null b) (equal b '(var e var-e)))
             (math-div (calcFunc-ln x) 0)
           (math-reject-arg b "*Logarithm base one")))
        ((math-equal-int x 1)
-        (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0))
+        (if (math-floatp b) '(float 0 0) 0))
        ((and (Math-ratp x) (Math-ratp b)
              (math-posp x) (math-posp b)
              (let* ((sign 1) (inv nil)
        (t (if (Math-numberp b)
               (calc-record-why 'numberp x)
             (calc-record-why 'numberp b))
-          (list 'calcFunc-log x b)))
-)
+          (list 'calcFunc-log x b))))
 
 (defun calcFunc-alog (x &optional b)
   (cond ((or (null b) (equal b '(var e var-e)))
         (math-normalize (list 'calcFunc-exp x)))
-       (t (math-pow b x)))
-)
+       (t (math-pow b x))))
 
 (defun calcFunc-ilog (x b)
   (if (and (math-natnump x) (not (eq x 0))
        (if (Math-natnum-lessp x b)
            0
          (cdr (math-integer-log x b))))
-    (math-floor (calcFunc-log x b)))
-)
+    (math-floor (calcFunc-log x b))))
 
 (defun math-integer-log (x b)
   (let ((pows (list b))
       (or (Math-lessp x next)
          (setq pow next
                sum (+ sum n))))
-    (cons (equal pow x) sum))
-)
+    (cons (equal pow x) sum)))
 
 
+(defvar math-log-base-cache nil)
 (defun math-log-base-raw (b)   ; [N N]
   (if (not (and (equal (car math-log-base-cache) b)
                (eq (nth 1 math-log-base-cache) calc-internal-prec)))
       (setq math-log-base-cache (list b calc-internal-prec
                                      (math-ln-raw (math-float b)))))
-  (nth 2 math-log-base-cache)
-)
-(setq math-log-base-cache nil)
+  (nth 2 math-log-base-cache))
 
 (defun calcFunc-lnp1 (x)   ; [N N] [Public]
   (cond ((Math-equal-int x -1)
             x
           '(var inf var-inf)))
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-lnp1 x)))
-)
+          (list 'calcFunc-lnp1 x))))
 
 (defun math-ln-raw (x)    ; [N N] --- must be float format!
   (cond ((eq (car-safe x) 'cplx)
                (math-pi))))
        (t (list 'cplx   ; negative and real
                 (math-ln-raw (math-neg-float x))
-                (math-pi))))
-)
+                (math-pi)))))
 
 (defun math-ln-raw-2 (x)    ; [F F]
   (cond ((math-lessp-float '(float 14 -1) x)
                         (math-ln-2)))
        (t    ; now .7 < x <= 1.4
         (math-ln-raw-3 (math-div-float (math-sub-float x '(float 1 0))
-                                       (math-add-float x '(float 1 0))))))
-)
+                                       (math-add-float x '(float 1 0)))))))
 
 (defun math-ln-raw-3 (x)   ; [F F]
   (math-mul-float (math-ln-raw-series x 3 x (math-sqr-float x))
-                 '(float 2 0))
-)
+                 '(float 2 0)))
 
 ;;; Compute ln((1+x)/(1-x))
 (defun math-ln-raw-series (sum n x xsqr)
         (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
     (if (math-nearly-equal-float sum nextsum)
        sum
-      (math-ln-raw-series nextsum (+ n 2) nextx xsqr)))
-)
+      (math-ln-raw-series nextsum (+ n 2) nextx xsqr))))
 
 (defun math-ln-plus-1-raw (x)
-  (math-lnp1-series x 2 x (math-neg x))
-)
+  (math-lnp1-series x 2 x (math-neg x)))
 
 (defun math-lnp1-series (sum n xpow x)
   (math-working "lnp1" sum)
         (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
     (if (math-nearly-equal-float sum nextsum)
        sum
-      (math-lnp1-series nextsum (1+ n) nextx x)))
-)
+      (math-lnp1-series nextsum (1+ n) nextx x))))
 
 (math-defcache math-ln-10 (float (bigpos 018 684 045 994 092 585 302 2) -21)
   (math-ln-raw-2 '(float 1 1)))
             (equal x '(var nan var-nan)))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-sinh x)))
-)
+          (list 'calcFunc-sinh x))))
 (put 'calcFunc-sinh 'math-expandable t)
 
 (defun calcFunc-cosh (x)   ; [N N] [Public]
             (equal x '(var nan var-nan)))
         (math-abs x))
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-cosh x)))
-)
+          (list 'calcFunc-cosh x))))
 (put 'calcFunc-cosh 'math-expandable t)
 
 (defun calcFunc-tanh (x)   ; [N N] [Public]
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-tanh x)))
-)
+          (list 'calcFunc-tanh x))))
 (put 'calcFunc-tanh 'math-expandable t)
 
+(defun calcFunc-sech (x)   ; [N N] [Public]
+  (cond ((eq x 0) 1)
+       (math-expand-formulas
+        (math-normalize
+         (list '/ 2 (list '+ (list 'calcFunc-exp x)
+                           (list 'calcFunc-exp (list 'neg x))))))
+       ((Math-numberp x)
+        (if calc-symbolic-mode (signal 'inexact-result nil))
+        (math-with-extra-prec 2
+          (let ((expx (math-exp-raw (math-float x))))
+            (math-div '(float 2 0) (math-add expx (math-div 1 expx))))))
+       ((eq (car-safe x) 'sdev)
+        (math-make-sdev (calcFunc-sech (nth 1 x))
+                        (math-mul (nth 2 x)
+                                   (math-mul (calcFunc-sech (nth 1 x))
+                                             (calcFunc-tanh (nth 1 x))))))
+       ((and (eq (car x) 'intv) (math-intv-constp x))
+        (setq x (math-abs x))
+        (math-sort-intv (nth 1 x)
+                        (calcFunc-sech (nth 2 x))
+                        (calcFunc-sech (nth 3 x))))
+       ((or (equal x '(var inf var-inf))
+            (equal x '(neg (var inf var-inf))))
+         0)
+        ((equal x '(var nan var-nan))
+         x)
+       (t (calc-record-why 'numberp x)
+          (list 'calcFunc-sech x))))
+(put 'calcFunc-sech 'math-expandable t)
+
+(defun calcFunc-csch (x)   ; [N N] [Public]
+  (cond ((eq x 0) (math-div 1 0))
+       (math-expand-formulas
+        (math-normalize
+         (list '/ 2 (list '- (list 'calcFunc-exp x)
+                           (list 'calcFunc-exp (list 'neg x))))))
+       ((Math-numberp x)
+        (if calc-symbolic-mode (signal 'inexact-result nil))
+        (math-with-extra-prec 2
+          (let ((expx (math-exp-raw (math-float x))))
+            (math-div '(float 2 0) (math-add expx (math-div -1 expx))))))
+       ((eq (car-safe x) 'sdev)
+        (math-make-sdev (calcFunc-csch (nth 1 x))
+                        (math-mul (nth 2 x) 
+                                   (math-mul (calcFunc-csch (nth 1 x))
+                                             (calcFunc-coth (nth 1 x))))))
+       ((eq (car x) 'intv)
+         (if (and (Math-negp (nth 2 x))
+                  (Math-posp (nth 3 x)))
+             '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
+           (math-sort-intv (nth 1 x)
+                           (calcFunc-csch (nth 2 x))
+                           (calcFunc-csch (nth 3 x)))))
+       ((or (equal x '(var inf var-inf))
+            (equal x '(neg (var inf var-inf))))
+         0)
+        ((equal x '(var nan var-nan))
+         x)
+       (t (calc-record-why 'numberp x)
+          (list 'calcFunc-csch x))))
+(put 'calcFunc-csch 'math-expandable t)
+
+(defun calcFunc-coth (x)   ; [N N] [Public]
+  (cond ((eq x 0) (math-div 1 0))
+       (math-expand-formulas
+        (math-normalize
+         (let ((expx (list 'calcFunc-exp x))
+               (expmx (list 'calcFunc-exp (list 'neg x))))
+           (math-normalize
+            (list '/ (list '+ expx expmx) (list '- expx expmx))))))
+       ((Math-numberp x)
+        (if calc-symbolic-mode (signal 'inexact-result nil))
+        (math-with-extra-prec 2
+          (let* ((expx (calcFunc-exp (math-float x)))
+                 (expmx (math-div 1 expx)))
+            (math-div (math-add expx expmx)
+                      (math-sub expx expmx)))))
+       ((eq (car-safe x) 'sdev)
+        (math-make-sdev (calcFunc-coth (nth 1 x))
+                        (math-div (nth 2 x)
+                                  (math-sqr (calcFunc-sinh (nth 1 x))))))
+       ((eq (car x) 'intv)
+         (if (and (Math-negp (nth 2 x))
+                  (Math-posp (nth 3 x)))
+             '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
+           (math-sort-intv (nth 1 x)
+                           (calcFunc-coth (nth 2 x))
+                           (calcFunc-coth (nth 3 x)))))
+       ((equal x '(var inf var-inf))
+        1)
+       ((equal x '(neg (var inf var-inf)))
+        -1)
+       ((equal x '(var nan var-nan))
+        x)
+       (t (calc-record-why 'numberp x)
+          (list 'calcFunc-coth x))))
+(put 'calcFunc-coth 'math-expandable t)
+
 (defun calcFunc-arcsinh (x)   ; [N N] [Public]
   (cond ((eq x 0) 0)
        (math-expand-formulas
             (equal x '(var nan var-nan)))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-arcsinh x)))
-)
+          (list 'calcFunc-arcsinh x))))
 (put 'calcFunc-arcsinh 'math-expandable t)
 
 (defun calcFunc-arccosh (x)   ; [N N] [Public]
             (equal x '(var nan var-nan)))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-arccosh x)))
-)
+          (list 'calcFunc-arccosh x))))
 (put 'calcFunc-arccosh 'math-expandable t)
 
 (defun calcFunc-arctanh (x)   ; [N N] [Public]
        ((equal x '(var nan var-nan))
         x)
        (t (calc-record-why 'numberp x)
-          (list 'calcFunc-arctanh x)))
-)
+          (list 'calcFunc-arctanh x))))
 (put 'calcFunc-arctanh 'math-expandable t)
 
 
        (math-expand-formulas
         (math-div (math-mul a '(var pi var-pi)) 180))
        ((math-infinitep a) a)
-       (t (list 'calcFunc-rad a)))
-)
+       (t (list 'calcFunc-rad a))))
 (put 'calcFunc-rad 'math-expandable t)
 
 ;;; Convert A from HMS or radians to degrees.
        (math-expand-formulas
         (math-div (math-mul 180 a) '(var pi var-pi)))
        ((math-infinitep a) a)
-       (t (list 'calcFunc-deg a)))
-)
+       (t (list 'calcFunc-deg a))))
 (put 'calcFunc-deg 'math-expandable t)
 
+(provide 'calc-math)
 
-
-
+;;; arch-tag: c7367e8e-d0b8-4f70-8577-2fb3f31dbb4c
+;;; calc-math.el ends here