digitalmars.D.learn - How to serialize a double.
- Jake Pittis (12/12) Nov 30 2016 How do I convert a double to a ubyte[]?
- Jerry (4/16) Nov 30 2016 That test passes for me, are you sure there isn't something else
- H. S. Teoh via Digitalmars-d-learn (13/27) Nov 30 2016 union U
- Basile B. (2/14) Nov 30 2016 platform, archi, compiler version ?
- Bauss (26/38) Nov 30 2016 You could do something like below which will allow you to
- Jake Pittis (5/32) Dec 01 2016 Regarding the test assertion that failed. Turns out I had a bug
How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. ```` unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } ````
Nov 30 2016
On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote:How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. ```` unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } ````That test passes for me, are you sure there isn't something else wrong with your code? Check to see if it works for just a ulong that has values in it's upper 32-bits?
Nov 30 2016
On Thu, Dec 01, 2016 at 12:36:30AM +0000, Jake Pittis via Digitalmars-d-learn wrote:How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. ```` unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } ````union U { ubyte[double.sizeof] bytes; double d; } U u, v; u.d = 3.14159; v.bytes[] = u.bytes[]; assert(v.d == 3.14159); T -- Those who don't understand Unix are condemned to reinvent it, poorly.
Nov 30 2016
On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote:How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. ```` unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } ````platform, archi, compiler version ?
Nov 30 2016
On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote:How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. ```` unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } ````You could do something like below which will allow you to serialize any number. ```` import std.stdio : writeln; import std.traits : isNumeric; ubyte[] bytes(T)(T num) if (isNumeric!T) { auto buf = new ubyte[T.sizeof]; (*cast(T*)(buf.ptr)) = num; return buf; } T value(T)(ubyte[] buf) if (isNumeric!T) { return (*cast(T*)(buf.ptr)); } ```` And example usage: ```` double foo = 3.14; writeln(foo); // Prints 3.14 ubyte[] bar = foo.bytes; writeln(bar); // Prints the bytes equal to 3.14 foo = bar.value!double; writeln(foo); // Prints 3.14 ````
Nov 30 2016
On Thursday, 1 December 2016 at 07:13:45 UTC, Bauss wrote:On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote:Regarding the test assertion that failed. Turns out I had a bug in the test. (of course) This last solution is very pretty. Thanks. You folks are all so kind![...]You could do something like below which will allow you to serialize any number. ```` import std.stdio : writeln; import std.traits : isNumeric; ubyte[] bytes(T)(T num) if (isNumeric!T) { auto buf = new ubyte[T.sizeof]; (*cast(T*)(buf.ptr)) = num; return buf; } T value(T)(ubyte[] buf) if (isNumeric!T) { return (*cast(T*)(buf.ptr)); } ```` And example usage: ```` double foo = 3.14; writeln(foo); // Prints 3.14 ubyte[] bar = foo.bytes; writeln(bar); // Prints the bytes equal to 3.14 foo = bar.value!double; writeln(foo); // Prints 3.14 ````
Dec 01 2016