mercredi 3 octobre 2018

How to avoid scientific notation and set decimal places on Intel Decimal Floating-Point Math Library

I'm working on a little C++ project from scratch that manipulates money. I'm start using Intel Decimal Floating-Point Math Library v2.0u2 and after writing some code I see that numbers with more than 6 digits are presented with scientific notation. I read its documentation and I can't figure out how to change it. Example:

#include "bid_conf.h"
#include "bid_functions.h"
...
BID_UINT64 d = __bid64_from_int64(345, round, &flags);
BID_UINT64 e = __bid64_from_int64(10000, round, &flags)
BID_UINT64 f = __bid64_mul(d, e, round, &flags)
cout << f;   // shows 3584865303390364816
cout << __bid64_to_binary64(f, round, &flags); // shows 3.45e+06

I need to get: 345 * 10000 = 3450000.00 in order to save it on a database and show it on user interfaces.

Also how to set decimal places for floating-point values with rounding, I mean:

With 2 decimal places      Actual      Expected
2 / 16 =                   0.125       0.13
1 / 3  =                   0.33333...  0.33
12 + 2 =                   14          14.00
With 4 decimal places      Actual      Expected
2 / 16 =                   0.125       0.1250
1 / 3  =                   0.33333...  0.3333
12 + 2 =                   14          14.0000
13 / 7 =                   1.8571428   1.8571

Also as a comment every time I use __bid64_mul function I need to set variables using __bid64_from_int64 function to multiply, otherwise I always get zero as result, however for addition, subtraction and division there's no problems.

BID_UINT64 a = 345;
BID_UINT64 b = 1000;
BID_UINT64 c = __bid64_add(a, b, round, &flags);
cout << c; // shows 1345 ok

c = __bid64_sub(a, b, round, &flags);
cout << c; // shows 9223372036854776463 
cout << __bid64_abs(c); // shows 655
cout << __bid64_to_binary64(c, round, &flags); // shows -0

c = __bid64_div(a, b, round, &flags);
cout << c; // shows 3557843705622692185
cout << __bid64_to_binary64(c, round, &flags); // shows 0.345, I would like 0.35

c = __bid64_mul(a, b, round, &flags);
cout << c << endl; // shows 0
cout << __bid64_to_binary64(c, round, &flags); // shows 0

Any help will be really appreciate it. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire