home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
misc
/
volume24
/
gnucalc
/
part15
< prev
next >
Wrap
Lisp/Scheme
|
1991-10-29
|
57KB
|
1,801 lines
Newsgroups: comp.sources.misc
From: daveg@synaptics.com (David Gillespie)
Subject: v24i063: gnucalc - GNU Emacs Calculator, v2.00, Part15/56
Message-ID: <1991Oct29.230249.20498@sparky.imd.sterling.com>
X-Md4-Signature: f50ac648daab865903706eb51c2b4e86
Date: Tue, 29 Oct 1991 23:02:49 GMT
Approved: kent@sparky.imd.sterling.com
Submitted-by: daveg@synaptics.com (David Gillespie)
Posting-number: Volume 24, Issue 63
Archive-name: gnucalc/part15
Environment: Emacs
Supersedes: gmcalc: Volume 13, Issue 27-45
---- Cut Here and unpack ----
#!/bin/sh
# do not concatenate these parts, unpack them in order with /bin/sh
# file calc-ext.el continued
#
if test ! -r _shar_seq_.tmp; then
echo 'Please unpack part 1 first!'
exit 1
fi
(read Scheck
if test "$Scheck" != 15; then
echo Please unpack part "$Scheck" next!
exit 1
else
exit 0
fi
) < _shar_seq_.tmp || exit 1
if test ! -f _shar_wnt_.tmp; then
echo 'x - still skipping calc-ext.el'
else
echo 'x - continuing file calc-ext.el'
sed 's/^X//' << 'SHAR_EOF' >> 'calc-ext.el' &&
X (let* ((calc-language nil)
X (math-expr-opers math-standard-opers)
X (val (math-read-expr exp-str)))
X (and error-check
X (eq (car-safe val) 'error)
X (error "%s: %s" (nth 2 val) exp-str))
X val)
)
X
X
(defun math-read-string ()
X (let ((str (read-from-string (concat exp-data "\""))))
X (or (and (= (cdr str) (1+ (length exp-data)))
X (stringp (car str)))
X (throw 'syntax "Error in string constant"))
X (math-read-token)
X (append '(vec) (car str) nil))
)
X
X
X
;;; They said it couldn't be done...
X
(defun math-read-big-expr (str)
X (and (> (length calc-left-label) 0)
X (string-match (concat "^" (regexp-quote calc-left-label)) str)
X (setq str (concat (substring str 0 (match-beginning 0))
X (substring str (match-end 0)))))
X (and (> (length calc-right-label) 0)
X (string-match (concat (regexp-quote calc-right-label) " *$") str)
X (setq str (concat (substring str 0 (match-beginning 0))
X (substring str (match-end 0)))))
X (if (string-match "\\\\[^ \n|]" str)
X (if (eq calc-language 'tex)
X (math-read-expr str)
X (let ((calc-language 'tex)
X (calc-language-option nil)
X (math-expr-opers (get 'tex 'math-oper-table))
X (math-expr-function-mapping (get 'tex 'math-function-table))
X (math-expr-variable-mapping (get 'tex 'math-variable-table)))
X (math-read-expr str)))
X (let ((lines nil)
X (pos 0)
X (width 0)
X (err-msg nil)
X the-baseline the-h2
X new-pos p)
X (while (setq new-pos (string-match "\n" str pos))
X (setq lines (cons (substring str pos new-pos) lines)
X pos (1+ new-pos)))
X (setq lines (nreverse (cons (substring str pos) lines))
X p lines)
X (while p
X (setq width (max width (length (car p)))
X p (cdr p)))
X (if (math-read-big-bigp lines)
X (or (catch 'syntax
X (math-read-big-rec 0 0 width (length lines)))
X err-msg
X '(error 0 "Syntax error"))
X (math-read-expr str))))
)
X
(defun math-read-big-bigp (lines)
X (and (cdr lines)
X (let ((matrix nil)
X (v 0)
X (height (if (> (length (car lines)) 0) 1 0)))
X (while (and (cdr lines)
X (let* ((i 0)
X j
X (l1 (car lines))
X (l2 (nth 1 lines))
X (len (min (length l1) (length l2))))
X (if (> (length l2) 0)
X (setq height (1+ height)))
X (while (and (< i len)
X (or (memq (aref l1 i) '(?\ ?\- ?\_))
X (memq (aref l2 i) '(?\ ?\-))
X (and (memq (aref l1 i) '(?\| ?\,))
X (= (aref l2 i) (aref l1 i)))
X (and (eq (aref l1 i) ?\[)
X (eq (aref l2 i) ?\[)
X (let ((h2 (length l1)))
X (setq j (math-read-big-balance
X (1+ i) v "[")))
X (setq i (1- j)))))
X (setq i (1+ i)))
X (or (= i len)
X (and (eq (aref l1 i) ?\[)
X (eq (aref l2 i) ?\[)
X (setq matrix t)
X nil))))
X (setq lines (cdr lines)
X v (1+ v)))
X (or (and (> height 1)
X (not (cdr lines)))
X matrix)))
)
X
X
X
;;; Nontrivial "flat" formatting.
X
(defun math-format-flat-expr-fancy (a prec)
X (cond
X ((eq (car a) 'incomplete)
X (format "<incomplete %s>" (nth 1 a)))
X ((eq (car a) 'vec)
X (if (or calc-full-trail-vectors (not calc-can-abbrev-vectors)
X (< (length a) 7))
X (concat "[" (math-format-flat-vector (cdr a) ", "
X (if (cdr (cdr a)) 0 1000)) "]")
X (concat "["
X (math-format-flat-expr (nth 1 a) 0) ", "
X (math-format-flat-expr (nth 2 a) 0) ", "
X (math-format-flat-expr (nth 3 a) 0) ", ..., "
X (math-format-flat-expr (nth (1- (length a)) a) 0) "]")))
X ((eq (car a) 'intv)
X (concat (if (memq (nth 1 a) '(0 1)) "(" "[")
X (math-format-flat-expr (nth 2 a) 1000)
X " .. "
X (math-format-flat-expr (nth 3 a) 1000)
X (if (memq (nth 1 a) '(0 2)) ")" "]")))
X ((eq (car a) 'date)
X (concat "<" (math-format-date a) ">"))
X ((and (eq (car a) 'calcFunc-lambda) (> (length a) 2))
X (let ((p (cdr a))
X (ap calc-arg-values)
X (math-format-hash-args (if (= (length a) 3) 1 t)))
X (while (and (cdr p) (equal (car p) (car ap)))
X (setq p (cdr p) ap (cdr ap)))
X (concat "<"
X (if (cdr p)
X (concat (math-format-flat-vector
X (nreverse (cdr (reverse (cdr a)))) ", " 0)
X " : ")
X "")
X (math-format-flat-expr (nth (1- (length a)) a) 0)
X ">")))
X ((eq (car a) 'var)
X (or (and math-format-hash-args
X (let ((p calc-arg-values) (v 1))
X (while (and p (not (equal (car p) a)))
X (setq p (and (eq math-format-hash-args t) (cdr p))
X v (1+ v)))
X (and p
X (if (eq math-format-hash-args 1)
X "#"
X (format "#%d" v)))))
X (symbol-name (nth 1 a))))
X ((and (memq (car a) '(calcFunc-string calcFunc-bstring))
X (= (length a) 2)
X (math-vectorp (nth 1 a))
X (math-vector-is-string (nth 1 a)))
X (concat (substring (symbol-name (car a)) 9)
X "(" (math-vector-to-string (nth 1 a) t)) ")")
X (t
X (let ((op (math-assq2 (car a) math-standard-opers)))
X (cond ((and op (= (length a) 3))
X (if (> prec (min (nth 2 op) (nth 3 op)))
X (concat "(" (math-format-flat-expr a 0) ")")
X (let ((lhs (math-format-flat-expr (nth 1 a) (nth 2 op)))
X (rhs (math-format-flat-expr (nth 2 a) (nth 3 op))))
X (setq op (car op))
X (if (or (equal op "^") (equal op "_"))
X (if (= (aref lhs 0) ?-)
X (setq lhs (concat "(" lhs ")")))
X (setq op (concat " " op " ")))
X (concat lhs op rhs))))
X ((eq (car a) 'neg)
X (concat "-" (math-format-flat-expr (nth 1 a) 1000)))
X (t
X (concat (math-remove-dashes
X (if (string-match "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
X (symbol-name (car a)))
X (math-match-substring (symbol-name (car a)) 1)
X (symbol-name (car a))))
X "("
X (math-format-flat-vector (cdr a) ", " 0)
X ")"))))))
)
(setq math-format-hash-args nil)
X
(defun math-format-flat-vector (vec sep prec)
X (if vec
X (let ((buf (math-format-flat-expr (car vec) prec)))
X (while (setq vec (cdr vec))
X (setq buf (concat buf sep (math-format-flat-expr (car vec) prec))))
X buf)
X "")
)
(setq calc-can-abbrev-vectors nil)
X
(defun math-format-nice-expr (x w)
X (cond ((and (eq (car-safe x) 'vec)
X (cdr (cdr x))
X (let ((ops '(vec calcFunc-assign calcFunc-condition
X calcFunc-schedule calcFunc-iterations
X calcFunc-phase)))
X (or (memq (car-safe (nth 1 x)) ops)
X (memq (car-safe (nth 2 x)) ops)
X (memq (car-safe (nth 3 x)) ops)
X calc-break-vectors)))
X (concat "[ " (math-format-flat-vector (cdr x) ",\n " 0) " ]"))
X (t
X (let ((str (math-format-flat-expr x 0))
X (pos 0) p)
X (or (string-match "\"" str)
X (while (<= (setq p (+ pos w)) (length str))
X (while (and (> (setq p (1- p)) pos)
X (not (= (aref str p) ? ))))
X (if (> p (+ pos 5))
X (setq str (concat (substring str 0 p)
X "\n "
X (substring str p))
X pos (1+ p))
X (setq pos (+ pos w)))))
X str)))
)
X
(defun math-assq2 (v a)
X (while (and a (not (eq v (nth 1 (car a)))))
X (setq a (cdr a)))
X (car a)
)
X
X
(defun math-format-number-fancy (a prec)
X (cond
X ((eq (car a) 'float) ; non-decimal radix
X (if (Math-integer-negp (nth 1 a))
X (concat "-" (math-format-number (math-neg a)))
X (let ((str (if (and calc-radix-formatter
X (not (memq calc-language '(c pascal))))
X (funcall calc-radix-formatter
X calc-number-radix
X (math-format-radix-float a prec))
X (format "%d#%s" calc-number-radix
X (math-format-radix-float a prec)))))
X (if (and prec (> prec 191) (string-match "\\*" str))
X (concat "(" str ")")
X str))))
X ((eq (car a) 'frac)
X (setq a (math-adjust-fraction a))
X (if (> (length (car calc-frac-format)) 1)
X (if (Math-integer-negp (nth 1 a))
X (concat "-" (math-format-number (math-neg a)))
X (let ((q (math-idivmod (nth 1 a) (nth 2 a))))
X (concat (let ((calc-frac-format nil))
X (math-format-number (car q)))
X (substring (car calc-frac-format) 0 1)
X (let ((math-radix-explicit-format nil)
X (calc-frac-format nil))
X (math-format-number (cdr q)))
X (substring (car calc-frac-format) 1 2)
X (let ((math-radix-explicit-format nil)
X (calc-frac-format nil))
X (math-format-number (nth 2 a))))))
X (concat (let ((calc-frac-format nil))
X (math-format-number (nth 1 a)))
X (car calc-frac-format)
X (let ((math-radix-explicit-format nil)
X (calc-frac-format nil))
X (math-format-number (nth 2 a))))))
X ((eq (car a) 'cplx)
X (if (math-zerop (nth 2 a))
X (math-format-number (nth 1 a))
X (if (null calc-complex-format)
X (concat "(" (math-format-number (nth 1 a))
X ", " (math-format-number (nth 2 a)) ")")
X (if (math-zerop (nth 1 a))
X (if (math-equal-int (nth 2 a) 1)
X (symbol-name calc-complex-format)
X (if prec
X (math-compose-expr (list '* (nth 2 a) '(cplx 0 1)) prec)
X (concat (math-format-number (nth 2 a)) " "
X (symbol-name calc-complex-format))))
X (if prec
X (math-compose-expr (list (if (math-negp (nth 2 a)) '- '+)
X (nth 1 a)
X (list 'cplx 0 (math-abs (nth 2 a))))
X prec)
X (concat (math-format-number (nth 1 a))
X (if (math-negp (nth 2 a)) " - " " + ")
X (math-format-number (math-abs (nth 2 a))) " "
X (symbol-name calc-complex-format)))))))
X ((eq (car a) 'polar)
X (concat "(" (math-format-number (nth 1 a))
X "; " (math-format-number (nth 2 a)) ")"))
X ((eq (car a) 'hms)
X (if (math-negp a)
X (concat "-" (math-format-number (math-neg a)))
X (let ((calc-number-radix 10)
X (calc-leading-zeros nil)
X (calc-group-digits nil))
X (format calc-hms-format
X (let ((calc-frac-format '(":" nil)))
X (math-format-number (nth 1 a)))
X (let ((calc-frac-format '(":" nil)))
X (math-format-number (nth 2 a)))
X (math-format-number (nth 3 a))))))
X ((eq (car a) 'intv)
X (concat (if (memq (nth 1 a) '(0 1)) "(" "[")
X (math-format-number (nth 2 a))
X " .. "
X (math-format-number (nth 3 a))
X (if (memq (nth 1 a) '(0 2)) ")" "]")))
X ((eq (car a) 'sdev)
X (concat (math-format-number (nth 1 a))
X " +/- "
X (math-format-number (nth 2 a))))
X ((eq (car a) 'vec)
X (math-format-flat-expr a 0))
X (t (format "%s" a)))
)
X
(defun math-adjust-fraction (a)
X (if (nth 1 calc-frac-format)
X (progn
X (if (Math-integerp a) (setq a (list 'frac a 1)))
X (let ((g (math-quotient (nth 1 calc-frac-format)
X (math-gcd (nth 2 a)
X (nth 1 calc-frac-format)))))
X (list 'frac (math-mul (nth 1 a) g) (math-mul (nth 2 a) g))))
X a)
)
X
(defun math-format-bignum-fancy (a) ; [X L]
X (let ((str (cond ((= calc-number-radix 10)
X (math-format-bignum-decimal a))
X ((= calc-number-radix 2)
X (math-format-bignum-binary a))
X ((= calc-number-radix 8)
X (math-format-bignum-octal a))
X ((= calc-number-radix 16)
X (math-format-bignum-hex a))
X (t (math-format-bignum-radix a)))))
X (if calc-leading-zeros
X (let* ((calc-internal-prec 6)
X (digs (math-compute-max-digits (math-abs calc-word-size)
X calc-number-radix))
X (len (length str)))
X (if (< len digs)
X (setq str (concat (make-string (- digs len) ?0) str)))))
X (if calc-group-digits
X (let ((i (length str))
X (g (if (integerp calc-group-digits)
X (math-abs calc-group-digits)
X (if (memq calc-number-radix '(2 16)) 4 3))))
X (while (> i g)
X (setq i (- i g)
X str (concat (substring str 0 i)
X calc-group-char
X (substring str i))))
X str))
X (if (and (/= calc-number-radix 10)
X math-radix-explicit-format)
X (if calc-radix-formatter
X (funcall calc-radix-formatter calc-number-radix str)
X (format "%d#%s" calc-number-radix str))
X str))
)
X
X
(defun math-group-float (str) ; [X X]
X (let* ((pt (or (string-match "[^0-9a-zA-Z]" str) (length str)))
X (g (if (integerp calc-group-digits) (math-abs calc-group-digits) 3))
X (i pt))
X (if (and (integerp calc-group-digits) (< calc-group-digits 0))
X (while (< (setq i (+ (1+ i) g)) (length str))
X (setq str (concat (substring str 0 i)
X calc-group-char
X (substring str i))
X i (+ i (1- (length calc-group-char))))))
X (setq i pt)
X (while (> i g)
X (setq i (- i g)
X str (concat (substring str 0 i)
X calc-group-char
X (substring str i))))
X str)
)
X
X
X
X
X
X
X
X
(setq math-compose-level 0)
(setq math-comp-selected nil)
(setq math-comp-tagged nil)
(setq math-comp-sel-hpos nil)
(setq math-comp-sel-vpos nil)
(setq math-comp-sel-cpos nil)
(setq math-compose-hash-args nil)
X
X
;;; Users can redefine this in their .emacs files.
(defvar calc-keypad-user-menu nil
X "If not NIL, this describes an additional menu for calc-keypad.
It should contain a list of three rows.
Each row should be a list of six keys.
Each key should be a list of a label string, plus a Calc command name spec.
A command spec is a command name symbol, a keyboard macro string, a
list containing a numeric entry string, or nil.
A key may contain additional specs for Inverse, Hyperbolic, and Inv+Hyp.")
X
X
X
X
X
(run-hooks 'calc-ext-load-hook)
X
X
SHAR_EOF
echo 'File calc-ext.el is complete' &&
chmod 0644 calc-ext.el ||
echo 'restore of calc-ext.el failed'
Wc_c="`wc -c < 'calc-ext.el'`"
test 118802 -eq "$Wc_c" ||
echo 'calc-ext.el: original size 118802, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= calc-fin.el ==============
if test -f 'calc-fin.el' -a X"$1" != X"-c"; then
echo 'x - skipping calc-fin.el (File already exists)'
rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting calc-fin.el (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'calc-fin.el' &&
;; Calculator for GNU Emacs, part II [calc-fin.el]
;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
X
;; This file is part of GNU Emacs.
X
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY. No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing. Refer to the GNU Emacs General Public
;; License for full details.
X
;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License. A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities. It should be in a
;; file named COPYING. Among other things, the copyright notice
;; and this notice must be preserved on all copies.
X
X
X
;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)
X
(require 'calc-macs)
X
(defun calc-Need-calc-fin () nil)
X
X
;;; Financial functions.
X
(defun calc-fin-pv ()
X (interactive)
X (calc-slow-wrapper
X (if (calc-is-hyperbolic)
X (calc-enter-result 3 "pvl" (cons 'calcFunc-pvl (calc-top-list-n 3)))
X (if (calc-is-inverse)
X (calc-enter-result 3 "pvb" (cons 'calcFunc-pvb (calc-top-list-n 3)))
X (calc-enter-result 3 "pv" (cons 'calcFunc-pv (calc-top-list-n 3))))))
)
X
(defun calc-fin-npv (arg)
X (interactive "p")
X (calc-slow-wrapper
X (if (calc-is-inverse)
X (calc-vector-op "npvb" 'calcFunc-npvb (1+ arg))
X (calc-vector-op "npv" 'calcFunc-npv (1+ arg))))
)
X
(defun calc-fin-fv ()
X (interactive)
X (calc-slow-wrapper
X (if (calc-is-hyperbolic)
X (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl (calc-top-list-n 3)))
X (if (calc-is-inverse)
X (calc-enter-result 3 "fvb" (cons 'calcFunc-fvb (calc-top-list-n 3)))
X (calc-enter-result 3 "fv" (cons 'calcFunc-fv (calc-top-list-n 3))))))
)
X
(defun calc-fin-pmt ()
X (interactive)
X (calc-slow-wrapper
X (if (calc-is-hyperbolic)
X (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl (calc-top-list-n 3)))
X (if (calc-is-inverse)
X (calc-enter-result 3 "pmtb" (cons 'calcFunc-pmtb (calc-top-list-n 3)))
X (calc-enter-result 3 "pmt" (cons 'calcFunc-pmt (calc-top-list-n 3))))))
)
X
(defun calc-fin-nper ()
X (interactive)
X (calc-slow-wrapper
X (if (calc-is-hyperbolic)
X (calc-enter-result 3 "nprl" (cons 'calcFunc-nperl (calc-top-list-n 3)))
X (if (calc-is-inverse)
X (calc-enter-result 3 "nprb" (cons 'calcFunc-nperb
X (calc-top-list-n 3)))
X (calc-enter-result 3 "nper" (cons 'calcFunc-nper
X (calc-top-list-n 3))))))
)
X
(defun calc-fin-rate ()
X (interactive)
X (calc-slow-wrapper
X (if (calc-is-hyperbolic)
X (calc-enter-result 3 "ratl" (cons 'calcFunc-ratel
X (calc-top-list-n 3)))
X (if (calc-is-inverse)
X (calc-enter-result 3 "ratb" (cons 'calcFunc-rateb
X (calc-top-list-n 3)))
X (calc-enter-result 3 "rate" (cons 'calcFunc-rate
X (calc-top-list-n 3))))))
)
X
(defun calc-fin-irr (arg)
X (interactive "P")
X (calc-slow-wrapper
X (if (calc-is-inverse)
X (calc-vector-op "irrb" 'calcFunc-irrb arg)
X (calc-vector-op "irr" 'calcFunc-irr arg)))
)
X
(defun calc-fin-sln ()
X (interactive)
X (calc-slow-wrapper
X (calc-enter-result 3 "sln" (cons 'calcFunc-sln (calc-top-list-n 3))))
)
X
(defun calc-fin-syd ()
X (interactive)
X (calc-slow-wrapper
X (calc-enter-result 4 "syd" (cons 'calcFunc-syd (calc-top-list-n 4))))
)
X
(defun calc-fin-ddb ()
X (interactive)
X (calc-slow-wrapper
X (calc-enter-result 4 "ddb" (cons 'calcFunc-ddb (calc-top-list-n 4))))
)
X
X
X
X
X
X
;;; Financial functions.
X
(defun calcFunc-pv (rate num amount &optional lump)
X (math-check-financial rate num)
X (math-with-extra-prec 2
X (let ((p (math-pow (math-add 1 rate) num)))
X (math-add (math-mul amount
X (math-div (math-sub 1 (math-div 1 p))
X rate))
X (math-div (or lump 0) p))))
)
(put 'calcFunc-pv 'math-expandable t)
X
(defun calcFunc-pvl (rate num amount)
X (calcFunc-pv rate num 0 amount)
)
(put 'calcFunc-pvl 'math-expandable t)
X
(defun calcFunc-pvb (rate num amount &optional lump)
X (math-check-financial rate num)
X (math-with-extra-prec 2
X (let* ((p (math-pow (math-add 1 rate) num)))
X (math-add (math-mul amount
X (math-div (math-mul (math-sub 1 (math-div 1 p))
X (math-add 1 rate))
X rate))
X (math-div (or lump 0) p))))
)
(put 'calcFunc-pvb 'math-expandable t)
X
(defun calcFunc-npv (rate &rest flows)
X (math-check-financial rate 1)
X (math-with-extra-prec 2
X (let* ((flat (math-flatten-many-vecs flows))
X (pp (math-add 1 rate))
X (p pp)
X (accum 0))
X (while (setq flat (cdr flat))
X (setq accum (math-add accum (math-div (car flat) p))
X p (math-mul p pp)))
X accum))
)
(put 'calcFunc-npv 'math-expandable t)
X
(defun calcFunc-npvb (rate &rest flows)
X (math-check-financial rate 1)
X (math-with-extra-prec 2
X (let* ((flat (math-flatten-many-vecs flows))
X (pp (math-add 1 rate))
X (p 1)
X (accum 0))
X (while (setq flat (cdr flat))
X (setq accum (math-add accum (math-div (car flat) p))
X p (math-mul p pp)))
X accum))
)
(put 'calcFunc-npvb 'math-expandable t)
X
(defun calcFunc-fv (rate num amount &optional initial)
X (math-check-financial rate num)
X (math-with-extra-prec 2
X (let ((p (math-pow (math-add 1 rate) num)))
X (math-add (math-mul amount
X (math-div (math-sub p 1)
X rate))
X (math-mul (or initial 0) p))))
)
(put 'calcFunc-fv 'math-expandable t)
X
(defun calcFunc-fvl (rate num amount)
X (calcFunc-fv rate num 0 amount)
)
(put 'calcFunc-fvl 'math-expandable t)
X
(defun calcFunc-fvb (rate num amount &optional initial)
X (math-check-financial rate num)
X (math-with-extra-prec 2
X (let ((p (math-pow (math-add 1 rate) num)))
X (math-add (math-mul amount
X (math-div (math-mul (math-sub p 1)
X (math-add 1 rate))
X rate))
X (math-mul (or initial 0) p))))
)
(put 'calcFunc-fvb 'math-expandable t)
X
(defun calcFunc-pmt (rate num amount &optional lump)
X (math-check-financial rate num)
X (math-with-extra-prec 2
X (let ((p (math-pow (math-add 1 rate) num)))
X (math-div (math-mul (math-sub amount
X (math-div (or lump 0) p))
X rate)
X (math-sub 1 (math-div 1 p)))))
)
(put 'calcFunc-pmt 'math-expandable t)
X
(defun calcFunc-pmtb (rate num amount &optional lump)
X (math-check-financial rate num)
X (math-with-extra-prec 2
X (let ((p (math-pow (math-add 1 rate) num)))
X (math-div (math-mul (math-sub amount (math-div (or lump 0) p)) rate)
X (math-mul (math-sub 1 (math-div 1 p))
X (math-add 1 rate)))))
)
(put 'calcFunc-pmtb 'math-expandable t)
X
(defun calcFunc-nper (rate pmt amount &optional lump)
X (math-compute-nper rate pmt amount lump nil)
)
(put 'calcFunc-nper 'math-expandable t)
X
(defun calcFunc-nperb (rate pmt amount &optional lump)
X (math-compute-nper rate pmt amount lump 'b)
)
(put 'calcFunc-nperb 'math-expandable t)
X
(defun calcFunc-nperl (rate pmt amount)
X (math-compute-nper rate pmt amount nil 'l)
)
(put 'calcFunc-nperl 'math-expandable t)
X
(defun math-compute-nper (rate pmt amount lump bflag)
X (and lump (math-zerop lump)
X (setq lump nil))
X (and lump (math-zerop pmt)
X (setq amount lump
X lump nil
X bflag 'l))
X (or (math-objectp rate) (and math-expand-formulas (null lump))
X (math-reject-arg rate 'numberp))
X (and (math-zerop rate)
X (math-reject-arg rate 'nonzerop))
X (or (math-objectp pmt) (and math-expand-formulas (null lump))
X (math-reject-arg pmt 'numberp))
X (or (math-objectp amount) (and math-expand-formulas (null lump))
X (math-reject-arg amount 'numberp))
X (if lump
X (progn
X (or (math-objectp lump)
X (math-reject-arg lump 'numberp))
X (let ((root (math-find-root (list 'calcFunc-eq
X (list (if bflag
X 'calcFunc-pvb
X 'calcFunc-pv)
X rate
X '(var DUMMY var-DUMMY)
X pmt
X lump)
X amount)
X '(var DUMMY var-DUMMY)
X '(intv 3 0 100)
X t)))
X (if (math-vectorp root)
X (nth 1 root)
X root)))
X (math-with-extra-prec 2
X (let ((temp (if (eq bflag 'l)
X (math-div amount pmt)
X (math-sub 1 (math-div (math-mul amount rate)
X (if bflag
X (math-mul pmt (math-add 1 rate))
X pmt))))))
X (if (or (math-posp temp) math-expand-formulas)
X (math-neg (calcFunc-log temp (math-add 1 rate)))
X (math-reject-arg pmt "*Payment too small to cover interest rate")))))
)
X
(defun calcFunc-rate (num pmt amount &optional lump)
X (math-compute-rate num pmt amount lump 'calcFunc-pv)
)
X
(defun calcFunc-rateb (num pmt amount &optional lump)
X (math-compute-rate num pmt amount lump 'calcFunc-pvb)
)
X
(defun math-compute-rate (num pmt amount lump func)
X (or (math-objectp num)
X (math-reject-arg num 'numberp))
X (or (math-objectp pmt)
X (math-reject-arg pmt 'numberp))
X (or (math-objectp amount)
X (math-reject-arg amount 'numberp))
X (or (null lump)
X (math-objectp lump)
X (math-reject-arg lump 'numberp))
X (let ((root (math-find-root (list 'calcFunc-eq
X (list func
X '(var DUMMY var-DUMMY)
X num
X pmt
X (or lump 0))
X amount)
X '(var DUMMY var-DUMMY)
X '(intv 3 (float 1 -4) 1)
X t)))
X (if (math-vectorp root)
X (nth 1 root)
X root))
)
X
(defun calcFunc-ratel (num pmt amount)
X (or (math-objectp num) math-expand-formulas
X (math-reject-arg num 'numberp))
X (or (math-objectp pmt) math-expand-formulas
X (math-reject-arg pmt 'numberp))
X (or (math-objectp amount) math-expand-formulas
X (math-reject-arg amount 'numberp))
X (math-with-extra-prec 2
X (math-sub (math-pow (math-div pmt amount) (math-div 1 num)) 1))
)
X
(defun calcFunc-irr (&rest vecs)
X (math-compute-irr vecs 'calcFunc-npv)
)
X
(defun calcFunc-irrb (&rest vecs)
X (math-compute-irr vecs 'calcFunc-npvb)
)
X
(defun math-compute-irr (vecs func)
X (let* ((flat (math-flatten-many-vecs vecs))
X (root (math-find-root (list func
X '(var DUMMY var-DUMMY)
X flat)
X '(var DUMMY var-DUMMY)
X '(intv 3 '(float 1 -4) 1)
X t)))
X (if (math-vectorp root)
X (nth 1 root)
X root))
)
X
(defun math-check-financial (rate num)
X (or (math-objectp rate) math-expand-formulas
X (math-reject-arg rate 'numberp))
X (and (math-zerop rate)
X (math-reject-arg rate 'nonzerop))
X (or (math-objectp num) math-expand-formulas
X (math-reject-arg num 'numberp))
)
X
X
(defun calcFunc-sln (cost salvage life &optional period)
X (or (math-realp cost) math-expand-formulas
X (math-reject-arg cost 'realp))
X (or (math-realp salvage) math-expand-formulas
X (math-reject-arg salvage 'realp))
X (or (math-realp life) math-expand-formulas
X (math-reject-arg life 'realp))
X (if (math-zerop life) (math-reject-arg life 'nonzerop))
X (if (and period
X (if (math-num-integerp period)
X (or (Math-lessp life period) (not (math-posp period)))
X (math-reject-arg period 'integerp)))
X 0
X (math-div (math-sub cost salvage) life))
)
(put 'calcFunc-sln 'math-expandable t)
X
(defun calcFunc-syd (cost salvage life period)
X (or (math-realp cost) math-expand-formulas
X (math-reject-arg cost 'realp))
X (or (math-realp salvage) math-expand-formulas
X (math-reject-arg salvage 'realp))
X (or (math-realp life) math-expand-formulas
X (math-reject-arg life 'realp))
X (if (math-zerop life) (math-reject-arg life 'nonzerop))
X (or (math-realp period) math-expand-formulas
X (math-reject-arg period 'realp))
X (if (or (Math-lessp life period) (not (math-posp period)))
X 0
X (math-div (math-mul (math-sub cost salvage)
X (math-add (math-sub life period) 1))
X (math-div (math-mul life (math-add life 1)) 2)))
)
(put 'calcFunc-syd 'math-expandable t)
X
(defun calcFunc-ddb (cost salvage life period)
X (if (math-messy-integerp period) (setq period (math-trunc period)))
X (or (integerp period) (math-reject-arg period 'fixnump))
X (or (math-realp cost) (math-reject-arg cost 'realp))
X (or (math-realp salvage) (math-reject-arg salvage 'realp))
X (or (math-realp life) (math-reject-arg life 'realp))
X (if (math-zerop life) (math-reject-arg life 'nonzerop))
X (if (or (Math-lessp life period) (<= period 0))
X 0
X (let ((book cost)
X (res 0))
X (while (>= (setq period (1- period)) 0)
X (setq res (math-div (math-mul book 2) life)
X book (math-sub book res))
X (if (Math-lessp book salvage)
X (setq res (math-add res (math-sub book salvage))
X book salvage)))
X res))
)
X
X
X
SHAR_EOF
chmod 0644 calc-fin.el ||
echo 'restore of calc-fin.el failed'
Wc_c="`wc -c < 'calc-fin.el'`"
test 12610 -eq "$Wc_c" ||
echo 'calc-fin.el: original size 12610, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= calc-forms.el ==============
if test -f 'calc-forms.el' -a X"$1" != X"-c"; then
echo 'x - skipping calc-forms.el (File already exists)'
rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting calc-forms.el (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'calc-forms.el' &&
;; Calculator for GNU Emacs, part II [calc-forms.el]
;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
X
;; This file is part of GNU Emacs.
X
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY. No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing. Refer to the GNU Emacs General Public
;; License for full details.
X
;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License. A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities. It should be in a
;; file named COPYING. Among other things, the copyright notice
;; and this notice must be preserved on all copies.
X
X
X
;; This file is autoloaded from calc-ext.el.
(require 'calc-ext)
X
(require 'calc-macs)
X
(defun calc-Need-calc-forms () nil)
X
X
(defun calc-time ()
X (interactive)
X (calc-wrapper
X (let ((time (current-time-string)))
X (calc-enter-result 0 "time"
X (list 'mod
X (list 'hms
X (string-to-int (substring time 11 13))
X (string-to-int (substring time 14 16))
X (string-to-int (substring time 17 19)))
X (list 'hms 24 0 0)))))
)
X
X
X
X
(defun calc-to-hms (arg)
X (interactive "P")
X (calc-wrapper
X (if (calc-is-inverse)
X (if (eq calc-angle-mode 'rad)
X (calc-unary-op ">rad" 'calcFunc-rad arg)
X (calc-unary-op ">deg" 'calcFunc-deg arg))
X (calc-unary-op ">hms" 'calcFunc-hms arg)))
)
X
(defun calc-from-hms (arg)
X (interactive "P")
X (calc-invert-func)
X (calc-to-hms arg)
)
X
X
(defun calc-hms-notation (fmt)
X (interactive "sHours-minutes-seconds format (hms, @ ' \", etc.): ")
X (calc-wrapper
X (if (string-match "\\`\\([^,; ]+\\)\\([,; ]*\\)\\([^,; ]\\)\\([,; ]*\\)\\([^,; ]\\)\\'" fmt)
X (progn
X (calc-change-mode 'calc-hms-format
X (concat "%s" (math-match-substring fmt 1)
X (math-match-substring fmt 2)
X "%s" (math-match-substring fmt 3)
X (math-match-substring fmt 4)
X "%s" (math-match-substring fmt 5))
X t)
X (setq-default calc-hms-format calc-hms-format)) ; for minibuffer
X (error "Bad hours-minutes-seconds format.")))
)
X
(defun calc-date-notation (fmt arg)
X (interactive "sDate format (e.g., M/D/YY h:mm:ss): \nP")
X (calc-wrapper
X (if (equal fmt "")
X (setq fmt "1"))
X (if (string-match "\\` *[0-9] *\\'" fmt)
X (setq fmt (nth (string-to-int fmt) calc-standard-date-formats)))
X (or (string-match "[a-zA-Z]" fmt)
X (error "Bad date format specifier"))
X (and arg
X (>= (setq arg (prefix-numeric-value arg)) 0)
X (<= arg 9)
X (setcar (nthcdr arg calc-standard-date-formats) fmt))
X (let ((case-fold-search nil))
X (and (not (string-match "<.*>" fmt))
X (string-match "\\`[^hHspP]*\\([^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*[bBhHmpPsS]+[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*\\)[^hHspP]*\\'" fmt)
X (string-match (concat "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*"
X (regexp-quote (math-match-substring fmt 1))
X "[^ac-gi-lnoqrt-zAC-GI-OQRT-Z]*") fmt)
X (setq fmt (concat (substring fmt 0 (match-beginning 0))
X "<"
X (substring fmt (match-beginning 0) (match-end 0))
X ">"
X (substring fmt (match-end 0))))))
X (let ((lfmt nil)
X (fullfmt nil)
X (time nil)
X pos pos2 sym temp)
X (let ((case-fold-search nil))
X (and (setq temp (string-match ":[BS]S" fmt))
X (aset fmt temp ?C)))
X (while (setq pos (string-match "[<>a-zA-Z]" fmt))
X (if (> pos 0)
X (setq lfmt (cons (substring fmt 0 pos) lfmt)))
X (setq pos2 (1+ pos))
X (cond ((= (aref fmt pos) ?\<)
X (and time (error "Nested <'s not allowed"))
X (and lfmt (setq fullfmt (nconc lfmt fullfmt)
X lfmt nil))
X (setq time t))
X ((= (aref fmt pos) ?\>)
X (or time (error "Misplaced > in format"))
X (and lfmt (setq fullfmt (cons (nreverse lfmt) fullfmt)
X lfmt nil))
X (setq time nil))
X (t
X (if (string-match "\\`[^a-zA-Z]*[bB][a-zA-Z]" fmt)
X (setq pos2 (1+ pos2)))
X (while (and (< pos2 (length fmt))
X (= (upcase (aref fmt pos2))
X (upcase (aref fmt (1- pos2)))))
X (setq pos2 (1+ pos2)))
X (setq sym (intern (substring fmt pos pos2)))
X (or (memq sym '(Y YY BY YYY YYYY
X aa AA aaa AAA aaaa AAAA
X bb BB bbb BBB bbbb BBBB
X M MM BM Mmm Mmmm MMM MMMM
X D DD BD d ddd bdd
X W Www Wwww WWW WWWW
X h hh bh H HH BH
X p P pp PP pppp PPPP
X m mm bm s ss bss SS BS C
X N n J j U b))
X (and (eq sym 'X) (not lfmt) (not fullfmt))
X (error "Bad format code: %s" sym))
X (and (memq sym '(bb BB bbb BBB bbbb BBBB))
X (setq lfmt (cons 'b lfmt)))
X (setq lfmt (cons sym lfmt))))
X (setq fmt (substring fmt pos2)))
X (or (equal fmt "")
X (setq lfmt (cons fmt lfmt)))
X (and lfmt (if time
X (setq fullfmt (cons (nreverse lfmt) fullfmt))
X (setq fullfmt (nconc lfmt fullfmt))))
X (calc-change-mode 'calc-date-format (nreverse fullfmt) t)))
)
X
X
(defun calc-hms-mode ()
X (interactive)
X (calc-wrapper
X (calc-change-mode 'calc-angle-mode 'hms)
X (message "Angles measured in degrees-minutes-seconds."))
)
X
X
(defun calc-now (arg)
X (interactive "P")
X (calc-date-zero-args "now" 'calcFunc-now arg)
)
X
(defun calc-date-part (arg)
X (interactive "NPart code (1-9 = Y,M,D,H,M,S,Wd,Yd,Hms): ")
X (if (or (< arg 1) (> arg 9))
X (error "Part code out of range"))
X (calc-wrapper
X (calc-enter-result 1
X (nth arg '(nil "year" "mnth" "day" "hour" "minu"
X "sec" "wday" "yday" "hmst"))
X (list (nth arg '(nil calcFunc-year calcFunc-month
X calcFunc-day calcFunc-hour
X calcFunc-minute calcFunc-second
X calcFunc-weekday calcFunc-yearday
X calcFunc-time))
X (calc-top-n 1))))
)
X
(defun calc-date (arg)
X (interactive "p")
X (if (or (< arg 1) (> arg 6))
X (error "Between one and six arguments are allowed"))
X (calc-wrapper
X (calc-enter-result arg "date" (cons 'calcFunc-date (calc-top-list-n arg))))
)
X
(defun calc-julian (arg)
X (interactive "P")
X (calc-date-one-arg "juln" 'calcFunc-julian arg)
)
X
(defun calc-unix-time (arg)
X (interactive "P")
X (calc-date-one-arg "unix" 'calcFunc-unixtime arg)
)
X
(defun calc-time-zone (arg)
X (interactive "P")
X (calc-date-zero-args "zone" 'calcFunc-tzone arg)
)
X
(defun calc-convert-time-zones (old &optional new)
X (interactive "sFrom time zone: ")
X (calc-wrapper
X (if (equal old "$")
X (calc-enter-result 3 "tzcv" (cons 'calcFunc-tzconv (calc-top-list-n 3)))
X (if (equal old "") (setq old "local"))
X (or new
X (setq new (read-string (concat "From time zone: " old
X ", to zone: "))))
X (if (stringp old) (setq old (math-read-expr old)))
X (if (eq (car-safe old) 'error)
X (error "Error in expression: " (nth 1 old)))
X (if (equal new "") (setq new "local"))
X (if (stringp new) (setq new (math-read-expr new)))
X (if (eq (car-safe new) 'error)
X (error "Error in expression: " (nth 1 new)))
X (calc-enter-result 1 "tzcv" (list 'calcFunc-tzconv
X (calc-top-n 1) old new))))
)
X
(defun calc-new-week (arg)
X (interactive "P")
X (calc-date-one-arg "nwwk" 'calcFunc-newweek arg)
)
X
(defun calc-new-month (arg)
X (interactive "P")
X (calc-date-one-arg "nwmn" 'calcFunc-newmonth arg)
)
X
(defun calc-new-year (arg)
X (interactive "P")
X (calc-date-one-arg "nwyr" 'calcFunc-newyear arg)
)
X
(defun calc-inc-month (arg)
X (interactive "p")
X (calc-date-one-arg "incm" 'calcFunc-incmonth arg)
)
X
(defun calc-date-zero-args (prefix func arg)
X (calc-wrapper
X (if (consp arg)
X (calc-enter-result 1 prefix (list func (calc-top-n 1)))
X (calc-enter-result 0 prefix (if arg
X (list func (prefix-numeric-value arg))
X (list func)))))
)
X
(defun calc-date-one-arg (prefix func arg)
X (calc-wrapper
X (if (consp arg)
X (calc-enter-result 2 prefix (cons func (calc-top-list-n 2)))
X (calc-enter-result 1 prefix (if arg
X (list func (calc-top-n 1)
X (prefix-numeric-value arg))
X (list func (calc-top-n 1))))))
)
X
X
X
X
X
X
X
X
;;;; Hours-minutes-seconds forms.
X
(defun math-normalize-hms (a)
X (let ((h (math-normalize (nth 1 a)))
X (m (math-normalize (nth 2 a)))
X (s (let ((calc-internal-prec (max (- calc-internal-prec 4) 3)))
X (math-normalize (nth 3 a)))))
X (if (math-negp h)
X (progn
X (if (math-posp s)
X (setq s (math-add s -60)
X m (math-add m 1)))
X (if (math-posp m)
X (setq m (math-add m -60)
X h (math-add h 1)))
X (if (not (Math-lessp -60 s))
X (setq s (math-add s 60)
X m (math-add m -1)))
X (if (not (Math-lessp -60 m))
X (setq m (math-add m 60)
X h (math-add h -1))))
X (if (math-negp s)
X (setq s (math-add s 60)
X m (math-add m -1)))
X (if (math-negp m)
X (setq m (math-add m 60)
X h (math-add h -1)))
X (if (not (Math-lessp s 60))
X (setq s (math-add s -60)
X m (math-add m 1)))
X (if (not (Math-lessp m 60))
X (setq m (math-add m -60)
X h (math-add h 1))))
X (if (and (eq (car-safe s) 'float)
X (<= (+ (math-numdigs (nth 1 s)) (nth 2 s))
X (- 2 calc-internal-prec)))
X (setq s 0))
X (list 'hms h m s))
)
X
;;; Convert A from ANG or current angular mode to HMS format.
(defun math-to-hms (a &optional ang) ; [X R] [Public]
X (cond ((eq (car-safe a) 'hms) a)
X ((eq (car-safe a) 'sdev)
X (math-make-sdev (math-to-hms (nth 1 a))
X (math-to-hms (nth 2 a))))
X ((not (Math-numberp a))
X (list 'calcFunc-hms a))
X ((math-negp a)
X (math-neg (math-to-hms (math-neg a) ang)))
X ((eq (or ang calc-angle-mode) 'rad)
X (math-to-hms (math-div a (math-pi-over-180)) 'deg))
X ((memq (car-safe a) '(cplx polar)) a)
X (t
X ;(setq a (let ((calc-internal-prec (max (1- calc-internal-prec) 3)))
X ; (math-normalize a)))
X (math-normalize
X (let* ((b (math-mul a 3600))
X (hm (math-trunc (math-div b 60)))
X (hmd (math-idivmod hm 60)))
X (list 'hms
X (car hmd)
X (cdr hmd)
X (math-sub b (math-mul hm 60)))))))
)
(defun calcFunc-hms (h &optional m s)
X (or (Math-realp h) (math-reject-arg h 'realp))
X (or m (setq m 0))
X (or (Math-realp m) (math-reject-arg m 'realp))
X (or s (setq s 0))
X (or (Math-realp s) (math-reject-arg s 'realp))
X (if (and (not (Math-lessp m 0)) (Math-lessp m 60)
X (not (Math-lessp s 0)) (Math-lessp s 60))
X (math-add (math-to-hms h)
X (list 'hms 0 m s))
X (math-to-hms (math-add h
X (math-add (math-div (or m 0) 60)
X (math-div (or s 0) 3600)))
X 'deg))
)
X
;;; Convert A from HMS format to ANG or current angular mode.
(defun math-from-hms (a &optional ang) ; [R X] [Public]
X (cond ((not (eq (car-safe a) 'hms))
X (if (Math-numberp a)
X a
X (if (eq (car-safe a) 'sdev)
X (math-make-sdev (math-from-hms (nth 1 a) ang)
X (math-from-hms (nth 2 a) ang))
X (if (eq (or ang calc-angle-mode) 'rad)
X (list 'calcFunc-rad a)
X (list 'calcFunc-deg a)))))
X ((math-negp a)
X (math-neg (math-from-hms (math-neg a) ang)))
X ((eq (or ang calc-angle-mode) 'rad)
X (math-mul (math-from-hms a 'deg) (math-pi-over-180)))
X (t
X (math-add (math-div (math-add (math-div (nth 3 a)
X '(float 6 1))
X (nth 2 a))
X 60)
X (nth 1 a))))
)
X
X
X
;;;; Date forms.
X
X
;;; Some of these functions are adapted from Edward Reingold's "calendar.el".
;;; These versions are rewritten to use arbitrary-size integers.
;;; The Julian calendar is used up to 9/2/1752, after which the Gregorian
;;; calendar is used; the first day after 9/2/1752 is 9/14/1752.
X
;;; A numerical date is the number of days since midnight on
;;; the morning of January 1, 1 A.D. If the date is a non-integer,
;;; it represents a specific date and time.
;;; A "dt" is a list of the form, (year month day), corresponding to
;;; an integer code, or (year month day hour minute second), corresponding
;;; to a non-integer code.
X
(defun math-date-to-dt (value)
X (if (eq (car-safe value) 'date)
X (setq value (nth 1 value)))
X (or (math-realp value)
X (math-reject-arg value 'datep))
X (let* ((parts (math-date-parts value))
X (date (car parts))
X (time (nth 1 parts))
X (month 1)
X day
X (year (math-quotient (math-add date (if (Math-lessp date 711859)
X 365 ; for speed, we take
X -108)) ; >1950 as a special case
X (if (math-negp value) 366 365)))
X ; this result may be an overestimate
X temp)
X (while (Math-lessp date (setq temp (math-absolute-from-date year 1 1)))
X (setq year (math-add year -1)))
X (if (eq year 0) (setq year -1))
X (setq date (1+ (math-sub date temp)))
X (and (eq year 1752) (>= date 247)
X (setq date (+ date 11)))
X (setq temp (if (math-leap-year-p year)
X [1 32 61 92 122 153 183 214 245 275 306 336 999]
X [1 32 60 91 121 152 182 213 244 274 305 335 999]))
X (while (>= date (aref temp month))
X (setq month (1+ month)))
X (setq day (1+ (- date (aref temp (1- month)))))
X (if (math-integerp value)
X (list year month day)
X (list year month day
X (/ time 3600)
X (% (/ time 60) 60)
X (math-add (% time 60) (nth 2 parts)))))
)
X
(defun math-dt-to-date (dt)
X (or (integerp (nth 1 dt))
X (math-reject-arg (nth 1 dt) 'fixnump))
X (if (or (< (nth 1 dt) 1) (> (nth 1 dt) 12))
X (math-reject-arg (nth 1 dt) "Month value is out of range"))
X (or (integerp (nth 2 dt))
X (math-reject-arg (nth 2 dt) 'fixnump))
X (if (or (< (nth 2 dt) 1) (> (nth 2 dt) 31))
X (math-reject-arg (nth 2 dt) "Day value is out of range"))
X (let ((date (math-absolute-from-date (car dt) (nth 1 dt) (nth 2 dt))))
X (if (nth 3 dt)
X (math-add (math-float date)
X (math-div (math-add (+ (* (nth 3 dt) 3600)
X (* (nth 4 dt) 60))
X (nth 5 dt))
X '(float 864 2)))
X date))
)
X
(defun math-date-parts (value &optional offset)
X (let* ((date (math-floor value))
X (time (math-round (math-mul (math-sub value (or offset date)) 86400)
X (and (> calc-internal-prec 12)
X (- calc-internal-prec 12))))
X (ftime (math-floor time)))
X (list date
X ftime
X (math-sub time ftime)))
)
X
X
(defun math-this-year ()
X (string-to-int (substring (current-time-string) -4))
)
X
(defun math-leap-year-p (year)
X (if (Math-lessp year 1752)
X (if (math-negp year)
X (= (math-imod (math-neg year) 4) 1)
X (= (math-imod year 4) 0))
X (setq year (math-imod year 400))
X (or (and (= (% year 4) 0) (/= (% year 100) 0))
X (= year 0)))
)
X
(defun math-days-in-month (year month)
X (if (and (= month 2) (math-leap-year-p year))
X 29
X (aref [31 28 31 30 31 30 31 31 30 31 30 31] (1- month)))
)
X
(defun math-day-number (year month day)
X (let ((day-of-year (+ day (* 31 (1- month)))))
X (if (> month 2)
X (progn
X (setq day-of-year (- day-of-year (/ (+ 23 (* 4 month)) 10)))
X (if (math-leap-year-p year)
X (setq day-of-year (1+ day-of-year)))))
X (and (eq year 1752)
X (or (> month 9)
X (and (= month 9) (>= day 14)))
X (setq day-of-year (- day-of-year 11)))
X day-of-year)
)
X
(defun math-absolute-from-date (year month day)
X (if (eq year 0) (setq year -1))
X (let ((yearm1 (math-sub year 1)))
X (math-sub (math-add (math-day-number year month day)
X (math-add (math-mul 365 yearm1)
X (if (math-posp year)
X (math-quotient yearm1 4)
X (math-sub 365
X (math-quotient (math-sub 3 year)
X 4)))))
X (if (or (Math-lessp year 1753)
X (and (eq year 1752) (<= month 9)))
X 1
X (let ((correction (math-mul (math-quotient yearm1 100) 3)))
X (let ((res (math-idivmod correction 4)))
X (math-add (if (= (cdr res) 0)
X -1
X 0)
X (car res)))))))
)
X
X
;;; It is safe to redefine these in your .emacs file to use a different
;;; language.
X
(defvar math-long-weekday-names '( "Sunday" "Monday" "Tuesday" "Wednesday"
X "Thursday" "Friday" "Saturday" ))
(defvar math-short-weekday-names '( "Sun" "Mon" "Tue" "Wed"
X "Thu" "Fri" "Sat" ))
X
(defvar math-long-month-names '( "January" "February" "March" "April"
X "May" "June" "July" "August"
X "September" "October" "November" "December" ))
(defvar math-short-month-names '( "Jan" "Feb" "Mar" "Apr" "May" "Jun"
X "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ))
X
X
(defun math-format-date (date)
X (if (eq (car-safe date) 'date)
X (setq date (nth 1 date)))
X (let ((entry (list date calc-internal-prec calc-date-format)))
X (or (cdr (assoc entry math-format-date-cache))
X (let* ((dt nil)
X (calc-group-digits nil)
X (calc-leading-zeros nil)
X (calc-number-radix 10)
X year month day weekday hour minute second
X (bc-flag nil)
X (fmt (apply 'concat (mapcar 'math-format-date-part
X calc-date-format))))
X (setq math-format-date-cache (cons (cons entry fmt)
X math-format-date-cache))
X (and (setq dt (nthcdr 10 math-format-date-cache))
X (setcdr dt nil))
X fmt)))
)
(setq math-format-date-cache nil)
X
(defun math-format-date-part (x)
X (cond ((stringp x)
X x)
X ((listp x)
X (if (math-integerp date)
X ""
X (apply 'concat (mapcar 'math-format-date-part x))))
X ((eq x 'X)
X "")
X ((eq x 'N)
X (math-format-number date))
X ((eq x 'n)
X (math-format-number (math-floor date)))
X ((eq x 'J)
X (math-format-number (math-add date '(float (bigpos 235 214 17) -1))))
X ((eq x 'j)
X (math-format-number (math-add (math-floor date) '(bigpos 424 721 1))))
X ((eq x 'U)
X (math-format-number (nth 1 (math-date-parts date 719164))))
X ((progn
X (or dt
X (progn
X (setq dt (math-date-to-dt date)
X year (car dt)
X month (nth 1 dt)
X day (nth 2 dt)
X weekday (math-mod (math-add (math-floor date) 6) 7)
X hour (nth 3 dt)
X minute (nth 4 dt)
X second (nth 5 dt))
X (and (memq 'b calc-date-format)
X (math-negp year)
X (setq year (math-neg year)
X bc-flag t))))
X (memq x '(Y YY BY)))
X (if (and (integerp year) (> year 1940) (< year 2040))
X (format (cond ((eq x 'YY) "%02d")
X ((eq x 'BYY) "%2d")
X (t "%d"))
X (% year 100))
X (if (and (natnump year) (< year 100))
X (format "+%d" year)
X (math-format-number year))))
X ((eq x 'YYY)
X (math-format-number year))
X ((eq x 'YYYY)
X (if (and (natnump year) (< year 100))
X (format "+%d" year)
X (math-format-number year)))
X ((eq x 'b) "")
X ((eq x 'aa)
X (and (not bc-flag) "ad"))
X ((eq x 'AA)
X (and (not bc-flag) "AD"))
X ((eq x 'aaa)
X (and (not bc-flag) "ad "))
X ((eq x 'AAA)
X (and (not bc-flag) "AD "))
X ((eq x 'aaaa)
X (and (not bc-flag) "a.d."))
X ((eq x 'AAAA)
X (and (not bc-flag) "A.D."))
X ((eq x 'bb)
X (and bc-flag "bc"))
X ((eq x 'BB)
X (and bc-flag "BC"))
X ((eq x 'bbb)
X (and bc-flag " bc"))
X ((eq x 'BBB)
X (and bc-flag " BC"))
X ((eq x 'bbbb)
X (and bc-flag "b.c."))
X ((eq x 'BBBB)
X (and bc-flag "B.C."))
X ((eq x 'M)
X (format "%d" month))
X ((eq x 'MM)
X (format "%02d" month))
X ((eq x 'BM)
X (format "%2d" month))
X ((eq x 'Mmm)
X (nth (1- month) math-short-month-names))
X ((eq x 'MMM)
X (upcase (nth (1- month) math-short-month-names)))
X ((eq x 'Mmmm)
X (nth (1- month) math-long-month-names))
X ((eq x 'MMMM)
X (upcase (nth (1- month) math-long-month-names)))
X ((eq x 'D)
X (format "%d" day))
X ((eq x 'DD)
X (format "%02d" day))
X ((eq x 'BD)
X (format "%2d" day))
X ((eq x 'W)
X (format "%d" weekday))
X ((eq x 'Www)
X (nth weekday math-short-weekday-names))
X ((eq x 'WWW)
X (upcase (nth weekday math-short-weekday-names)))
X ((eq x 'Wwww)
X (nth weekday math-long-weekday-names))
X ((eq x 'WWWW)
X (upcase (nth weekday math-long-weekday-names)))
X ((eq x 'd)
X (format "%d" (math-day-number year month day)))
X ((eq x 'ddd)
X (format "%03d" (math-day-number year month day)))
X ((eq x 'bdd)
X (format "%3d" (math-day-number year month day)))
X ((eq x 'h)
X (and hour (format "%d" hour)))
X ((eq x 'hh)
X (and hour (format "%02d" hour)))
X ((eq x 'bh)
X (and hour (format "%2d" hour)))
X ((eq x 'H)
X (and hour (format "%d" (1+ (% (+ hour 11) 12)))))
X ((eq x 'HH)
X (and hour (format "%02d" (1+ (% (+ hour 11) 12)))))
X ((eq x 'BH)
X (and hour (format "%2d" (1+ (% (+ hour 11) 12)))))
X ((eq x 'p)
X (and hour (if (< hour 12) "a" "p")))
X ((eq x 'P)
X (and hour (if (< hour 12) "A" "P")))
X ((eq x 'pp)
X (and hour (if (< hour 12) "am" "pm")))
X ((eq x 'PP)
X (and hour (if (< hour 12) "AM" "PM")))
X ((eq x 'pppp)
X (and hour (if (< hour 12) "a.m." "p.m.")))
X ((eq x 'PPPP)
X (and hour (if (< hour 12) "A.M." "P.M.")))
X ((eq x 'm)
X (and minute (format "%d" minute)))
X ((eq x 'mm)
X (and minute (format "%02d" minute)))
X ((eq x 'bm)
X (and minute (format "%2d" minute)))
X ((eq x 'C)
X (and second (not (math-zerop second))
X ":"))
X ((memq x '(s ss bs SS BS))
X (and second
X (not (and (memq x '(SS BS)) (math-zerop second)))
X (if (integerp second)
X (format (cond ((memq x '(ss SS)) "%02d")
X ((memq x '(bs BS)) "%2d")
X (t "%d"))
X second)
X (concat (if (Math-lessp second 10)
X (cond ((memq x '(ss SS)) "0")
X ((memq x '(bs BS)) " ")
X (t ""))
X "")
X (let ((calc-float-format
X (list 'fix (min (- 12 calc-internal-prec)
X 0))))
X (math-format-number second)))))))
)
X
X
(defun math-parse-date (str)
X (catch 'syntax
X (or (math-parse-standard-date str t)
X (math-parse-standard-date str nil)
X (and (string-match "\\`[^-+/0-9a-zA-Z]*\\([-+]?[0-9]+\\.?[0-9]*\\([eE][-+]?[0-9]+\\)?\\)[^-+/0-9a-zA-Z]*\\'" str)
X (list 'date (math-read-number (math-match-substring str 1))))
X (let ((case-fold-search t)
X (year nil) (month nil) (day nil) (weekday nil)
X (hour nil) (minute nil) (second nil) (bc-flag nil)
X (a nil) (b nil) (c nil) (bigyear nil) temp)
X
X ;; Extract the time, if any.
X (if (or (string-match "\\([0-9][0-9]?\\):\\([0-9][0-9]?\\)\\(:\\([0-9][0-9]?\\(\\.[0-9]+\\)?\\)\\)? *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)?" str)
X (string-match "\\([0-9][0-9]?\\)\\(\\)\\(\\(\\(\\)\\)\\) *\\([ap]m?\\|[ap]\\. *m\\.\\|noon\\|n\\>\\|midnight\\|mid\\>\\|m\\>\\)" str))
X (let ((ampm (math-match-substring str 6)))
X (setq hour (string-to-int (math-match-substring str 1))
X minute (math-match-substring str 2)
X second (math-match-substring str 4)
X str (concat (substring str 0 (match-beginning 0))
X (substring str (match-end 0))))
X (if (equal minute "")
X (setq minute 0)
X (setq minute (string-to-int minute)))
X (if (equal second "")
X (setq second 0)
X (setq second (math-read-number second)))
X (if (equal ampm "")
X (if (> hour 23)
X (throw 'syntax "Hour value out of range"))
X (setq ampm (upcase (aref ampm 0)))
X (if (memq ampm '(?N ?M))
X (if (and (= hour 12) (= minute 0) (eq second 0))
X (if (eq ampm ?M) (setq hour 0))
X (throw 'syntax
X "Time must be 12:00:00 in this context"))
X (if (or (= hour 0) (> hour 12))
X (throw 'syntax "Hour value out of range"))
X (if (eq (= ampm ?A) (= hour 12))
X (setq hour (% (+ hour 12) 24)))))))
X
X ;; Rewrite xx-yy-zz to xx/yy/zz to avoid seeing "-" as a minus sign.
X (while (string-match "[0-9a-zA-Z]\\(-\\)[0-9a-zA-Z]" str)
X (progn
X (setq str (copy-sequence str))
X (aset str (match-beginning 1) ?\/)))
X
X ;; Extract obvious month or weekday names.
X (if (string-match "[a-zA-Z]" str)
X (progn
X (setq month (math-parse-date-word math-long-month-names))
X (setq weekday (math-parse-date-word math-long-weekday-names))
X (or month (setq month
X (math-parse-date-word math-short-month-names)))
X (or weekday (math-parse-date-word math-short-weekday-names))
X (or hour
X (if (setq temp (math-parse-date-word
X '( "noon" "midnight" "mid" )))
X (setq hour (if (= temp 1) 12 0) minute 0 second 0)))
X (or (math-parse-date-word '( "ad" "a.d." ))
X (if (math-parse-date-word '( "bc" "b.c." ))
X (setq bc-flag t)))
X (if (string-match "[a-zA-Z]+" str)
X (throw 'syntax (format "Bad word in date: \"%s\""
X (math-match-substring str 0))))))
X
X ;; If there is a huge number other than the year, ignore it.
X (while (and (string-match "[-+]?0*[1-9][0-9][0-9][0-9][0-9]+" str)
X (setq temp (concat (substring str 0 (match-beginning 0))
X (substring str (match-end 0))))
X (string-match "[4-9][0-9]\\|[0-9][0-9][0-9]\\|[-+][0-9]+[^-]*\\'" temp))
X (setq str temp))
X
X ;; If there is a number with a sign or a large number, it is a year.
X (if (or (string-match "\\([-+][0-9]+\\)[^-]*\\'" str)
X (string-match "\\(0*[1-9][0-9][0-9]+\\)" str))
X (setq year (math-match-substring str 1)
X str (concat (substring str 0 (match-beginning 1))
X (substring str (match-end 1)))
X year (math-read-number year)
X bigyear t))
X
X ;; Collect remaining numbers.
X (setq temp 0)
X (while (string-match "[0-9]+" str temp)
X (and c (throw 'syntax "Too many numbers in date"))
X (setq c (string-to-int (math-match-substring str 0)))
X (or b (setq b c c nil))
X (or a (setq a b b nil))
X (setq temp (match-end 0)))
X
X ;; Check that we have the right amount of information.
X (setq temp (+ (if year 1 0) (if month 1 0) (if day 1 0)
X (if a 1 0) (if b 1 0) (if c 1 0)))
X (if (> temp 3)
X (throw 'syntax "Too many numbers in date")
X (if (or (< temp 2) (and year (= temp 2)))
X (throw 'syntax "Not enough numbers in date")
X (if (= temp 2) ; if year omitted, assume current year
X (setq year (math-this-year)))))
X
X ;; A large number must be a year.
X (or year
X (if (and a (or (> a 31) (< a 1)))
X (setq year a a b b c c nil)
X (if (and b (or (> b 31) (< b 1)))
X (setq year b b c c nil)
X (if (and c (or (> c 31) (< c 1)))
X (setq year c c nil)))))
X
X ;; A medium-large number must be a day.
X (if year
X (if (and a (> a 12))
X (setq day a a b b c c nil)
X (if (and b (> b 12))
X (setq day b b c c nil)
X (if (and c (> c 12))
X (setq day c c nil)))))
X
X ;; We may know enough to sort it out now.
X (if (and year day)
X (or month (setq month a))
X (if (and year month)
X (setq day a)
X
X ;; Interpret order of numbers as same as for display format.
X (setq temp calc-date-format)
X (while temp
X (cond ((not (symbolp (car temp))))
X ((memq (car temp) '(Y YY BY YYY YYYY))
X (or year (setq year a a b b c)))
X ((memq (car temp) '(M MM BM Mmm Mmmm MMM MMMM))
X (or month (setq month a a b b c)))
X ((memq (car temp) '(D DD BD))
X (or day (setq day a a b b c))))
X (setq temp (cdr temp)))
X
X ;; If display format was not complete, assume American style.
X (or month (setq month a a b b c))
X (or day (setq day a a b b c))
X (or year (setq year a a b b c))))
X
X (if bc-flag
X (setq year (math-neg (math-abs year))))
X
X (math-parse-date-validate year bigyear month day
X hour minute second))))
)
X
(defun math-parse-date-validate (year bigyear month day hour minute second)
X (and (not bigyear) (natnump year) (< year 100)
X (setq year (+ year (if (< year 40) 2000 1900))))
X (if (eq year 0)
X (throw 'syntax "Year value is out of range"))
X (if (or (< month 1) (> month 12))
X (throw 'syntax "Month value is out of range"))
SHAR_EOF
true || echo 'restore of calc-forms.el failed'
fi
echo 'End of part 15'
echo 'File calc-forms.el is continued in part 16'
echo 16 > _shar_seq_.tmp
exit 0
exit 0 # Just in case...
--
Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
Sterling Software, IMD UUCP: uunet!sparky!kent
Phone: (402) 291-8300 FAX: (402) 291-4362
Please send comp.sources.misc-related mail to kent@uunet.uu.net.