Accumulating very long integers

Platforms supporting the CommonPoint application system provide signed and unsigned integers at least 32 bits wide. The signed integers reach a magnitude over 2 billion, the unsigned integers twice that. You might find it necessary to accumulate even larger values using the class TDoubleLong. It supports signed 64-bit integers, with a range up to almost , or 10 quintillion. While insufficient to count the carbon atoms in a 60-carat diamond (that being Avogadro's number, over ), TDoubleLong values can represent long time periods (over 3 billion seconds per century), or money amounts in accounting applications, where rounding is subject to legal, not just mathematical, constraints.

Here is a program fragment to compute 7% of a monetary amount, rounding in three application-specific ways. It works for any unit of currency; tenths of a cent, or mills, are often used:

      TDoubleLong balance = ComputeCurrentAccountBalance();
      TDoubleLong sevenPercentOfBalance = (balance * 7) / 100;
      TDoubleLong remainder = (balance * 7) % 100; // 0 <= remainder < 100
      switch (roundingType) {
          kCurrencyTruncate: // integer arithmetic truncates by default
              break;
          kCurrencyRoundUp:
              if (remainder > 0)
                  sevenPercentOfBalance += 1;
              break;
          kCurrencyAddHalfAndTruncate:
              if (remainder >= 50)
                  sevenPercentOfBalance += 1;
      }
Note how int and TDoubleLong operands are mixed in operations.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker