Thread overview
How to serialize a double.
Dec 01, 2016
Jake Pittis
Dec 01, 2016
H. S. Teoh
Dec 01, 2016
Jerry
Dec 01, 2016
Basile B.
Dec 01, 2016
Bauss
Dec 01, 2016
Jake Pittis
December 01, 2016
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.
}
````
November 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.
December 01, 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?
December 01, 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 ?
December 01, 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
````
December 01, 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:
>> [...]
>
> 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!