Code Monkey home page Code Monkey logo

almost-equals's Introduction

almost-equals

Compare floats for equality.

namespace Jstewmc\AlmostEquals;

// instantiate the service
$service = new AlmostEquals();

// compare stuff!
$service(1/10, 0.1);  // returns true (0.1 === 0.1)
$service(2/10, 0.1);  // returns false (0.2 !== 0.1)

Floating point numbers should never be compared for equivalence because of the way they are stored internally. They have limited precision, and many numbers that are representable as floating point numbers in base-10 (e.g., 0.1 or 0.7) do not have an exact representation in base-2.

Epsilon

To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, and is the largest acceptable difference in calculations (exclusive).

The service's epsilon can be set on instantiation. If omitted, it will default to 0.00001. Keep in mind, the epsilon value should be proportional to the differences in the values you're comparing. If very small differences matter to your application, use a very small epsilon.

namespace Jstewmc\AlmostEquals;

// defaults to an epsilon of 0.00001
$service = new AlmostEquals();

$service(1/10, 0.1);      // returns true
$service(0.10002, 0.1);   // returns false (0.00002 > 0.00001)
$service(0.100002, 0.1);  // returns true (0.000002 < 0.00001)

Integers

To increase ease-of-use, this library supports integer comparison as well. It will use PHP's default type coercion to convert integers to floats before comparison.

That's about it!

Author

Jack Clayton

License

MIT

Version

0.1.0, March 5, 2017

  • Initial release

almost-equals's People

Contributors

jstewmc avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.