Send feedback on this topic.
Teradata.Client.Provider
Arithmetic Operations
.NET Data Provider for Teradata > Developer's Guide > Data Types > Numeric Data Types > Arithmetic Operations

The Numeric Provider Specific Types supports all arithmetic operators.

However, there are potential issues when performing an arithmetic operation between two numbers represented as TdDecimals. The issues are discussed in the following sections.

TdDecimal General Issues With Arithmetic Operations

Adjusting the Scale of operands

When performing an arithmetic operation, the scale between the operands are adjusted to be the same.  For example, if the operands have a precision and scale of (4, 3) and (10, 2) an adjustment is made such that the second operand will have a precision and scale of (11, 3).  Adjusting the scale also affects the precision.   Because the precision is also affected when adjusting the scale, there is a possibility that when an operand is adjusted, its precision will be greater than 38.  When this occurs an OverflowException will be thrown.  For example, consider the following add operation

    0.349854785012893247809213589231 + 3249230848.2341

The first operand has a precision and scale of (30, 30) and the second (14, 4). When the scale of the second operand is adjusted to match the first, its precision and scale will be (40, 30). In this case, an OverflowException will get thrown.

Result will have a Precision greater than 38

When performing an Add or Multiply operation the precision of the result may increase as compared to the operands. If the precision of the result will be greater than 38, an OverflowException will get thrown. For example consider the following add operation:

    0.52139806784690123423893457123645907892 + 0.72329006781209123423893457123645967129

Both operands have a precision and scale of (38, 38). The result will have a precision and scale of (39, 38). An OverflowException will be thrown in this case.

Divide Operation

When a divide operation is performed the result will have the maximum scale between the two operands. Therefore if the two operands have a scale of 4 and 6, the result will have a scale of 6. If more scale is needed in the result, the scale of one of the operands must be adjusted up by calling TdDecimal.AdjustScale. For example, to adjust the scale of the second operand by 5, AdjustScale is called as follows:

    TdDecimal.AdjustScale(operand2, 5, true)

The new scale of the second operand will be 11, and the result of the operation will have a scale of 11.

Another example is performing the divide operation: 5 / 10. Since both operands have a scale of 0, the result will be 0. If the scale of the first or second operand were adjusted up by 1, the result will be .5