digitalmars.D.learn - Int to float?
- Taylor Hillegeist (2/2) Mar 05 2015 How to I cast a Int to float without changing its binary
- Benjamin Thaut (3/4) Mar 05 2015 int someValue = 5;
- Taylor Hillegeist (2/7) Mar 05 2015 ahh of course! lol :)
- Jesse Phillips (15/23) Mar 05 2015 I think I read somewhere you don't want to use unions like this,
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (10/13) Mar 05 2015 It is non-portable, since some hardware architectures may use
- bearophile (4/6) Mar 06 2015 I am not sure if D is the same as C regarding this.
- Nicolas Sicard (8/15) Mar 06 2015 Then maybe use std.bitmanip?
- anonymous (2/4) Mar 05 2015 The cast(void*) isn't necessary.
- badlink (7/11) Mar 05 2015 Actually even the cast is unecessary, just use a uniform.
- anonymous (2/15) Mar 05 2015 That's not really simpler, though.
- Adam D. Ruppe (9/10) Mar 05 2015 Maybe, but I think the union is a bit nicer because then the
How to I cast a Int to float without changing its binary representation?
Mar 05 2015
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);
Mar 05 2015
On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist:ahh of course! lol :)How to I cast a Int to float without changing its binary representation?int someValue = 5; float sameBinary = *(cast(float*)cast(void*)&someValue);
Mar 05 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: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); }Am 05.03.2015 um 21:00 schrieb Taylor Hillegeist:ahh of course! lol :)How to I cast a Int to float without changing its binary representation?int someValue = 5; float sameBinary = *(cast(float*)cast(void*)&someValue);
Mar 05 2015
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). D claims to follow C, so using unions for type punning is ultimately implementation defined. In C++ using unions for type punning is illegal/undefined behaviour, so in C++ you should use memcpy. Memcpy also has the advantage of explicitly copying thus avoiding some aliasing issues.
Mar 05 2015
Ola Fosheim Grøstad:D claims to follow C, so using unions for type punning is ultimately implementation defined.I am not sure if D is the same as C regarding this. Bye, bearophile
Mar 06 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:Then maybe use std.bitmanip? import std.bitmanip; int i = 5; float f = bigEndianToNative!float(nativeToBigEndian(i)); // or float f = littleEndianToNative!float(nativeToLittleEndian(i));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).
Mar 06 2015
On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:int someValue = 5; float sameBinary = *(cast(float*)cast(void*)&someValue);The cast(void*) isn't necessary.
Mar 05 2015
On Thursday, 5 March 2015 at 20:16:55 UTC, anonymous wrote:On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:Actually even the cast is unecessary, just use a uniform. union N { int i; float f; } http://dpaste.dzfl.pl/58b6eddcf725int someValue = 5; float sameBinary = *(cast(float*)cast(void*)&someValue);The cast(void*) isn't necessary.
Mar 05 2015
On Thursday, 5 March 2015 at 20:21:18 UTC, badlink wrote:On Thursday, 5 March 2015 at 20:16:55 UTC, anonymous wrote:That's not really simpler, though.On Thursday, 5 March 2015 at 20:03:09 UTC, Benjamin Thaut wrote:Actually even the cast is unecessary, just use a uniform. union N { int i; float f; } http://dpaste.dzfl.pl/58b6eddcf725int someValue = 5; float sameBinary = *(cast(float*)cast(void*)&someValue);The cast(void*) isn't necessary.
Mar 05 2015
On Thursday, 5 March 2015 at 20:32:20 UTC, anonymous wrote:That's not really simpler, though.Maybe, but I think the union is a bit nicer because then the compiler is responsible for more of the details. For example, it should work with class objects without the complication of dealing with the fact that they are already pointers under the hood. Either way works though and should compile to the same instructions, just if I was doing it as a generic library, I think I'd use the union method.
Mar 05 2015