Coder Social home page Coder Social logo

same-ish's Introduction

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

cljdoc Build status Coverage 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.

Usage

See the full API docs are available on cljdoc for more detailed instructions and 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.core:

(ns foo-test
  (:requre [clojure.test :refer :all]
           [same.core :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))))

Requirements

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

References

Changelog

See CHANGELOG.md.

Contributing

Contributions are welcomed, see CONTRIBUTING.md for details.

License

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

same-ish's People

Contributors

baumandm avatar dependabot[bot] avatar dl1ely avatar emlyn avatar microsoft-github-policy-service[bot] avatar msftgits avatar sritchie avatar willcohen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

same-ish's Issues

Negative numbers are all zeroish?

  (require '[same :as same])
  (same/zeroish? 0) ;; true
  (same/zeroish? 1) ;; false
  (same/zeroish? -1) ;; true
  (same/zeroish? -100) ;; true

Very close number not ish?

I am not understanding a lot on floating points, but it seems to me that these
2 should be "equal" for ish?:

(ish? 1798.4039776258396
         1798.4039776258326)

but they are not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.