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 variable.

Example:

  ; This program adds an integer to a BCD number.
  import "ombcd.xmd" unprefixed
  process
      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)
  • ** (raise a number to a specified power)

For operations with BCD numbers, import ombcd into your program using a statement like this:

  import "ombcd.xmd" unprefixed

For operations with floating point numbers, import omfloat into your program using a statement like this:

  import "omfloat.xmd" unprefixed

You can import both the BCD and floating point modules 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.


  ; This program adds mixed data types.
  import "ombcd.xmd" unprefixed
  process
      local integer red-ones initial {0}
      local integer blue-ones initial {0}
      local integer total initial {0}
  
      local bcd price
      local bcd tax
      local bcd total-price
  
      local integer foo initial {7}
      local bcd bar 
  
      set red-ones to 43 + 967
      set blue-ones to 4 + 7
      set total to red-ones + blue-ones
  
      set price to bcd 29 + bcd 0.99
      set tax to bcd 0.076 + bcd 2.873
      set total-price to price + tax
  
      set bar to bcd foo + 12
      

There are boundary conditions limiting operations on mixed data types. For example, if you add two integers totalling more than the largest possible 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.