Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get boost multiprecision number parts?

Say I have this number:

// bmp = boost::multiprecision
bmp::cpp_dec_float n("123456789.1234567891011121314");

its backend data are:

[0]  1         unsigned int
[1]  23456789  unsigned int
[2]  12345678  unsigned int
[3]  91011121  unsigned int
[4]  31400000  unsigned int
...  0
[15] 0         unsigned int

which is exactly what I want to get; unfortunately, I don't find a way to get either both parts of my number as bmp::int128_t --for instance--, or the underlying data of my number.

That is, I like something like this exists:

bmp::int128_t integerPart;
bmp::int128_t floatPart;
n.getParts(integerPart, floatPart);

or

auto&& data = n.data(); // which is actually private when using `cpp_dec_float`.

Anyway, does someone known how to do what I am trying to achieve?

For the record, I need this to express a big decimal number as a C# decimal for the sake of interoperability.

like image 927
mister why Avatar asked May 10 '13 11:05

mister why


1 Answers

From the boost documentation the back end is intentionally opaque as it may change at any time without warning. (Class template cpp_dec_float fulfils all of the requirements for a Backend type. Its members and non-member functions are deliberately not documented: these are considered implementation details that are subject to change.) from http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/ref/cpp_dec_ref.html

As far as I can see you have two choices here. You can look at the source code for the specific version of the backend class you're using, access it from your number using the backend method, and hope that the backend never changes (especially consider changes that only broke binary format and not compilation).

Alternately I would recommend you take the string representation of the cpp_dec_float and split it into the two parts yourself, using the string or char* constructors for the underlying integral types you're interested in.

like image 78
Mark B Avatar answered Oct 05 '22 23:10

Mark B