Skip to content

Instantly share code, notes, and snippets.

@PJensen
Created April 22, 2013 14:05
Show Gist options
  • Select an option

  • Save PJensen/5435374 to your computer and use it in GitHub Desktop.

Select an option

Save PJensen/5435374 to your computer and use it in GitHub Desktop.
Just a demonstration of IEEE floating point issue
var d = 0.0;
for (var i = 0; i < 100; i++)
{
d += 0.1;
d += 0.01;
d += 0.001;
d += 0.0001;
d += 0.00001;
d += 0.000001;
d += 0.0000001;
d += 0.00000001;
}
for (var i = 0; i < 100; i++)
{
d -= 0.00000001;
d -= 0.0000001;
d -= 0.000001;
d -= 0.00001;
d -= 0.0001;
d -= 0.001;
d -= 0.01;
d -= 0.1;
}
// expect 0, get -4.57966997657877E-16
Console.WriteLine(d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment