June 09, 2017 Re: a way to specily floating-point numbers as bit patters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | Basile B. wrote:
> enum binFloat = *cast(float*) &b;
i was SO sure that this won't work in CTFE that i didn't even tried to do it. "it will fail anyway, there is no reason in trying!" ;-)
|
June 09, 2017 Re: a way to specily floating-point numbers as bit patters | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 9 June 2017 at 17:18:43 UTC, ketmar wrote:
> Basile B. wrote:
>
>> enum binFloat = *cast(float*) &b;
>
> i was SO sure that this won't work in CTFE that i didn't even tried to do it. "it will fail anyway, there is no reason in trying!" ;-)
You can do the arithmetic as well. I don't know why but i supposed that my static asserts were a proof of CTFE-ability.
|
June 09, 2017 Re: a way to specily floating-point numbers as bit patters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | Basile B. wrote:
> On Friday, 9 June 2017 at 17:18:43 UTC, ketmar wrote:
>> Basile B. wrote:
>>
>>> enum binFloat = *cast(float*) &b;
>>
>> i was SO sure that this won't work in CTFE that i didn't even tried to do it. "it will fail anyway, there is no reason in trying!" ;-)
>
> You can do the arithmetic as well. I don't know why but i supposed that my static asserts were a proof of CTFE-ability.
yeah, it is CTFEable. now i recall that such casting for floats was special-cased in interpreter exactly to allow this kind of things. my bad.
|
June 09, 2017 Re: a way to specily floating-point numbers as bit patters | ||||
---|---|---|---|---|
| ||||
Posted in reply to Honey | Honey wrote:
> On Friday, 9 June 2017 at 16:34:28 UTC, ketmar wrote:
>>> Try -0.16666685f.
>>
>> it doesn't matter if i can find the decimal representation for the given bit pattern or not. the whole post is about removing the need to rely on lossy binary->decimal->binary conversions.
>
> Lossless turn-around is guaranteed if you are using sufficiently many digits. In case of IEEE-754 single precision it's 8 significant decimal digits.
it is highly platform-dependent. and both bin->dec, and dec->bin conversion routines can contain errors, btw. so using decimal forms for exact bit-patterns is the last thing i want to do, as i know how fragile they are.
|
June 09, 2017 Re: a way to specily floating-point numbers as bit patters | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 9 June 2017 at 17:25:22 UTC, ketmar wrote:
> it is highly platform-dependent. and both bin->dec, and dec->bin conversion routines can contain errors, btw. so using decimal forms for exact bit-patterns is the last thing i want to do, as i know how fragile they are.
Sure. Hex-format is the right choice if you want a fixed bit pattern.
|
June 09, 2017 Re: a way to specily floating-point numbers as bit patters | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Friday, 9 June 2017 at 16:56:46 UTC, ketmar wrote:
> Petar Kirov [ZombineDev] wrote:
>
>> Do HexFloats (http://dlang.org/spec/lex#HexFloat) help?
>
> hm. i somehow completely missed "%a" format specifier! yeah, "-0x1.55556ep-3" did the trick.
>
> tnx. i should do my homework *before* posting big rants, lol.
There's also FloatRep in std.bitmanip.
|
Copyright © 1999-2021 by the D Language Foundation