Clojure - How to set the lower precision
我知道要在Clojure中设置数字精度,我应该使用
1 2 3 4 5 6 7 8 9 | (defn bar-triang [[a b] [c d] [e f]] (def cen [(/ (+ a c) 2.0) (/ (+ b d) 2.0)]) (def g (get cen 0)) (def h (get cen 1)) [(with-precision 4 (+ (bigdec g) (/ (- e g) 3M))) (with-precision 4 (+ (bigdec h) (/ (- f h) 3M)))]) (bar-triang [4, 6], [12, 4], [10, 10]) => [8.666666666666666 6.666666666666667] |
在这里,我将精度设置为4,但是REPL给我的数字与以前相同,但位数更多。此外,我已经使用
稍后重构:
1 2 3 4 5 6 7 8 9 | (defn bar-triang [[a b] [c d] [e f]] (with-precision 4 (let [g (/ (+ a c) 2M) h (/ (+ b d) 2M)] [(+ g (/ (- e g) 3M)) (+ h (/ (- f h) 3M))]))) #'user/bar-triang user=> (bar-triang [4, 6], [12, 4], [10, 10]) [8.667M 6.667M] |