Mapping float to ulong in CTFE

berni44 dlang at d-ecke.de
Thu Dec 12 19:21:22 UTC 2019


Is it possible to get to the bits of a float in CTFE? I tried the 
following, but this doesn't work:

```
import std.stdio;

union FloatBits
{
     float floatValue;
     ulong ulongValue;
}

ulong test(float f)
{
     FloatBits fb;
     fb.floatValue = f;
     return fb.ulongValue;
}

void main()
{
     static assert(test(3.0) == 1077936128);
}
```

test.d(13): Error: reinterpretation through overlapped field 
ulongValue is not allowed in CTFE
test.d(18):        called from here: test(3.00000F)
test.d(18):        while evaluating: static assert(test(3.00000F) 
== 1077936128LU)


More information about the Digitalmars-d-learn mailing list