Skip to content

Instantly share code, notes, and snippets.

@wilzbach
Created August 1, 2016 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilzbach/f2e5fc48e76c92fef7eb4f8b55b80e4b to your computer and use it in GitHub Desktop.
Save wilzbach/f2e5fc48e76c92fef7eb4f8b55b80e4b to your computer and use it in GitHub Desktop.
FP magic with 32-bit
import std.stdio;
alias S = float;
float shL = 0x1.1b95eep-4; // -0.069235
float shR = 0x1.9c7cfep-7; // -0.012588
F fun(F)(F x)
{
return 1.0 + x * 2;
}
S f1()
{
S r = fun(shR);
S l = fun(shL);
return (r - l);
}
S f2()
{
return (fun(shR) - fun(shL));
}
unittest
{
writefln("f1: %a", f1());
writefln("f2: %a", f2());
assert(f1 == f2); // fails on 32-bit
}
f1: -0x1.d00cap-4
f2: -0x1.d00c9cp-4
core.exception.AssertError@en.d(28): unittest failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment