1 ;; Calculator for GNU Emacs, part II [calc-fin.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-fin () nil)
32 ;;; Financial functions.
37 (if (calc-is-hyperbolic)
38 (calc-enter-result 3 "pvl" (cons 'calcFunc-pvl (calc-top-list-n 3)))
40 (calc-enter-result 3 "pvb" (cons 'calcFunc-pvb (calc-top-list-n 3)))
41 (calc-enter-result 3 "pv" (cons 'calcFunc-pv (calc-top-list-n 3))))))
44 (defun calc-fin-npv (arg)
48 (calc-vector-op "npvb" 'calcFunc-npvb (1+ arg))
49 (calc-vector-op "npv" 'calcFunc-npv (1+ arg))))
55 (if (calc-is-hyperbolic)
56 (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl (calc-top-list-n 3)))
58 (calc-enter-result 3 "fvb" (cons 'calcFunc-fvb (calc-top-list-n 3)))
59 (calc-enter-result 3 "fv" (cons 'calcFunc-fv (calc-top-list-n 3))))))
62 (defun calc-fin-pmt ()
65 (if (calc-is-hyperbolic)
66 (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl (calc-top-list-n 3)))
68 (calc-enter-result 3 "pmtb" (cons 'calcFunc-pmtb (calc-top-list-n 3)))
69 (calc-enter-result 3 "pmt" (cons 'calcFunc-pmt (calc-top-list-n 3))))))
72 (defun calc-fin-nper ()
75 (if (calc-is-hyperbolic)
76 (calc-enter-result 3 "nprl" (cons 'calcFunc-nperl (calc-top-list-n 3)))
78 (calc-enter-result 3 "nprb" (cons 'calcFunc-nperb
80 (calc-enter-result 3 "nper" (cons 'calcFunc-nper
81 (calc-top-list-n 3))))))
84 (defun calc-fin-rate ()
87 (calc-pop-push-record 3
88 (if (calc-is-hyperbolic) "ratl"
89 (if (calc-is-inverse) "ratb" "rate"))
92 (cons (if (calc-is-hyperbolic) 'calcFunc-ratel
93 (if (calc-is-hyperbolic) 'calcFunc-rateb
95 (calc-top-list-n 3))))))
98 (defun calc-fin-irr (arg)
101 (if (calc-is-inverse)
102 (calc-vector-op "irrb" 'calcFunc-irrb arg)
103 (calc-vector-op "irr" 'calcFunc-irr arg)))
106 (defun calc-fin-sln ()
109 (calc-enter-result 3 "sln" (cons 'calcFunc-sln (calc-top-list-n 3))))
112 (defun calc-fin-syd ()
115 (calc-enter-result 4 "syd" (cons 'calcFunc-syd (calc-top-list-n 4))))
118 (defun calc-fin-ddb ()
121 (calc-enter-result 4 "ddb" (cons 'calcFunc-ddb (calc-top-list-n 4))))
125 (defun calc-to-percentage (x)
126 (cond ((Math-objectp x)
127 (setq x (math-mul x 100))
128 (if (Math-num-integerp x)
129 (setq x (math-trunc x)))
130 (list 'calcFunc-percent x))
132 (cons 'vec (mapcar 'calc-to-percentage (cdr x))))
136 (defun calc-convert-percent ()
139 (calc-pop-push-record 1 "c%" (calc-to-percentage (calc-top-n 1))))
142 (defun calc-percent-change ()
145 (let ((res (calc-normalize (cons 'calcFunc-relch (calc-top-list 2)))))
146 (calc-pop-push-record 2 "%ch" (calc-to-percentage res))))
153 ;;; Financial functions.
155 (defun calcFunc-pv (rate num amount &optional lump)
156 (math-check-financial rate num)
157 (math-with-extra-prec 2
158 (let ((p (math-pow (math-add 1 rate) num)))
159 (math-add (math-mul amount
160 (math-div (math-sub 1 (math-div 1 p))
162 (math-div (or lump 0) p))))
164 (put 'calcFunc-pv 'math-expandable t)
166 (defun calcFunc-pvl (rate num amount)
167 (calcFunc-pv rate num 0 amount)
169 (put 'calcFunc-pvl 'math-expandable t)
171 (defun calcFunc-pvb (rate num amount &optional lump)
172 (math-check-financial rate num)
173 (math-with-extra-prec 2
174 (let* ((p (math-pow (math-add 1 rate) num)))
175 (math-add (math-mul amount
176 (math-div (math-mul (math-sub 1 (math-div 1 p))
179 (math-div (or lump 0) p))))
181 (put 'calcFunc-pvb 'math-expandable t)
183 (defun calcFunc-npv (rate &rest flows)
184 (math-check-financial rate 1)
185 (math-with-extra-prec 2
186 (let* ((flat (math-flatten-many-vecs flows))
187 (pp (math-add 1 rate))
190 (while (setq flat (cdr flat))
191 (setq accum (math-add accum (math-div (car flat) p))
195 (put 'calcFunc-npv 'math-expandable t)
197 (defun calcFunc-npvb (rate &rest flows)
198 (math-check-financial rate 1)
199 (math-with-extra-prec 2
200 (let* ((flat (math-flatten-many-vecs flows))
201 (pp (math-add 1 rate))
204 (while (setq flat (cdr flat))
205 (setq accum (math-add accum (math-div (car flat) p))
209 (put 'calcFunc-npvb 'math-expandable t)
211 (defun calcFunc-fv (rate num amount &optional initial)
212 (math-check-financial rate num)
213 (math-with-extra-prec 2
214 (let ((p (math-pow (math-add 1 rate) num)))
215 (math-add (math-mul amount
216 (math-div (math-sub p 1)
218 (math-mul (or initial 0) p))))
220 (put 'calcFunc-fv 'math-expandable t)
222 (defun calcFunc-fvl (rate num amount)
223 (calcFunc-fv rate num 0 amount)
225 (put 'calcFunc-fvl 'math-expandable t)
227 (defun calcFunc-fvb (rate num amount &optional initial)
228 (math-check-financial rate num)
229 (math-with-extra-prec 2
230 (let ((p (math-pow (math-add 1 rate) num)))
231 (math-add (math-mul amount
232 (math-div (math-mul (math-sub p 1)
235 (math-mul (or initial 0) p))))
237 (put 'calcFunc-fvb 'math-expandable t)
239 (defun calcFunc-pmt (rate num amount &optional lump)
240 (math-check-financial rate num)
241 (math-with-extra-prec 2
242 (let ((p (math-pow (math-add 1 rate) num)))
243 (math-div (math-mul (math-sub amount
244 (math-div (or lump 0) p))
246 (math-sub 1 (math-div 1 p)))))
248 (put 'calcFunc-pmt 'math-expandable t)
250 (defun calcFunc-pmtb (rate num amount &optional lump)
251 (math-check-financial rate num)
252 (math-with-extra-prec 2
253 (let ((p (math-pow (math-add 1 rate) num)))
254 (math-div (math-mul (math-sub amount (math-div (or lump 0) p)) rate)
255 (math-mul (math-sub 1 (math-div 1 p))
256 (math-add 1 rate)))))
258 (put 'calcFunc-pmtb 'math-expandable t)
260 (defun calcFunc-nper (rate pmt amount &optional lump)
261 (math-compute-nper rate pmt amount lump nil)
263 (put 'calcFunc-nper 'math-expandable t)
265 (defun calcFunc-nperb (rate pmt amount &optional lump)
266 (math-compute-nper rate pmt amount lump 'b)
268 (put 'calcFunc-nperb 'math-expandable t)
270 (defun calcFunc-nperl (rate pmt amount)
271 (math-compute-nper rate pmt amount nil 'l)
273 (put 'calcFunc-nperl 'math-expandable t)
275 (defun math-compute-nper (rate pmt amount lump bflag)
276 (and lump (math-zerop lump)
278 (and lump (math-zerop pmt)
282 (or (math-objectp rate) (and math-expand-formulas (null lump))
283 (math-reject-arg rate 'numberp))
284 (and (math-zerop rate)
285 (math-reject-arg rate 'nonzerop))
286 (or (math-objectp pmt) (and math-expand-formulas (null lump))
287 (math-reject-arg pmt 'numberp))
288 (or (math-objectp amount) (and math-expand-formulas (null lump))
289 (math-reject-arg amount 'numberp))
292 (or (math-objectp lump)
293 (math-reject-arg lump 'numberp))
294 (let ((root (math-find-root (list 'calcFunc-eq
299 '(var DUMMY var-DUMMY)
303 '(var DUMMY var-DUMMY)
306 (if (math-vectorp root)
309 (math-with-extra-prec 2
310 (let ((temp (if (eq bflag 'l)
311 (math-div amount pmt)
312 (math-sub 1 (math-div (math-mul amount rate)
314 (math-mul pmt (math-add 1 rate))
316 (if (or (math-posp temp) math-expand-formulas)
317 (math-neg (calcFunc-log temp (math-add 1 rate)))
318 (math-reject-arg pmt "*Payment too small to cover interest rate")))))
321 (defun calcFunc-rate (num pmt amount &optional lump)
322 (math-compute-rate num pmt amount lump 'calcFunc-pv)
325 (defun calcFunc-rateb (num pmt amount &optional lump)
326 (math-compute-rate num pmt amount lump 'calcFunc-pvb)
329 (defun math-compute-rate (num pmt amount lump func)
330 (or (math-objectp num)
331 (math-reject-arg num 'numberp))
332 (or (math-objectp pmt)
333 (math-reject-arg pmt 'numberp))
334 (or (math-objectp amount)
335 (math-reject-arg amount 'numberp))
338 (math-reject-arg lump 'numberp))
339 (let ((root (math-find-root (list 'calcFunc-eq
341 '(var DUMMY var-DUMMY)
346 '(var DUMMY var-DUMMY)
347 '(intv 3 (float 1 -4) 1)
349 (if (math-vectorp root)
354 (defun calcFunc-ratel (num pmt amount)
355 (or (math-objectp num) math-expand-formulas
356 (math-reject-arg num 'numberp))
357 (or (math-objectp pmt) math-expand-formulas
358 (math-reject-arg pmt 'numberp))
359 (or (math-objectp amount) math-expand-formulas
360 (math-reject-arg amount 'numberp))
361 (math-with-extra-prec 2
362 (math-sub (math-pow (math-div pmt amount) (math-div 1 num)) 1))
365 (defun calcFunc-irr (&rest vecs)
366 (math-compute-irr vecs 'calcFunc-npv)
369 (defun calcFunc-irrb (&rest vecs)
370 (math-compute-irr vecs 'calcFunc-npvb)
373 (defun math-compute-irr (vecs func)
374 (let* ((flat (math-flatten-many-vecs vecs))
375 (root (math-find-root (list func
376 '(var DUMMY var-DUMMY)
378 '(var DUMMY var-DUMMY)
379 '(intv 3 (float 1 -4) 1)
381 (if (math-vectorp root)
386 (defun math-check-financial (rate num)
387 (or (math-objectp rate) math-expand-formulas
388 (math-reject-arg rate 'numberp))
389 (and (math-zerop rate)
390 (math-reject-arg rate 'nonzerop))
391 (or (math-objectp num) math-expand-formulas
392 (math-reject-arg num 'numberp))
396 (defun calcFunc-sln (cost salvage life &optional period)
397 (or (math-realp cost) math-expand-formulas
398 (math-reject-arg cost 'realp))
399 (or (math-realp salvage) math-expand-formulas
400 (math-reject-arg salvage 'realp))
401 (or (math-realp life) math-expand-formulas
402 (math-reject-arg life 'realp))
403 (if (math-zerop life) (math-reject-arg life 'nonzerop))
405 (if (math-num-integerp period)
406 (or (Math-lessp life period) (not (math-posp period)))
407 (math-reject-arg period 'integerp)))
409 (math-div (math-sub cost salvage) life))
411 (put 'calcFunc-sln 'math-expandable t)
413 (defun calcFunc-syd (cost salvage life period)
414 (or (math-realp cost) math-expand-formulas
415 (math-reject-arg cost 'realp))
416 (or (math-realp salvage) math-expand-formulas
417 (math-reject-arg salvage 'realp))
418 (or (math-realp life) math-expand-formulas
419 (math-reject-arg life 'realp))
420 (if (math-zerop life) (math-reject-arg life 'nonzerop))
421 (or (math-realp period) math-expand-formulas
422 (math-reject-arg period 'realp))
423 (if (or (Math-lessp life period) (not (math-posp period)))
425 (math-div (math-mul (math-sub cost salvage)
426 (math-add (math-sub life period) 1))
427 (math-div (math-mul life (math-add life 1)) 2)))
429 (put 'calcFunc-syd 'math-expandable t)
431 (defun calcFunc-ddb (cost salvage life period)
432 (if (math-messy-integerp period) (setq period (math-trunc period)))
433 (or (integerp period) (math-reject-arg period 'fixnump))
434 (or (math-realp cost) (math-reject-arg cost 'realp))
435 (or (math-realp salvage) (math-reject-arg salvage 'realp))
436 (or (math-realp life) (math-reject-arg life 'realp))
437 (if (math-zerop life) (math-reject-arg life 'nonzerop))
438 (if (or (Math-lessp life period) (<= period 0))
442 (while (>= (setq period (1- period)) 0)
443 (setq res (math-div (math-mul book 2) life)
444 book (math-sub book res))
445 (if (Math-lessp book salvage)
446 (setq res (math-add res (math-sub book salvage))