Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何测试Clojure中的两个数字是否接近_Unit Testing_Testing_Clojure - Fatal编程技术网

Unit testing 如何测试Clojure中的两个数字是否接近

Unit testing 如何测试Clojure中的两个数字是否接近,unit-testing,testing,clojure,Unit Testing,Testing,Clojure,在Clojure中,测试两个数字是否彼此接近的惯用方法是什么 沿着以下路线的某处: (deftest sqrt-test (is (~= 1.414 (Math/sqrt 2))) 您需要定义“关闭”。如果您要求小于某个公差的“差异”,您可以使用滚动您自己的函数,尽管我喜欢MichałMarczyk的解决方案,但请注意已经有了 大约= clojure.algo.generic.math-functions中定义的函数。示例用法: clojuree.o=> (approx= 1.41

在Clojure中,测试两个数字是否彼此接近的惯用方法是什么

沿着以下路线的某处:

(deftest sqrt-test
   (is (~= 1.414 (Math/sqrt 2)))

您需要定义“关闭”。如果您要求小于某个公差的“差异”,您可以使用
滚动您自己的函数,尽管我喜欢MichałMarczyk的解决方案,但请注意已经有了

大约=

clojure.algo.generic.math-functions中定义的函数。示例用法:

clojuree.o=> (approx= 1.41 (sqrt 2) 1e-2)
true
clojuree.o=> (approx= 1.41 (sqrt 2) 1e-4)
false
(defn absolute-difference ^double [^double x ^double y]
  (Math/abs (double (- x y))))
(defn relative-difference ^double [^double x ^double y]
  (/ (Math/abs (- x y))
     (max (Math/abs x) (Math/abs y))))
clojuree.o=> (approx= 1.41 (sqrt 2) 1e-2)
true
clojuree.o=> (approx= 1.41 (sqrt 2) 1e-4)
false