a way to specily floating-point numbers as bit patters

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 9 10:14:37 PDT 2017


On Friday, 9 June 2017 at 16:42:31 UTC, ketmar wrote:
> Basile B. wrote:
>> Yes, easy to do, a template alĂ  octal or hexString.
>
> can you show it, please? remember, CTFE-able!

Sure, here's a dirty draft:

template binFloat(string sign, string exp, string mant)
{
     enum s = sign == "+" ? "0" : "1";
     const b = mixin("0b" ~ s ~ exp ~ mant);
     pragma(msg,"0b" ~ s ~ exp ~ mant);
     enum binFloat = *cast(float*) &b;
}

unittest
{
     static assert(binFloat!("+", "10000000", 
"10000000000000000000000") == 3.0f);
     static assert(binFloat!("-", "10000000", 
"10000000000000000000000") == -3.0f);
}


More information about the Digitalmars-d-learn mailing list