www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to serialize a double.

reply Jake Pittis <jakepittis gmail.com> writes:
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
next sibling parent Jerry <hurricane hereiam.com> writes:
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
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
prev sibling next sibling parent Basile B. <b2.temp gmx.com> writes:
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
prev sibling parent reply Bauss <jj_1337 live.dk> writes:
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
parent Jake Pittis <jakepittis gmail.com> writes:
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:
 [...]
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 ````
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!
Dec 01 2016