Created
August 1, 2016 06:50
-
-
Save wilzbach/f2e5fc48e76c92fef7eb4f8b55b80e4b to your computer and use it in GitHub Desktop.
FP magic with 32-bit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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