Liking cljdoc? Tell your friends :D

same/ish - floating point comparisons for tests.

Equality may perhaps be a right, but no power on earth can ever turn it into a fact.
        — Honore de Balzac

Travis CI Codecov Dependencies Status Downloads

You have some functions that return doubles or floats and you want to write some tests. How do you compare your results to the expected values? You can't use equality because of rounding errors, so you have to check if the values are near each other. But how near is near enough? And what if your numbers are buried inside data structures?

same/ish is designed to help with these situations.


See the API docs for more detailed instructions and interactive examples. A brief summary is provided below.

In Leiningen, add the following to your :dependencies in project.clj:

Clojars Project

Then in your test namespace(s), require same:

(ns foo-test
  (:requre [clojure.test :refer :all]
           [same :refer [ish? zeroish?]]))

To compare two numbers, instead of using = or ==, use ish?:

(defn f [x]
  (* (/ 1. x) x)))

(deftest f-test
  (is (ish? 1.0 (f 49.0))))

You can also compare data structures, and they will be compared element-wise, using ish? for floating point types, == for other numbers, and = for anything else:

(defn g [x]
  {:a x
   :b [(* x x) (Math/sqrt x)]))

(deftest g-test
  (is (ish? {:a 23 :b [529 4.7958315233]}
            (g 23.0))))


Clojure 1.7.0 or higher is required due to the reader conditionals required for ClojureScript support.





Contributions are welcomed, see for details.


Copyright © Microsoft Corporation. All rights reserved.
Licensed under the MIT License.

Can you improve this documentation? These fine people already did:
Emlyn Corrin & Dave Bauman
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close