Clojure:
数字取整

如何实现:

在 Clojure 中,我们主要使用 Math/roundMath/floorMath/ceil

(Math/round 3.5) ; => 4
(Math/round 3.4) ; => 3

(Math/floor 3.7) ; => 3.0
(Math/ceil 3.2)  ; => 4.0

针对特定的小数位,我们进行乘法、舍入和除法操作:

(let [num 3.14159
      scale 1000]
  (/ (Math/round (* num scale)) scale)) ; => 3.142

深入探讨

在拥有先进编程语言之前,舍入是一个手动过程,比如算盘或纸张。在编程中,由于浮点数精度的限制,数字表示非常关键。

舍入的替代方法包括使用 BigDecimal 类来控制精度,或者使用像 clojure.math.numeric-tower 这样的库来进行高级数学功能计算。Clojure 的 Math/round 依赖于 Java 的 Math.roundMath/floorMath/ceil 函数,这意味着它继承了同样的浮点数和双精度浮点数的细微差别。

在实现时,记住当 Clojure 处理小数时自动使用双精度浮点数。小心舍入误差!

另请参阅