-
Notifications
You must be signed in to change notification settings - Fork 6
/
math.cljc
24 lines (22 loc) · 906 Bytes
/
math.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(ns axel-f.functions.math
(:require [axel-f.error :as error]
[axel-f.functions.coercion :as coercion]))
(defn round-fn
([d] (round-fn d 0))
([d precision]
(if-let [d (coercion/excel-number d)]
(if-let [precision (coercion/excel-number precision)]
(let [factor (Math/pow 10 precision)
res (/ (Math/round (* d factor)) factor)]
(if (> precision 0)
res
(int res)))
(throw (error/error "#VALUE!" (error/format-not-a-number-error "ROUND" 2 precision))))
(throw (error/error "#VALUE!" (error/format-not-a-number-error "ROUND" 1 d))))))
(defn sum-fn [& items]
(reduce #?(:clj +' :cljs +)
(map (fn [n]
(if-let [n (coercion/excel-number n)]
n
(throw (error/error "#VALUE!" (error/format-not-a-number-error "SUM" nil n)))))
(flatten items))))