Thread overview
Floating point constant folding
Mar 03, 2017
Guillaume Chatelet
Mar 03, 2017
Johan Engelen
Mar 03, 2017
Guillaume Chatelet
Mar 03, 2017
Fool
March 03, 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?
March 03, 2017
On Friday, 3 March 2017 at 09:31:19 UTC, Guillaume Chatelet wrote:
> 
> 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;
> }

Clang without/with optimizations turned on:
❯ clang++ float.cpp && ./a.out
1.00000011920928955078125

❯ clang++ float.cpp -O3 && ./a.out
1

-Johan

March 03, 2017
On Friday, 3 March 2017 at 22:35:15 UTC, Johan Engelen wrote:
> Clang without/with optimizations turned on:
> ❯ clang++ float.cpp && ./a.out
> 1.00000011920928955078125
>
> ❯ clang++ float.cpp -O3 && ./a.out
> 1
>
> -Johan

Thx Johan I should have checked... My point is moot then.

--
"welcome to the magician world" - Don Clugston | DConf2016

March 03, 2017
On Friday, 3 March 2017 at 22:35:15 UTC, Johan Engelen wrote:
> Clang without/with optimizations turned on:
> ❯ clang++ float.cpp && ./a.out
> 1.00000011920928955078125
>
> ❯ clang++ float.cpp -O3 && ./a.out
> 1
>
> -Johan

GCC with optimizations turned on:
$ g++ float.cpp -O3 -frounding-math && ./a.out
1.00000011920928955078125