Operations on mixed data types

You can use any arithmetic or comparison operator on mixed data types (either BCD or floating point with integer).

Mixing data types is possible because the arithmetic and comparison operators are overloaded. This means that the operations are performed on a single data type and all other data types are cast to that type.

To add a BCD to an integer, you must specify a BCD shelf item. The following program adds an integer to a BCD:

  import "ombcd.xmd" unprefixed
      local integer eggs          initial { 12 }
      local bcd     powdered-eggs initial { 33.5 }
      local bcd     egg-total 
      set egg-total to eggs + powdered-eggs
      output "Total egg equivalents: " || "d" % egg-total ||  "%n"

OmniMark automatically coerces the integer eggs to a BCD before adding it to powdered-eggs.

You can use the following operators only with BCD or floating point numbers:

  • sqrt (calculate the square root of a number),
  • exp (raise e to any power, or find a number from its natural logarithm),
  • ln (calculate the natural logarithm),
  • log10 (calculate the base 10 logarithm), and
  • ** (raise a number to a specified power).

For operations with BCD numbers, import OMBCD into your program using an import declaration such as:

  import "ombcd.xmd" unprefixed

For operations with floating point numbers, import OMFLOAT into your program using an import declaration such as:

  import "omfloat.xmd" unprefixed

You can import both the BCD and floating point libraries in the same program. The math operations are overloaded so that the right operation is selected for use. Where conflicts could occur, different names have been used in the two modules.

There are boundary conditions limiting operations on mixed data types. For example, if you add two integers totalling more than the largest possible value for an integer, and set their result to a BCD in hopes of accommodating the larger total, the operation will fail. The integers get added together as integers first. This operation fails before the total can be cast as a BCD.