### How to store a double xplatform?

Posted:

**Mon Sep 12, 2005 9:14 am**Hi guys,

I am busy with a rewrite from my old serializer (SIO) to wxArchive, which will be the counter part of MFC's CArchive. I want to store all values as x-platform as possible meaning binary compatible so that serialized files can be opened on linux, MAC and Windows without binary incompatibility.

For a double, this is not so easy. One solution is convert to a string and store the double, another solution is convert it to two long or int64 values, there is a function that splits up the fraction and integer part.

However, with a splitted double I get back for example:

243.1434546784578

Integer: 243

Fraction: 0.1434546784578

Where I rather have two integers back which I can serialize in a binary compatible way.

The lamest method is:

Save the integer part

TmpInt = (Faction * 1000000000)

Save the fraction part

This is limiting.. The precision of the double can be more precise then this. So whatever method I choose, there is a precision error. The string conversion is still the most precise as you can specify how many digits you would like.

Any ideas anyone?

- Jorgen

I am busy with a rewrite from my old serializer (SIO) to wxArchive, which will be the counter part of MFC's CArchive. I want to store all values as x-platform as possible meaning binary compatible so that serialized files can be opened on linux, MAC and Windows without binary incompatibility.

For a double, this is not so easy. One solution is convert to a string and store the double, another solution is convert it to two long or int64 values, there is a function that splits up the fraction and integer part.

However, with a splitted double I get back for example:

243.1434546784578

Integer: 243

Fraction: 0.1434546784578

Where I rather have two integers back which I can serialize in a binary compatible way.

The lamest method is:

Save the integer part

TmpInt = (Faction * 1000000000)

Save the fraction part

This is limiting.. The precision of the double can be more precise then this. So whatever method I choose, there is a precision error. The string conversion is still the most precise as you can specify how many digits you would like.

Any ideas anyone?

- Jorgen