Boost logo

Boost :

From: elias_at_[hidden]
Date: 2003-07-16 20:27:58


I am very interested in the library as I am working in its
target field (i.e. financial applications).
Reviewing it though, I found that it does not really
support the scenarios I consider important.

IMHO in a financial application (please note below a
code example) there are two separate issues that require
two different approaches to implementation:

I1. Storage and representation of decimals, where specific
    scale is desired and can/should be specified up-front.
    Also desired is smaller size for these objects.

I2. Calculation, where maximum precision is desired
    [And then speed, of course]

I'm afraid the present implementation does not do well on either
of these issues. The particular problems I see:

P1. Storage: far from optimal. To represent 18 significant decimal
    digits the implementation would typically require 20 bytes
    [where 8 should be enough].
    Also: to specify scale on a per-type basis, I would have to
    inherit from this class and re-code all needed constructors
    and assignment and conversion operators.

P2. Calculations: precision is limited by same 18 significant
    digits, and (main point) the rounding occurs *in process*
    of calculations.

Please see below a code example. I'd love to hear everyone's take
on the issues I described.

Thanks & Best Regards.

Ilya Buchkin
MetaCommunications Engineering
mailto:elias_at_[hidden]

PS
/////////////////////////// Code example: //////////////////////////

namespace my_accounting_application {

// this is how I want to STORE the data -
// in the database, and in the program when not doing any calculations,
// specifying the scale according to application, and minimizing space:

// (type parameterized by scale and rounding policy)
typedef boost::decimal<2,round_up> t_decimal_price;
typedef boost::decimal<5> t_decimal_unit_price;
typedef boost::decimal<10> t_decimal_quantity;
typedef boost::decimal<10> t_decimal_tax_rate;

struct t_invoice_item
    {
    std::string description_;
    t_decimal_unit_price item_price_; // e.g. 0.0625 ($ per
each)
    t_decimal_quantity quantity_;
    bool is_taxable_;
    };

struct t_invoice
    {
    t_customer* customer_;
    std::vector<t_invoice_item> invoice_items_; // 100 items maybe
    t_decimal_price subtotal_; // e.g. 12345.55 ($)
    t_decimal_price tax_; // e.g. 1064.80 ($)
    };

struct t_customer
    {
    std::string name_;
    t_decimal_tax_rate tax_rate_; // e.g. 8.625 (%)
    };

// ...this is how I would like to code CALCULATIONS:

void recalc_totals( std::vector<t_invoice*> a_invoices )
    {
    for_each ( a_invoices, inv ) // (simplified iteration notation)
        {
        boost::max_precision_decimal tax = 0; // accumulate with max
possible precision, please!
        inv->subtotal_ = 0;
        for_each ( inv->invoice_items_, item ) // (simplified iteration...)
            {
            boost::max_precision_decimal item_total = item->quantity_ *
item->item_price_;
            inv->subtotal_ += item_total;
            if ( item->is_taxable_ )
                tax += inv->customer_->tax_rate_ * item_total;
            }
        inv->tax_ = tax; // round it here
        }
    }

};

/////////////////////////// end code example
/////////////////////////////////


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