calc-fin.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. ;;; calc-fin.el --- financial functions for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; This file is autoloaded from calc-ext.el.
  19. (require 'calc-ext)
  20. (require 'calc-macs)
  21. ;;; Financial functions.
  22. (defun calc-fin-pv ()
  23. (interactive)
  24. (calc-slow-wrapper
  25. (if (calc-is-hyperbolic)
  26. (calc-enter-result 3 "pvl" (cons 'calcFunc-pvl (calc-top-list-n 3)))
  27. (if (calc-is-inverse)
  28. (calc-enter-result 3 "pvb" (cons 'calcFunc-pvb (calc-top-list-n 3)))
  29. (calc-enter-result 3 "pv" (cons 'calcFunc-pv (calc-top-list-n 3)))))))
  30. (defun calc-fin-npv (arg)
  31. (interactive "p")
  32. (calc-slow-wrapper
  33. (if (calc-is-inverse)
  34. (calc-vector-op "npvb" 'calcFunc-npvb (1+ arg))
  35. (calc-vector-op "npv" 'calcFunc-npv (1+ arg)))))
  36. (defun calc-fin-fv ()
  37. (interactive)
  38. (calc-slow-wrapper
  39. (if (calc-is-hyperbolic)
  40. (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl (calc-top-list-n 3)))
  41. (if (calc-is-inverse)
  42. (calc-enter-result 3 "fvb" (cons 'calcFunc-fvb (calc-top-list-n 3)))
  43. (calc-enter-result 3 "fv" (cons 'calcFunc-fv (calc-top-list-n 3)))))))
  44. (defun calc-fin-pmt ()
  45. (interactive)
  46. (calc-slow-wrapper
  47. (if (calc-is-hyperbolic)
  48. (calc-enter-result 3 "fvl" (cons 'calcFunc-fvl (calc-top-list-n 3)))
  49. (if (calc-is-inverse)
  50. (calc-enter-result 3 "pmtb" (cons 'calcFunc-pmtb (calc-top-list-n 3)))
  51. (calc-enter-result 3 "pmt" (cons 'calcFunc-pmt (calc-top-list-n 3)))))))
  52. (defun calc-fin-nper ()
  53. (interactive)
  54. (calc-slow-wrapper
  55. (if (calc-is-hyperbolic)
  56. (calc-enter-result 3 "nprl" (cons 'calcFunc-nperl (calc-top-list-n 3)))
  57. (if (calc-is-inverse)
  58. (calc-enter-result 3 "nprb" (cons 'calcFunc-nperb
  59. (calc-top-list-n 3)))
  60. (calc-enter-result 3 "nper" (cons 'calcFunc-nper
  61. (calc-top-list-n 3)))))))
  62. (defun calc-fin-rate ()
  63. (interactive)
  64. (calc-slow-wrapper
  65. (calc-pop-push-record 3
  66. (if (calc-is-hyperbolic) "ratl"
  67. (if (calc-is-inverse) "ratb" "rate"))
  68. (calc-to-percentage
  69. (calc-normalize
  70. (cons (if (calc-is-hyperbolic) 'calcFunc-ratel
  71. (if (calc-is-hyperbolic) 'calcFunc-rateb
  72. 'calcFunc-rate))
  73. (calc-top-list-n 3)))))))
  74. (defun calc-fin-irr (arg)
  75. (interactive "P")
  76. (calc-slow-wrapper
  77. (if (calc-is-inverse)
  78. (calc-vector-op "irrb" 'calcFunc-irrb arg)
  79. (calc-vector-op "irr" 'calcFunc-irr arg))))
  80. (defun calc-fin-sln ()
  81. (interactive)
  82. (calc-slow-wrapper
  83. (calc-enter-result 3 "sln" (cons 'calcFunc-sln (calc-top-list-n 3)))))
  84. (defun calc-fin-syd ()
  85. (interactive)
  86. (calc-slow-wrapper
  87. (calc-enter-result 4 "syd" (cons 'calcFunc-syd (calc-top-list-n 4)))))
  88. (defun calc-fin-ddb ()
  89. (interactive)
  90. (calc-slow-wrapper
  91. (calc-enter-result 4 "ddb" (cons 'calcFunc-ddb (calc-top-list-n 4)))))
  92. (defun calc-to-percentage (x)
  93. (cond ((Math-objectp x)
  94. (setq x (math-mul x 100))
  95. (if (Math-num-integerp x)
  96. (setq x (math-trunc x)))
  97. (list 'calcFunc-percent x))
  98. ((Math-vectorp x)
  99. (cons 'vec (mapcar 'calc-to-percentage (cdr x))))
  100. (t x)))
  101. (defun calc-convert-percent ()
  102. (interactive)
  103. (calc-slow-wrapper
  104. (calc-pop-push-record 1 "c%" (calc-to-percentage (calc-top-n 1)))))
  105. (defun calc-percent-change ()
  106. (interactive)
  107. (calc-slow-wrapper
  108. (let ((res (calc-normalize (cons 'calcFunc-relch (calc-top-list 2)))))
  109. (calc-pop-push-record 2 "%ch" (calc-to-percentage res)))))
  110. ;;; Financial functions.
  111. (defun calcFunc-pv (rate num amount &optional lump)
  112. (math-check-financial rate num)
  113. (math-with-extra-prec 2
  114. (let ((p (math-pow (math-add 1 rate) num)))
  115. (math-add (math-mul amount
  116. (math-div (math-sub 1 (math-div 1 p))
  117. rate))
  118. (math-div (or lump 0) p)))))
  119. (put 'calcFunc-pv 'math-expandable t)
  120. (defun calcFunc-pvl (rate num amount)
  121. (calcFunc-pv rate num 0 amount))
  122. (put 'calcFunc-pvl 'math-expandable t)
  123. (defun calcFunc-pvb (rate num amount &optional lump)
  124. (math-check-financial rate num)
  125. (math-with-extra-prec 2
  126. (let* ((p (math-pow (math-add 1 rate) num)))
  127. (math-add (math-mul amount
  128. (math-div (math-mul (math-sub 1 (math-div 1 p))
  129. (math-add 1 rate))
  130. rate))
  131. (math-div (or lump 0) p)))))
  132. (put 'calcFunc-pvb 'math-expandable t)
  133. (defun calcFunc-npv (rate &rest flows)
  134. (math-check-financial rate 1)
  135. (math-with-extra-prec 2
  136. (let* ((flat (math-flatten-many-vecs flows))
  137. (pp (math-add 1 rate))
  138. (p pp)
  139. (accum 0))
  140. (while (setq flat (cdr flat))
  141. (setq accum (math-add accum (math-div (car flat) p))
  142. p (math-mul p pp)))
  143. accum)))
  144. (put 'calcFunc-npv 'math-expandable t)
  145. (defun calcFunc-npvb (rate &rest flows)
  146. (math-check-financial rate 1)
  147. (math-with-extra-prec 2
  148. (let* ((flat (math-flatten-many-vecs flows))
  149. (pp (math-add 1 rate))
  150. (p 1)
  151. (accum 0))
  152. (while (setq flat (cdr flat))
  153. (setq accum (math-add accum (math-div (car flat) p))
  154. p (math-mul p pp)))
  155. accum)))
  156. (put 'calcFunc-npvb 'math-expandable t)
  157. (defun calcFunc-fv (rate num amount &optional initial)
  158. (math-check-financial rate num)
  159. (math-with-extra-prec 2
  160. (let ((p (math-pow (math-add 1 rate) num)))
  161. (math-add (math-mul amount
  162. (math-div (math-sub p 1)
  163. rate))
  164. (math-mul (or initial 0) p)))))
  165. (put 'calcFunc-fv 'math-expandable t)
  166. (defun calcFunc-fvl (rate num amount)
  167. (calcFunc-fv rate num 0 amount))
  168. (put 'calcFunc-fvl 'math-expandable t)
  169. (defun calcFunc-fvb (rate num amount &optional initial)
  170. (math-check-financial rate num)
  171. (math-with-extra-prec 2
  172. (let ((p (math-pow (math-add 1 rate) num)))
  173. (math-add (math-mul amount
  174. (math-div (math-mul (math-sub p 1)
  175. (math-add 1 rate))
  176. rate))
  177. (math-mul (or initial 0) p)))))
  178. (put 'calcFunc-fvb 'math-expandable t)
  179. (defun calcFunc-pmt (rate num amount &optional lump)
  180. (math-check-financial rate num)
  181. (math-with-extra-prec 2
  182. (let ((p (math-pow (math-add 1 rate) num)))
  183. (math-div (math-mul (math-sub amount
  184. (math-div (or lump 0) p))
  185. rate)
  186. (math-sub 1 (math-div 1 p))))))
  187. (put 'calcFunc-pmt 'math-expandable t)
  188. (defun calcFunc-pmtb (rate num amount &optional lump)
  189. (math-check-financial rate num)
  190. (math-with-extra-prec 2
  191. (let ((p (math-pow (math-add 1 rate) num)))
  192. (math-div (math-mul (math-sub amount (math-div (or lump 0) p)) rate)
  193. (math-mul (math-sub 1 (math-div 1 p))
  194. (math-add 1 rate))))))
  195. (put 'calcFunc-pmtb 'math-expandable t)
  196. (defun calcFunc-nper (rate pmt amount &optional lump)
  197. (math-compute-nper rate pmt amount lump nil))
  198. (put 'calcFunc-nper 'math-expandable t)
  199. (defun calcFunc-nperb (rate pmt amount &optional lump)
  200. (math-compute-nper rate pmt amount lump 'b))
  201. (put 'calcFunc-nperb 'math-expandable t)
  202. (defun calcFunc-nperl (rate pmt amount)
  203. (math-compute-nper rate pmt amount nil 'l))
  204. (put 'calcFunc-nperl 'math-expandable t)
  205. (defun math-compute-nper (rate pmt amount lump bflag)
  206. (and lump (math-zerop lump)
  207. (setq lump nil))
  208. (and lump (math-zerop pmt)
  209. (setq amount lump
  210. lump nil
  211. bflag 'l))
  212. (or (math-objectp rate) (and math-expand-formulas (null lump))
  213. (math-reject-arg rate 'numberp))
  214. (and (math-zerop rate)
  215. (math-reject-arg rate 'nonzerop))
  216. (or (math-objectp pmt) (and math-expand-formulas (null lump))
  217. (math-reject-arg pmt 'numberp))
  218. (or (math-objectp amount) (and math-expand-formulas (null lump))
  219. (math-reject-arg amount 'numberp))
  220. (if lump
  221. (progn
  222. (or (math-objectp lump)
  223. (math-reject-arg lump 'numberp))
  224. (let ((root (math-find-root (list 'calcFunc-eq
  225. (list (if bflag
  226. 'calcFunc-pvb
  227. 'calcFunc-pv)
  228. rate
  229. '(var DUMMY var-DUMMY)
  230. pmt
  231. lump)
  232. amount)
  233. '(var DUMMY var-DUMMY)
  234. '(intv 3 0 100)
  235. t)))
  236. (if (math-vectorp root)
  237. (nth 1 root)
  238. root)))
  239. (math-with-extra-prec 2
  240. (let ((temp (if (eq bflag 'l)
  241. (math-div amount pmt)
  242. (math-sub 1 (math-div (math-mul amount rate)
  243. (if bflag
  244. (math-mul pmt (math-add 1 rate))
  245. pmt))))))
  246. (if (or (math-posp temp) math-expand-formulas)
  247. (math-neg (calcFunc-log temp (math-add 1 rate)))
  248. (math-reject-arg pmt "*Payment too small to cover interest rate"))))))
  249. (defun calcFunc-rate (num pmt amount &optional lump)
  250. (math-compute-rate num pmt amount lump 'calcFunc-pv))
  251. (defun calcFunc-rateb (num pmt amount &optional lump)
  252. (math-compute-rate num pmt amount lump 'calcFunc-pvb))
  253. (defun math-compute-rate (num pmt amount lump func)
  254. (or (math-objectp num)
  255. (math-reject-arg num 'numberp))
  256. (or (math-objectp pmt)
  257. (math-reject-arg pmt 'numberp))
  258. (or (math-objectp amount)
  259. (math-reject-arg amount 'numberp))
  260. (or (null lump)
  261. (math-objectp lump)
  262. (math-reject-arg lump 'numberp))
  263. (let ((root (math-find-root (list 'calcFunc-eq
  264. (list func
  265. '(var DUMMY var-DUMMY)
  266. num
  267. pmt
  268. (or lump 0))
  269. amount)
  270. '(var DUMMY var-DUMMY)
  271. '(intv 3 (float 1 -4) 1)
  272. t)))
  273. (if (math-vectorp root)
  274. (nth 1 root)
  275. root)))
  276. (defun calcFunc-ratel (num pmt amount)
  277. (or (math-objectp num) math-expand-formulas
  278. (math-reject-arg num 'numberp))
  279. (or (math-objectp pmt) math-expand-formulas
  280. (math-reject-arg pmt 'numberp))
  281. (or (math-objectp amount) math-expand-formulas
  282. (math-reject-arg amount 'numberp))
  283. (math-with-extra-prec 2
  284. (math-sub (math-pow (math-div pmt amount) (math-div 1 num)) 1)))
  285. (defun calcFunc-irr (&rest vecs)
  286. (math-compute-irr vecs 'calcFunc-npv))
  287. (defun calcFunc-irrb (&rest vecs)
  288. (math-compute-irr vecs 'calcFunc-npvb))
  289. (defun math-compute-irr (vecs func)
  290. (let* ((flat (math-flatten-many-vecs vecs))
  291. (root (math-find-root (list func
  292. '(var DUMMY var-DUMMY)
  293. flat)
  294. '(var DUMMY var-DUMMY)
  295. '(intv 3 (float 1 -4) 1)
  296. t)))
  297. (if (math-vectorp root)
  298. (nth 1 root)
  299. root)))
  300. (defun math-check-financial (rate num)
  301. (or (math-objectp rate) math-expand-formulas
  302. (math-reject-arg rate 'numberp))
  303. (and (math-zerop rate)
  304. (math-reject-arg rate 'nonzerop))
  305. (or (math-objectp num) math-expand-formulas
  306. (math-reject-arg num 'numberp)))
  307. (defun calcFunc-sln (cost salvage life &optional period)
  308. (or (math-realp cost) math-expand-formulas
  309. (math-reject-arg cost 'realp))
  310. (or (math-realp salvage) math-expand-formulas
  311. (math-reject-arg salvage 'realp))
  312. (or (math-realp life) math-expand-formulas
  313. (math-reject-arg life 'realp))
  314. (if (math-zerop life) (math-reject-arg life 'nonzerop))
  315. (if (and period
  316. (if (math-num-integerp period)
  317. (or (Math-lessp life period) (not (math-posp period)))
  318. (math-reject-arg period 'integerp)))
  319. 0
  320. (math-div (math-sub cost salvage) life)))
  321. (put 'calcFunc-sln 'math-expandable t)
  322. (defun calcFunc-syd (cost salvage life period)
  323. (or (math-realp cost) math-expand-formulas
  324. (math-reject-arg cost 'realp))
  325. (or (math-realp salvage) math-expand-formulas
  326. (math-reject-arg salvage 'realp))
  327. (or (math-realp life) math-expand-formulas
  328. (math-reject-arg life 'realp))
  329. (if (math-zerop life) (math-reject-arg life 'nonzerop))
  330. (or (math-realp period) math-expand-formulas
  331. (math-reject-arg period 'realp))
  332. (if (or (Math-lessp life period) (not (math-posp period)))
  333. 0
  334. (math-div (math-mul (math-sub cost salvage)
  335. (math-add (math-sub life period) 1))
  336. (math-div (math-mul life (math-add life 1)) 2))))
  337. (put 'calcFunc-syd 'math-expandable t)
  338. (defun calcFunc-ddb (cost salvage life period)
  339. (if (math-messy-integerp period) (setq period (math-trunc period)))
  340. (or (integerp period) (math-reject-arg period 'fixnump))
  341. (or (math-realp cost) (math-reject-arg cost 'realp))
  342. (or (math-realp salvage) (math-reject-arg salvage 'realp))
  343. (or (math-realp life) (math-reject-arg life 'realp))
  344. (if (math-zerop life) (math-reject-arg life 'nonzerop))
  345. (if (or (Math-lessp life period) (<= period 0))
  346. 0
  347. (let ((book cost)
  348. (res 0))
  349. (while (>= (setq period (1- period)) 0)
  350. (setq res (math-div (math-mul book 2) life)
  351. book (math-sub book res))
  352. (if (Math-lessp book salvage)
  353. (setq res (math-add res (math-sub book salvage))
  354. book salvage)))
  355. res)))
  356. (provide 'calc-fin)
  357. ;;; calc-fin.el ends here