1 ;;; calc-fin.el --- financial functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;;; Financial functions.
36 (if (calc-is-hyperbolic)
37 (calc-enter-result 3 "pvl" (cons 'calcFunc-pvl
(calc-top-list-n 3)))
39 (calc-enter-result 3 "pvb" (cons 'calcFunc-pvb
(calc-top-list-n 3)))
40 (calc-enter-result 3 "pv" (cons 'calcFunc-pv
(calc-top-list-n 3)))))))
42 (defun calc-fin-npv (arg)
46 (calc-vector-op "npvb" 'calcFunc-npvb
(1+ arg
))
47 (calc-vector-op "npv" 'calcFunc-npv
(1+ arg
)))))
52 (if (calc-is-hyperbolic)
53 (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl
(calc-top-list-n 3)))
55 (calc-enter-result 3 "fvb" (cons 'calcFunc-fvb
(calc-top-list-n 3)))
56 (calc-enter-result 3 "fv" (cons 'calcFunc-fv
(calc-top-list-n 3)))))))
58 (defun calc-fin-pmt ()
61 (if (calc-is-hyperbolic)
62 (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl
(calc-top-list-n 3)))
64 (calc-enter-result 3 "pmtb" (cons 'calcFunc-pmtb
(calc-top-list-n 3)))
65 (calc-enter-result 3 "pmt" (cons 'calcFunc-pmt
(calc-top-list-n 3)))))))
67 (defun calc-fin-nper ()
70 (if (calc-is-hyperbolic)
71 (calc-enter-result 3 "nprl" (cons 'calcFunc-nperl
(calc-top-list-n 3)))
73 (calc-enter-result 3 "nprb" (cons 'calcFunc-nperb
75 (calc-enter-result 3 "nper" (cons 'calcFunc-nper
76 (calc-top-list-n 3)))))))
78 (defun calc-fin-rate ()
81 (calc-pop-push-record 3
82 (if (calc-is-hyperbolic) "ratl"
83 (if (calc-is-inverse) "ratb" "rate"))
86 (cons (if (calc-is-hyperbolic) 'calcFunc-ratel
87 (if (calc-is-hyperbolic) 'calcFunc-rateb
89 (calc-top-list-n 3)))))))
91 (defun calc-fin-irr (arg)
95 (calc-vector-op "irrb" 'calcFunc-irrb arg
)
96 (calc-vector-op "irr" 'calcFunc-irr arg
))))
98 (defun calc-fin-sln ()
101 (calc-enter-result 3 "sln" (cons 'calcFunc-sln
(calc-top-list-n 3)))))
103 (defun calc-fin-syd ()
106 (calc-enter-result 4 "syd" (cons 'calcFunc-syd
(calc-top-list-n 4)))))
108 (defun calc-fin-ddb ()
111 (calc-enter-result 4 "ddb" (cons 'calcFunc-ddb
(calc-top-list-n 4)))))
114 (defun calc-to-percentage (x)
115 (cond ((Math-objectp x
)
116 (setq x
(math-mul x
100))
117 (if (Math-num-integerp x
)
118 (setq x
(math-trunc x
)))
119 (list 'calcFunc-percent x
))
121 (cons 'vec
(mapcar 'calc-to-percentage
(cdr x
))))
124 (defun calc-convert-percent ()
127 (calc-pop-push-record 1 "c%" (calc-to-percentage (calc-top-n 1)))))
129 (defun calc-percent-change ()
132 (let ((res (calc-normalize (cons 'calcFunc-relch
(calc-top-list 2)))))
133 (calc-pop-push-record 2 "%ch" (calc-to-percentage res
)))))
136 ;;; Financial functions.
138 (defun calcFunc-pv (rate num amount
&optional lump
)
139 (math-check-financial rate num
)
140 (math-with-extra-prec 2
141 (let ((p (math-pow (math-add 1 rate
) num
)))
142 (math-add (math-mul amount
143 (math-div (math-sub 1 (math-div 1 p
))
145 (math-div (or lump
0) p
)))))
146 (put 'calcFunc-pv
'math-expandable t
)
148 (defun calcFunc-pvl (rate num amount
)
149 (calcFunc-pv rate num
0 amount
))
150 (put 'calcFunc-pvl
'math-expandable t
)
152 (defun calcFunc-pvb (rate num amount
&optional lump
)
153 (math-check-financial rate num
)
154 (math-with-extra-prec 2
155 (let* ((p (math-pow (math-add 1 rate
) num
)))
156 (math-add (math-mul amount
157 (math-div (math-mul (math-sub 1 (math-div 1 p
))
160 (math-div (or lump
0) p
)))))
161 (put 'calcFunc-pvb
'math-expandable t
)
163 (defun calcFunc-npv (rate &rest flows
)
164 (math-check-financial rate
1)
165 (math-with-extra-prec 2
166 (let* ((flat (math-flatten-many-vecs flows
))
167 (pp (math-add 1 rate
))
170 (while (setq flat
(cdr flat
))
171 (setq accum
(math-add accum
(math-div (car flat
) p
))
174 (put 'calcFunc-npv
'math-expandable t
)
176 (defun calcFunc-npvb (rate &rest flows
)
177 (math-check-financial rate
1)
178 (math-with-extra-prec 2
179 (let* ((flat (math-flatten-many-vecs flows
))
180 (pp (math-add 1 rate
))
183 (while (setq flat
(cdr flat
))
184 (setq accum
(math-add accum
(math-div (car flat
) p
))
187 (put 'calcFunc-npvb
'math-expandable t
)
189 (defun calcFunc-fv (rate num amount
&optional initial
)
190 (math-check-financial rate num
)
191 (math-with-extra-prec 2
192 (let ((p (math-pow (math-add 1 rate
) num
)))
193 (math-add (math-mul amount
194 (math-div (math-sub p
1)
196 (math-mul (or initial
0) p
)))))
197 (put 'calcFunc-fv
'math-expandable t
)
199 (defun calcFunc-fvl (rate num amount
)
200 (calcFunc-fv rate num
0 amount
))
201 (put 'calcFunc-fvl
'math-expandable t
)
203 (defun calcFunc-fvb (rate num amount
&optional initial
)
204 (math-check-financial rate num
)
205 (math-with-extra-prec 2
206 (let ((p (math-pow (math-add 1 rate
) num
)))
207 (math-add (math-mul amount
208 (math-div (math-mul (math-sub p
1)
211 (math-mul (or initial
0) p
)))))
212 (put 'calcFunc-fvb
'math-expandable t
)
214 (defun calcFunc-pmt (rate num amount
&optional lump
)
215 (math-check-financial rate num
)
216 (math-with-extra-prec 2
217 (let ((p (math-pow (math-add 1 rate
) num
)))
218 (math-div (math-mul (math-sub amount
219 (math-div (or lump
0) p
))
221 (math-sub 1 (math-div 1 p
))))))
222 (put 'calcFunc-pmt
'math-expandable t
)
224 (defun calcFunc-pmtb (rate num amount
&optional lump
)
225 (math-check-financial rate num
)
226 (math-with-extra-prec 2
227 (let ((p (math-pow (math-add 1 rate
) num
)))
228 (math-div (math-mul (math-sub amount
(math-div (or lump
0) p
)) rate
)
229 (math-mul (math-sub 1 (math-div 1 p
))
230 (math-add 1 rate
))))))
231 (put 'calcFunc-pmtb
'math-expandable t
)
233 (defun calcFunc-nper (rate pmt amount
&optional lump
)
234 (math-compute-nper rate pmt amount lump nil
))
235 (put 'calcFunc-nper
'math-expandable t
)
237 (defun calcFunc-nperb (rate pmt amount
&optional lump
)
238 (math-compute-nper rate pmt amount lump
'b
))
239 (put 'calcFunc-nperb
'math-expandable t
)
241 (defun calcFunc-nperl (rate pmt amount
)
242 (math-compute-nper rate pmt amount nil
'l
))
243 (put 'calcFunc-nperl
'math-expandable t
)
245 (defun math-compute-nper (rate pmt amount lump bflag
)
246 (and lump
(math-zerop lump
)
248 (and lump
(math-zerop pmt
)
252 (or (math-objectp rate
) (and math-expand-formulas
(null lump
))
253 (math-reject-arg rate
'numberp
))
254 (and (math-zerop rate
)
255 (math-reject-arg rate
'nonzerop
))
256 (or (math-objectp pmt
) (and math-expand-formulas
(null lump
))
257 (math-reject-arg pmt
'numberp
))
258 (or (math-objectp amount
) (and math-expand-formulas
(null lump
))
259 (math-reject-arg amount
'numberp
))
262 (or (math-objectp lump
)
263 (math-reject-arg lump
'numberp
))
264 (let ((root (math-find-root (list 'calcFunc-eq
269 '(var DUMMY var-DUMMY
)
273 '(var DUMMY var-DUMMY
)
276 (if (math-vectorp root
)
279 (math-with-extra-prec 2
280 (let ((temp (if (eq bflag
'l
)
281 (math-div amount pmt
)
282 (math-sub 1 (math-div (math-mul amount rate
)
284 (math-mul pmt
(math-add 1 rate
))
286 (if (or (math-posp temp
) math-expand-formulas
)
287 (math-neg (calcFunc-log temp
(math-add 1 rate
)))
288 (math-reject-arg pmt
"*Payment too small to cover interest rate"))))))
290 (defun calcFunc-rate (num pmt amount
&optional lump
)
291 (math-compute-rate num pmt amount lump
'calcFunc-pv
))
293 (defun calcFunc-rateb (num pmt amount
&optional lump
)
294 (math-compute-rate num pmt amount lump
'calcFunc-pvb
))
296 (defun math-compute-rate (num pmt amount lump func
)
297 (or (math-objectp num
)
298 (math-reject-arg num
'numberp
))
299 (or (math-objectp pmt
)
300 (math-reject-arg pmt
'numberp
))
301 (or (math-objectp amount
)
302 (math-reject-arg amount
'numberp
))
305 (math-reject-arg lump
'numberp
))
306 (let ((root (math-find-root (list 'calcFunc-eq
308 '(var DUMMY var-DUMMY
)
313 '(var DUMMY var-DUMMY
)
314 '(intv 3 (float 1 -
4) 1)
316 (if (math-vectorp root
)
320 (defun calcFunc-ratel (num pmt amount
)
321 (or (math-objectp num
) math-expand-formulas
322 (math-reject-arg num
'numberp
))
323 (or (math-objectp pmt
) math-expand-formulas
324 (math-reject-arg pmt
'numberp
))
325 (or (math-objectp amount
) math-expand-formulas
326 (math-reject-arg amount
'numberp
))
327 (math-with-extra-prec 2
328 (math-sub (math-pow (math-div pmt amount
) (math-div 1 num
)) 1)))
330 (defun calcFunc-irr (&rest vecs
)
331 (math-compute-irr vecs
'calcFunc-npv
))
333 (defun calcFunc-irrb (&rest vecs
)
334 (math-compute-irr vecs
'calcFunc-npvb
))
336 (defun math-compute-irr (vecs func
)
337 (let* ((flat (math-flatten-many-vecs vecs
))
338 (root (math-find-root (list func
339 '(var DUMMY var-DUMMY
)
341 '(var DUMMY var-DUMMY
)
342 '(intv 3 (float 1 -
4) 1)
344 (if (math-vectorp root
)
348 (defun math-check-financial (rate num
)
349 (or (math-objectp rate
) math-expand-formulas
350 (math-reject-arg rate
'numberp
))
351 (and (math-zerop rate
)
352 (math-reject-arg rate
'nonzerop
))
353 (or (math-objectp num
) math-expand-formulas
354 (math-reject-arg num
'numberp
)))
357 (defun calcFunc-sln (cost salvage life
&optional period
)
358 (or (math-realp cost
) math-expand-formulas
359 (math-reject-arg cost
'realp
))
360 (or (math-realp salvage
) math-expand-formulas
361 (math-reject-arg salvage
'realp
))
362 (or (math-realp life
) math-expand-formulas
363 (math-reject-arg life
'realp
))
364 (if (math-zerop life
) (math-reject-arg life
'nonzerop
))
366 (if (math-num-integerp period
)
367 (or (Math-lessp life period
) (not (math-posp period
)))
368 (math-reject-arg period
'integerp
)))
370 (math-div (math-sub cost salvage
) life
)))
371 (put 'calcFunc-sln
'math-expandable t
)
373 (defun calcFunc-syd (cost salvage life period
)
374 (or (math-realp cost
) math-expand-formulas
375 (math-reject-arg cost
'realp
))
376 (or (math-realp salvage
) math-expand-formulas
377 (math-reject-arg salvage
'realp
))
378 (or (math-realp life
) math-expand-formulas
379 (math-reject-arg life
'realp
))
380 (if (math-zerop life
) (math-reject-arg life
'nonzerop
))
381 (or (math-realp period
) math-expand-formulas
382 (math-reject-arg period
'realp
))
383 (if (or (Math-lessp life period
) (not (math-posp period
)))
385 (math-div (math-mul (math-sub cost salvage
)
386 (math-add (math-sub life period
) 1))
387 (math-div (math-mul life
(math-add life
1)) 2))))
388 (put 'calcFunc-syd
'math-expandable t
)
390 (defun calcFunc-ddb (cost salvage life period
)
391 (if (math-messy-integerp period
) (setq period
(math-trunc period
)))
392 (or (integerp period
) (math-reject-arg period
'fixnump
))
393 (or (math-realp cost
) (math-reject-arg cost
'realp
))
394 (or (math-realp salvage
) (math-reject-arg salvage
'realp
))
395 (or (math-realp life
) (math-reject-arg life
'realp
))
396 (if (math-zerop life
) (math-reject-arg life
'nonzerop
))
397 (if (or (Math-lessp life period
) (<= period
0))
401 (while (>= (setq period
(1- period
)) 0)
402 (setq res
(math-div (math-mul book
2) life
)
403 book
(math-sub book res
))
404 (if (Math-lessp book salvage
)
405 (setq res
(math-add res
(math-sub book salvage
))
411 ;;; calc-fin.el ends here