Boost logo

Boost :

From: Martin (adrianm_at_[hidden])
Date: 2005-06-21 02:24:22


> Does the traits class handle rounding?
yes, for each object the rounding mode can be specified including one that is
called round_global. round_global means that the rounding is determined from a
static variable of the traits class or an application wide setting. What
rounding modes are available is of course dependent on the arithmetic type.
e.g. For the TR decimal class only round_global is available while decnumber
supports both object rounding and global rounding with different traits. (The
former is not very efficient since it requires each object to carry a
decContext structure)

The basic_money class has the following constructors:
money m(V); // floating point, global rounding
money m(V, round_to_even); // floating point, local rounding
money m(V, decimals(2)); // fixed decimals, global rounding
money m(V, digits(8)); // fixed digits, global rounding
money m(V, decimals(2), round_to_nearest);
money m(V, digits(2), round_to_even);

V can be int, double, char*, decimal, money

> In my attempt at a money class, I have one extra template parameter, which
> represents the number of digits past the decimal point. I figured this is
> fundamental enough to be included in the type.

I use different classes were you can lock precision and/or rounding
basic_money<decimal64> digits/decimals decided on object creation
basic_money_spec<decimal64, digits, 8> all objects will work with 2 digits
basic_money_spec<decimal64, decimals, 2> all objects will work with 2
decimals
basic_money_round<decimal64, decimals, 5, round_nearest>

> Nice. This seems similar in direction to Daniel Frey's money classes, which
> has the weight of actual company usage behind it.

yes, I have only generelized his solution a bit (e.g. the currency id type is
a template parameter).

>
> The only drawback is that the currency units is not part of the currency
> type, but rather something checked at runtime via exceptions. What is the
> rationale behind that? From the outside, it looks safer to catch this
> at compile time, but I suppose usage of these classes is easier if it is
> a value? I'd like to be convinced your method is the best.

My plan is to have an extra class with template parameter for type safe
currencies.
Something like
typedef basic_money<decimal> tMoney;
typedef basic_currency_fixed<tMoney, 1> tUSD;
typedef basic_currenct_fixed<tMoney, 2> tEUR;

tMoney a(3);
tUSD b(4);
tEUR c(5);
b = c; // compile error
b = a; // ok

but I haven't tried it yet so I don't know how feasible and useful it is.
 
>
> Is the latest code available somewhere?

No, It is still just experimental.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk