1 ;;; calc-fin.el --- financial functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
34 (defun calc-Need-calc-fin () nil
)
37 ;;; Financial functions.
42 (if (calc-is-hyperbolic)
43 (calc-enter-result 3 "pvl" (cons 'calcFunc-pvl
(calc-top-list-n 3)))
45 (calc-enter-result 3 "pvb" (cons 'calcFunc-pvb
(calc-top-list-n 3)))
46 (calc-enter-result 3 "pv" (cons 'calcFunc-pv
(calc-top-list-n 3)))))))
48 (defun calc-fin-npv (arg)
52 (calc-vector-op "npvb" 'calcFunc-npvb
(1+ arg
))
53 (calc-vector-op "npv" 'calcFunc-npv
(1+ arg
)))))
58 (if (calc-is-hyperbolic)
59 (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl
(calc-top-list-n 3)))
61 (calc-enter-result 3 "fvb" (cons 'calcFunc-fvb
(calc-top-list-n 3)))
62 (calc-enter-result 3 "fv" (cons 'calcFunc-fv
(calc-top-list-n 3)))))))
64 (defun calc-fin-pmt ()
67 (if (calc-is-hyperbolic)
68 (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl
(calc-top-list-n 3)))
70 (calc-enter-result 3 "pmtb" (cons 'calcFunc-pmtb
(calc-top-list-n 3)))
71 (calc-enter-result 3 "pmt" (cons 'calcFunc-pmt
(calc-top-list-n 3)))))))
73 (defun calc-fin-nper ()
76 (if (calc-is-hyperbolic)
77 (calc-enter-result 3 "nprl" (cons 'calcFunc-nperl
(calc-top-list-n 3)))
79 (calc-enter-result 3 "nprb" (cons 'calcFunc-nperb
81 (calc-enter-result 3 "nper" (cons 'calcFunc-nper
82 (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)))))))
97 (defun calc-fin-irr (arg)
100 (if (calc-is-inverse)
101 (calc-vector-op "irrb" 'calcFunc-irrb arg
)
102 (calc-vector-op "irr" 'calcFunc-irr arg
))))
104 (defun calc-fin-sln ()
107 (calc-enter-result 3 "sln" (cons 'calcFunc-sln
(calc-top-list-n 3)))))
109 (defun calc-fin-syd ()
112 (calc-enter-result 4 "syd" (cons 'calcFunc-syd
(calc-top-list-n 4)))))
114 (defun calc-fin-ddb ()
117 (calc-enter-result 4 "ddb" (cons 'calcFunc-ddb
(calc-top-list-n 4)))))
120 (defun calc-to-percentage (x)
121 (cond ((Math-objectp x
)
122 (setq x
(math-mul x
100))
123 (if (Math-num-integerp x
)
124 (setq x
(math-trunc x
)))
125 (list 'calcFunc-percent x
))
127 (cons 'vec
(mapcar 'calc-to-percentage
(cdr x
))))
130 (defun calc-convert-percent ()
133 (calc-pop-push-record 1 "c%" (calc-to-percentage (calc-top-n 1)))))
135 (defun calc-percent-change ()
138 (let ((res (calc-normalize (cons 'calcFunc-relch
(calc-top-list 2)))))
139 (calc-pop-push-record 2 "%ch" (calc-to-percentage res
)))))
142 ;;; Financial functions.
144 (defun calcFunc-pv (rate num amount
&optional lump
)
145 (math-check-financial rate num
)
146 (math-with-extra-prec 2
147 (let ((p (math-pow (math-add 1 rate
) num
)))
148 (math-add (math-mul amount
149 (math-div (math-sub 1 (math-div 1 p
))
151 (math-div (or lump
0) p
)))))
152 (put 'calcFunc-pv
'math-expandable t
)
154 (defun calcFunc-pvl (rate num amount
)
155 (calcFunc-pv rate num
0 amount
))
156 (put 'calcFunc-pvl
'math-expandable t
)
158 (defun calcFunc-pvb (rate num amount
&optional lump
)
159 (math-check-financial rate num
)
160 (math-with-extra-prec 2
161 (let* ((p (math-pow (math-add 1 rate
) num
)))
162 (math-add (math-mul amount
163 (math-div (math-mul (math-sub 1 (math-div 1 p
))
166 (math-div (or lump
0) p
)))))
167 (put 'calcFunc-pvb
'math-expandable t
)
169 (defun calcFunc-npv (rate &rest flows
)
170 (math-check-financial rate
1)
171 (math-with-extra-prec 2
172 (let* ((flat (math-flatten-many-vecs flows
))
173 (pp (math-add 1 rate
))
176 (while (setq flat
(cdr flat
))
177 (setq accum
(math-add accum
(math-div (car flat
) p
))
180 (put 'calcFunc-npv
'math-expandable t
)
182 (defun calcFunc-npvb (rate &rest flows
)
183 (math-check-financial rate
1)
184 (math-with-extra-prec 2
185 (let* ((flat (math-flatten-many-vecs flows
))
186 (pp (math-add 1 rate
))
189 (while (setq flat
(cdr flat
))
190 (setq accum
(math-add accum
(math-div (car flat
) p
))
193 (put 'calcFunc-npvb
'math-expandable t
)
195 (defun calcFunc-fv (rate num amount
&optional initial
)
196 (math-check-financial rate num
)
197 (math-with-extra-prec 2
198 (let ((p (math-pow (math-add 1 rate
) num
)))
199 (math-add (math-mul amount
200 (math-div (math-sub p
1)
202 (math-mul (or initial
0) p
)))))
203 (put 'calcFunc-fv
'math-expandable t
)
205 (defun calcFunc-fvl (rate num amount
)
206 (calcFunc-fv rate num
0 amount
))
207 (put 'calcFunc-fvl
'math-expandable t
)
209 (defun calcFunc-fvb (rate num amount
&optional initial
)
210 (math-check-financial rate num
)
211 (math-with-extra-prec 2
212 (let ((p (math-pow (math-add 1 rate
) num
)))
213 (math-add (math-mul amount
214 (math-div (math-mul (math-sub p
1)
217 (math-mul (or initial
0) p
)))))
218 (put 'calcFunc-fvb
'math-expandable t
)
220 (defun calcFunc-pmt (rate num amount
&optional lump
)
221 (math-check-financial rate num
)
222 (math-with-extra-prec 2
223 (let ((p (math-pow (math-add 1 rate
) num
)))
224 (math-div (math-mul (math-sub amount
225 (math-div (or lump
0) p
))
227 (math-sub 1 (math-div 1 p
))))))
228 (put 'calcFunc-pmt
'math-expandable t
)
230 (defun calcFunc-pmtb (rate num amount
&optional lump
)
231 (math-check-financial rate num
)
232 (math-with-extra-prec 2
233 (let ((p (math-pow (math-add 1 rate
) num
)))
234 (math-div (math-mul (math-sub amount
(math-div (or lump
0) p
)) rate
)
235 (math-mul (math-sub 1 (math-div 1 p
))
236 (math-add 1 rate
))))))
237 (put 'calcFunc-pmtb
'math-expandable t
)
239 (defun calcFunc-nper (rate pmt amount
&optional lump
)
240 (math-compute-nper rate pmt amount lump nil
))
241 (put 'calcFunc-nper
'math-expandable t
)
243 (defun calcFunc-nperb (rate pmt amount
&optional lump
)
244 (math-compute-nper rate pmt amount lump
'b
))
245 (put 'calcFunc-nperb
'math-expandable t
)
247 (defun calcFunc-nperl (rate pmt amount
)
248 (math-compute-nper rate pmt amount nil
'l
))
249 (put 'calcFunc-nperl
'math-expandable t
)
251 (defun math-compute-nper (rate pmt amount lump bflag
)
252 (and lump
(math-zerop lump
)
254 (and lump
(math-zerop pmt
)
258 (or (math-objectp rate
) (and math-expand-formulas
(null lump
))
259 (math-reject-arg rate
'numberp
))
260 (and (math-zerop rate
)
261 (math-reject-arg rate
'nonzerop
))
262 (or (math-objectp pmt
) (and math-expand-formulas
(null lump
))
263 (math-reject-arg pmt
'numberp
))
264 (or (math-objectp amount
) (and math-expand-formulas
(null lump
))
265 (math-reject-arg amount
'numberp
))
268 (or (math-objectp lump
)
269 (math-reject-arg lump
'numberp
))
270 (let ((root (math-find-root (list 'calcFunc-eq
275 '(var DUMMY var-DUMMY
)
279 '(var DUMMY var-DUMMY
)
282 (if (math-vectorp root
)
285 (math-with-extra-prec 2
286 (let ((temp (if (eq bflag
'l
)
287 (math-div amount pmt
)
288 (math-sub 1 (math-div (math-mul amount rate
)
290 (math-mul pmt
(math-add 1 rate
))
292 (if (or (math-posp temp
) math-expand-formulas
)
293 (math-neg (calcFunc-log temp
(math-add 1 rate
)))
294 (math-reject-arg pmt
"*Payment too small to cover interest rate"))))))
296 (defun calcFunc-rate (num pmt amount
&optional lump
)
297 (math-compute-rate num pmt amount lump
'calcFunc-pv
))
299 (defun calcFunc-rateb (num pmt amount
&optional lump
)
300 (math-compute-rate num pmt amount lump
'calcFunc-pvb
))
302 (defun math-compute-rate (num pmt amount lump func
)
303 (or (math-objectp num
)
304 (math-reject-arg num
'numberp
))
305 (or (math-objectp pmt
)
306 (math-reject-arg pmt
'numberp
))
307 (or (math-objectp amount
)
308 (math-reject-arg amount
'numberp
))
311 (math-reject-arg lump
'numberp
))
312 (let ((root (math-find-root (list 'calcFunc-eq
314 '(var DUMMY var-DUMMY
)
319 '(var DUMMY var-DUMMY
)
320 '(intv 3 (float 1 -
4) 1)
322 (if (math-vectorp root
)
326 (defun calcFunc-ratel (num pmt amount
)
327 (or (math-objectp num
) math-expand-formulas
328 (math-reject-arg num
'numberp
))
329 (or (math-objectp pmt
) math-expand-formulas
330 (math-reject-arg pmt
'numberp
))
331 (or (math-objectp amount
) math-expand-formulas
332 (math-reject-arg amount
'numberp
))
333 (math-with-extra-prec 2
334 (math-sub (math-pow (math-div pmt amount
) (math-div 1 num
)) 1)))
336 (defun calcFunc-irr (&rest vecs
)
337 (math-compute-irr vecs
'calcFunc-npv
))
339 (defun calcFunc-irrb (&rest vecs
)
340 (math-compute-irr vecs
'calcFunc-npvb
))
342 (defun math-compute-irr (vecs func
)
343 (let* ((flat (math-flatten-many-vecs vecs
))
344 (root (math-find-root (list func
345 '(var DUMMY var-DUMMY
)
347 '(var DUMMY var-DUMMY
)
348 '(intv 3 (float 1 -
4) 1)
350 (if (math-vectorp root
)
354 (defun math-check-financial (rate num
)
355 (or (math-objectp rate
) math-expand-formulas
356 (math-reject-arg rate
'numberp
))
357 (and (math-zerop rate
)
358 (math-reject-arg rate
'nonzerop
))
359 (or (math-objectp num
) math-expand-formulas
360 (math-reject-arg num
'numberp
)))
363 (defun calcFunc-sln (cost salvage life
&optional period
)
364 (or (math-realp cost
) math-expand-formulas
365 (math-reject-arg cost
'realp
))
366 (or (math-realp salvage
) math-expand-formulas
367 (math-reject-arg salvage
'realp
))
368 (or (math-realp life
) math-expand-formulas
369 (math-reject-arg life
'realp
))
370 (if (math-zerop life
) (math-reject-arg life
'nonzerop
))
372 (if (math-num-integerp period
)
373 (or (Math-lessp life period
) (not (math-posp period
)))
374 (math-reject-arg period
'integerp
)))
376 (math-div (math-sub cost salvage
) life
)))
377 (put 'calcFunc-sln
'math-expandable t
)
379 (defun calcFunc-syd (cost salvage life period
)
380 (or (math-realp cost
) math-expand-formulas
381 (math-reject-arg cost
'realp
))
382 (or (math-realp salvage
) math-expand-formulas
383 (math-reject-arg salvage
'realp
))
384 (or (math-realp life
) math-expand-formulas
385 (math-reject-arg life
'realp
))
386 (if (math-zerop life
) (math-reject-arg life
'nonzerop
))
387 (or (math-realp period
) math-expand-formulas
388 (math-reject-arg period
'realp
))
389 (if (or (Math-lessp life period
) (not (math-posp period
)))
391 (math-div (math-mul (math-sub cost salvage
)
392 (math-add (math-sub life period
) 1))
393 (math-div (math-mul life
(math-add life
1)) 2))))
394 (put 'calcFunc-syd
'math-expandable t
)
396 (defun calcFunc-ddb (cost salvage life period
)
397 (if (math-messy-integerp period
) (setq period
(math-trunc period
)))
398 (or (integerp period
) (math-reject-arg period
'fixnump
))
399 (or (math-realp cost
) (math-reject-arg cost
'realp
))
400 (or (math-realp salvage
) (math-reject-arg salvage
'realp
))
401 (or (math-realp life
) (math-reject-arg life
'realp
))
402 (if (math-zerop life
) (math-reject-arg life
'nonzerop
))
403 (if (or (Math-lessp life period
) (<= period
0))
407 (while (>= (setq period
(1- period
)) 0)
408 (setq res
(math-div (math-mul book
2) life
)
409 book
(math-sub book res
))
410 (if (Math-lessp book salvage
)
411 (setq res
(math-add res
(math-sub book salvage
))
415 ;;; calc-fin.el ends here