Int to float?

Nicolas Sicard via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Mar 6 04:11:37 PST 2015


On Friday, 6 March 2015 at 00:57:16 UTC, Ola Fosheim Grøstad 
wrote:
> On Thursday, 5 March 2015 at 23:50:28 UTC, Jesse Phillips wrote:
>> I think I read somewhere you don't want to use unions like 
>> this, but I think it is more because you generally don't want 
>> to reinterpret bits.
>
> It is non-portable, since some hardware architectures may use 
> different representations (e.g. different byte order on int and 
> float).

Then maybe use std.bitmanip?

   import std.bitmanip;
   int i = 5;
   float f = bigEndianToNative!float(nativeToBigEndian(i));
   // or float f = 
littleEndianToNative!float(nativeToLittleEndian(i));


More information about the Digitalmars-d-learn mailing list