reinterpret_cast float to uint

matovitch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 29 06:45:10 PDT 2015


On Sunday, 29 March 2015 at 13:39:47 UTC, matovitch wrote:
> Hi,
>
> floats are stored on 32 bits using ieee754...and I would like 
> (for some obscure reason) to reinterpret a such float into a 32 
> bits uint (i.e without altering the memory). A simple :
>
> import std.stdio;
>
> void main()
> {
>     float f = 0.5;
>     uint i = cast(uint)(f);
>     writeln(i);
> }
>
> doesn't work since it just round the float.
>
> In C++, I do : reinterpret_cast<std::size_t&>(my_float). How 
> could I do this in D ?
>
> Thanks in advance for your help !

Ok this works :

import std.stdio;

void main()
{
     float f = 0.5;
     uint i = *cast(uint*)(&f);
     writeln(i);
}

It is kind of logical...I feel a bit dumb. :D


More information about the Digitalmars-d-learn mailing list