BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdldfp_decimalimputil

Detailed Description

Outline

Purpose

Provide a unified low-level interface for decimal floating point.

Classes

See also
bdldfp_decimalimputil_inteldfp

Description

This component provides a namespace, bdldfp::DecimalImpUtil, containing primitive utilities used in the implementation of a decimal floating point type (e.g., see bdldfp_decimal ).

Usage

This section shows the intended use of this component.

Example 1: Constructing a Representation of a Value in Decimal

A common requirement for decimal floating point types is to be able to create a value from independent "coefficient" and "exponent" values, where the resulting decimal has the value coefficient * 10 ^ exponent. In the following example we use such a coefficient and exponent to create Decimal32, Decimal64, and Decimal128 values.

First we define values representing the coefficient and exponent (note the result should be the value 42.5):

int coefficient = 425; // Yet another name for significand
int exponent = -1;

Then we call makeDecimal32, makeDecimal64, and makeDecimal128 to construct a Decimal32, Decimal64, and Decimal128 respectively.

static ValueType128 makeDecimalRaw128(unsigned long long int significand, int exponent)
static ValueType32 binaryToDecimal32(float value)
Definition bdldfp_decimalimputil.h:2906
Imp::ValueType64 ValueType64
Definition bdldfp_decimalimputil.h:250
Imp::ValueType128 ValueType128
Definition bdldfp_decimalimputil.h:251
static ValueType32 makeDecimalRaw32(int significand, int exponent)
Definition bdldfp_decimalimputil.h:2950
static ValueType64 binaryToDecimal64(float value)
Definition bdldfp_decimalimputil.h:2920
static bool equal(ValueType32 lhs, ValueType32 rhs)
Definition bdldfp_decimalimputil.h:2810
static ValueType64 makeDecimalRaw64(unsigned long long int significand, int exponent)
static ValueType128 binaryToDecimal128(float value)
Definition bdldfp_decimalimputil.h:2934
Imp::ValueType32 ValueType32
Definition bdldfp_decimalimputil.h:249

Example 2: Adding Two Decimal Floating Point Values

Decimal floating point values are frequently used in arithmetic computations where the precise representation of decimal values is of paramount importance (for example, financial calculations, as currency is typically denominated in base-10 decimal values). In the following example we demonstrate computing the sum of a sequence of security prices, where each price is held in a DecimalImpUtil::ValueType64 value.

First, we define the signature of a function that computes the sum of an array of security prices, and returns that sum as a decimal floating point value:

totalSecurities(bdldfp::DecimalImpUtil::ValueType64 *prices,
int numPrices)
// Return a Decimal Floating Point number representing the arithmetic
// total of the values specified by 'prices' and 'numPrices'.
{

Then, we create a local variable to hold the intermediate sum, and set it to 0:

static ValueType64 int32ToDecimal64(int value)
Definition bdldfp_decimalimputil.h:1536

Next, we loop over the array of prices and add each price to the intermediate total:

for (int i = 0; i < numPrices; ++i) {
total = bdldfp::DecimalImpUtil::add(total, prices[i]);
}
static ValueType32 add(ValueType32 lhs, ValueType32 rhs)
Definition bdldfp_decimalimputil.h:1619

Now, we return the computed total value of the securities:

return total;
}

Notice that add is called as a function, and is not an operator overload for +; this is because the bdldfp::DecimalImpUtil utility is intended to be used in the implementation of operator overloads on a more full fledged type.

Finally, we call the function with some sample data, and check the result:

for (int i = 0; i < 16; ++i) {
}
result = totalSecurities(data, 16);
// Totals of values from 1 to 'x' are '(x * x + x) / 2':
bdldfp::DecimalImpUtil::multiply(expected, expected),
expected);
expected,
assert(bdldfp::DecimalImpUtil::equal(expected, result));
static ValueType32 multiply(ValueType32 lhs, ValueType32 rhs)
Definition bdldfp_decimalimputil.h:1670
static ValueType32 divide(ValueType32 lhs, ValueType32 rhs)
Definition bdldfp_decimalimputil.h:1698

Notice that arithmetic is unwieldy and hard to visualize. This is by design, as the DecimalImpUtil and subordinate components are not intended for public consumption, or direct use in decimal arithmetic.