Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Created April 25, 2014 11:51
Show Gist options
  • Select an option

  • Save KittyGiraudel/11286934 to your computer and use it in GitHub Desktop.

Select an option

Save KittyGiraudel/11286934 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.5)
// Compass (v1.0.0.alpha.18)
// ----
// Because of the way floats work in any language
// You cannot expect a float to be precise.
// ---
// While one could expect 0.12 to equal 0.10 + 0.02
// Unfortunately, it is not.
// ---
// Hence a little function to check whether 2 floats are equal or not
// Inspired by: https://github.com/nex3/sass/issues/1224
// ---
// @param [number] $a: first number
// @param [number] $b: second number
// @param [number] $tolerance: tolerance (epsilon)
// ---
// @return [bool]
@function approximately-equal($a, $b, $tolerance: 0.0000001) {
@return abs($a - $b) <= $tolerance;
}
// Alias
@function a-equal($a, $b, $tolerance: 0.0000001) {
@return approximately-equal($a, $b, $tolerance);
}
// Test
test {
$a: 0.12;
$b: 0.10 + 0.02;
bug: $a == $b;
fix: approximately-equal($a, $b);
}
test {
bug: false;
fix: true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment