Last active
January 13, 2021 19:56
-
-
Save leomonteiro92/4040888a2f286e19b0b68888797a86e8 to your computer and use it in GitHub Desktop.
Cálculo CET em Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;CET (based on implementation of https://github.com/eduardordm/cet-java) | |
;; Run with inlein http://inlein.org | |
;; Download inlein and run ./inlein cet.clj | |
'{:dependencies [ | |
[org.clojure/clojure "1.10.1"] | |
[clojure.java-time "0.3.2"]] } | |
(require '[java-time :as t]) | |
(def CET_MAX_VALUE 10000.00) | |
(def CET_PRECISION 0.00001) | |
(def PRECISION 2) | |
(defn cet-per-month [fc0, fcj, n] | |
(loop [cet 0.0] | |
(let [total (reduce (fn [acc, nth] (+ acc (/ fcj (Math/pow (+ 1.0 cet) (+ nth 1)) ))) 0.0 (range 0 n))] | |
(let [incr (+ cet CET_PRECISION)] | |
(when (>= incr CET_MAX_VALUE) -1) | |
(if (<= (- total fc0) 0) (Double/parseDouble (format "%.2f" (* incr 100.0))) | |
(recur (* incr (/ total fc0)))))))) | |
(defn cet-per-year [fc0, fcj, n, d0, dj0] | |
(loop [cet 0.0] | |
(let [total | |
(reduce | |
(fn [acc, nth] | |
(let [dj (if (= nth 0) dj0 (t/plus dj0 (t/months nth)))] | |
(let [delta (t/time-between d0 dj :days)] | |
(+ acc (/ fcj (Math/pow (+ 1.0 cet) (/ delta 365.0))))))) | |
0.0 | |
(range 0 n))] | |
(let [incr (+ cet CET_PRECISION)] | |
(when (>= incr CET_MAX_VALUE) -1) | |
(if (<= (- total fc0) 0) (Double/parseDouble (format "%.2f" (* incr 100.0))) | |
(recur (* incr (/ total fc0)))))))) | |
(println (cet-per-month 940.0 205.73 24)) | |
(println (cet-per-year 940.0 205.73 24 (t/local-date 2012 01 01) (t/local-date 2012 02 01))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment