2017 © Pedro Peláez
 

library almost-equals

Compare floats for equality

image

jstewmc/almost-equals

Compare floats for equality

  • Monday, March 6, 2017
  • by Jstewmc
  • Repository
  • 1 Watchers
  • 1 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 20 % Grown

The README.md

almost-equals

Compare floats for equality., (*1)

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., (*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)., (*3)

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., (*4)

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., (*5)

That's about it!, (*6)

Author

Jack Clayton, (*7)

License

MIT, (*8)

Version

0.1.0, March 5, 2017

  • Initial release

The Versions

06/03 2017

dev-master

9999999-dev

Compare floats for equality

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Jack Clayton

php compare equality floats jstewmc

06/03 2017

v0.1.0

0.1.0.0

Compare floats for equality

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Jack Clayton

php compare equality floats jstewmc