BDE 4.14.0 Production release
|
Provide a unified low-level interface for decimal floating point.
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 ).
This section shows the intended use of this component.
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):
Then we call makeDecimal32
, makeDecimal64
, and makeDecimal128
to construct a Decimal32
, Decimal64
, and Decimal128
respectively.
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:
Then, we create a local variable to hold the intermediate sum, and set it to 0:
Next, we loop over the array of prices
and add each price to the intermediate total
:
Now, we return the computed total value of the securities:
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:
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.