Int to float?

Jesse Phillips via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 5 15:50:25 PST 2015


On Thursday, 5 March 2015 at 20:06:55 UTC, Taylor Hillegeist 
wrote:
> On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:
>> Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist:
>>> How to I cast a Int to float without changing its binary 
>>> representation?
>>
>> int someValue = 5;
>> float sameBinary = *(cast(float*)cast(void*)&someValue);
>
> ahh of course! lol :)

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.

import std.stdio;
void main() {
	union Fi {
		float f;
		int i;
	}

	Fi fi;
	fi.i = 65;
	writeln(fi.f);
}


More information about the Digitalmars-d-learn mailing list