www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reinterpret_cast float to uint

reply "matovitch" <camille.brugel laposte.net> writes:
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 !
Mar 29 2015
parent reply "matovitch" <camille.brugel laposte.net> writes:
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
Mar 29 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 29 Mar 2015 13:45:10 +0000, matovitch wrote:

you can also use unions.=
Mar 29 2015
parent reply "matovitch" <camille.brugel laposte.net> writes:
On Sunday, 29 March 2015 at 14:50:24 UTC, ketmar wrote:
 On Sun, 29 Mar 2015 13:45:10 +0000, matovitch wrote:

 you can also use unions.
Good idea ! In my case I think it was better to cast, but this could be helpful another time thanks ! :)
Mar 29 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 29 Mar 2015 16:00:05 +0000, matovitch wrote:

 On Sunday, 29 March 2015 at 14:50:24 UTC, ketmar wrote:
 On Sun, 29 Mar 2015 13:45:10 +0000, matovitch wrote:

 you can also use unions.
=20 Good idea ! In my case I think it was better to cast, but this could be helpful another time thanks ! :)
unions also looks better than pointers, and they are easier to read, i=20 think. ;-) union FU { float f; uint u; } void main () pure { float t =3D 42.0; assert((cast(FU)t).u =3D=3D 0x42280000); }=
Mar 29 2015
parent reply "Namespace" <rswhite4 gmail.com> writes:
On Sunday, 29 March 2015 at 16:29:40 UTC, ketmar wrote:
 On Sun, 29 Mar 2015 16:00:05 +0000, matovitch wrote:

 On Sunday, 29 March 2015 at 14:50:24 UTC, ketmar wrote:
 On Sun, 29 Mar 2015 13:45:10 +0000, matovitch wrote:

 you can also use unions.
Good idea ! In my case I think it was better to cast, but this could be helpful another time thanks ! :)
unions also looks better than pointers, and they are easier to read, i think. ;-) union FU { float f; uint u; } void main () pure { float t = 42.0; assert((cast(FU)t).u == 0x42280000); }
AFAIK this would be undefined behaviour in C++, right?
Mar 29 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 29 Mar 2015 17:33:02 +0000, Namespace wrote:

 On Sunday, 29 March 2015 at 16:29:40 UTC, ketmar wrote:
 On Sun, 29 Mar 2015 16:00:05 +0000, matovitch wrote:

 On Sunday, 29 March 2015 at 14:50:24 UTC, ketmar wrote:
 On Sun, 29 Mar 2015 13:45:10 +0000, matovitch wrote:

 you can also use unions.
=20 Good idea ! In my case I think it was better to cast, but this could be helpful another time thanks ! :)
unions also looks better than pointers, and they are easier to read, i think. ;-) union FU { float f; uint u; } void main () pure { float t =3D 42.0; assert((cast(FU)t).u =3D=3D 0x42280000); }
=20 AFAIK this would be undefined behaviour in C++, right?
i honestly don't remember. C++ itself is UB. ;-)=
Mar 29 2015