Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Created January 24, 2016 11:41
Show Gist options
  • Save LiamKarlMitchell/7167108c4fb1bf5de47b to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/7167108c4fb1bf5de47b to your computer and use it in GitHub Desktop.
// A way to compare two floating point numbers for "equality".
const EPSILON = 0.00000001;
function fEqual(a, b) {
return ((a - b) < EPSILON && (b - a) < EPSILON);
}
// An infamous mistake by rookies is to notice the following.
// 0.1 + 0.2 == 0.3
// And think it is a mistake when the language says false. Us humans would just say 0.3.
// But because of how float points work in computer you end up with something like 0.30000000000000004.
// For more information definatly read this: http://floating-point-gui.de/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment