Floating point constant folding

Guillaume Chatelet via Digitalmars-d digitalmars-d at puremagic.com
Fri Mar 3 01:31:19 PST 2017


Context: 
http://forum.dlang.org/post/qybweycrifqgtcssepgx@forum.dlang.org

---  prints 1 ---
void main(string[] args)
{
     import std.stdio;
     import core.stdc.fenv;
     fesetround(FE_UPWARD);
     writefln("%.32g", 1.0f + float.min_normal);
}
---

--- prints 1.00000011920928955078125 ---
void main(string[] args)
{
     import std.stdio;
     import core.stdc.fenv;
     fesetround(FE_UPWARD);
     float x = 1.0f;
     x += float.min_normal;
     writefln("%.32g", x);
}
---

Considering the floating point operations have a runtime 
component, it seems to me that constant folding is not allowed to 
occur in the first example. For example, it does not occur in the 
following C++ snippet:
---
#include <limits>
#include <cstdio>
#include <cfenv>

int main(int, char**) {
     std::fesetround(FE_UPWARD);
     printf("%.32g\n", std::numeric_limits<float>::denorm_min() + 
1.0f);
     return 0;
}
---

Thoughts?


More information about the Digitalmars-d mailing list