# Operations using numeric values Foundation

There are more operators available for use with values of "numeric" data type than for any other data type. The precedence of different operators is especially important with complex numeric expressions using different operators. The two unary operators increment and decrement are unique in that the precedence is dependent on whether they appear in prefix and postfix notation (see the chapter "Operators"). The following table shows a complete list of all operators that can be used with numeric values:

Operator | Description |
---|---|

+ | Positive prefix (unary) |

- | Negative prefix (unary) |

++ | Increment (unary) |

-- | Decrement (unary) |

** ^ | Exponentiation |

% | Modulus (remainder of division) |

* | Multiplication |

/ | Division |

+ | Addition (binary) |

- | Subtraction (binary) |

== | Comparison: equal |

!= <> # | Comparison: not equal |

> | Comparison: greater than |

>= | Comparison: greater than or equal |

= | Comparison: equal |

<= | Comparison: less than or equal |

< | Comparison: less than |

= | Assignment |

:= | Inline assignment |

**= ^= | Inline exponentiation |

%= | Inline modulus |

*= | Inline multiplication |

/= | Inline division |

+= | Inline addition |

-= | Inline subtraction |

The precedence of the mathematical operators plays an important role in how complex expressions are evaluated and affect the results of the expression. Expression segments are evaluated in the order determined by the precedence rules. The order of the evaluation can be explicitly set using parentheses.

In the first line of this example, the results of the division and multiplication segments are calculated first due to operator precedence and then addition and subtraction are performed. In the second line addition and subtraction receive higher precedence because of the parentheses and are executed prior to the division and multiplication. Using parentheses to specify the order of evaluation in complex expression is recommended. This increases program reliability and readability.

Unlike character value comparisons, the comparison of numeric values is not dependent on the setting SET EXACT. The settings that are significant for numeric values are SET FIXED and SET DECIMALS. These settings determine the default format used to display numeric values (see the reference documentation for the two commands). The country settings are used to determine whether a decimal point or decimal comma will be used when numeric values are displayed.

The functions of the Xbase++ runtime library that perform operations on numeric values, are listed in the following tables.

Function | Description |
---|---|

Abs() | Calculates absolute value of a number |

Chr() | Converts number to ASCII character |

Descend() | Converts numeric value for descending sorting |

Empty() | Tests if numeric value is equal to 0 |

Exp() | Calculates power of the number e (2,71817..) |

FieldPut() | Assigns numeric value to field variable |

FieldGet() | Reads numeric value from field variable |

Int() | Converts floating point number to integer |

Log() | Calculates natural logarithm |

Max() | Determines greater of two numeric values |

Min() | Determines lesser of two numeric values |

Mod() | Calculates modulus (dBase compatible) |

Round() | Rounds numeric value |

Sqrt() | Calculates square root |

Str() | Converts numeric value to character string |

Transform() | Converts numeric value to formatted character string |

Type() | Determines data type using macro operator |

Valtype() | Determines data type |

Function | Description |
---|---|

I2bin() | Convert integer with prefix to 16 bit binary number |

L2bin() | Convert long integer with prefix to 32 bit binary number |

U2bin() | Convert long integer without prefix to 32 bit binary number |

W2bin() | Covert integer without prefix to 16 bit binary number |

Bin2i() | Convert 16 bit binary number to integer with prefix |

Bin2l() | Convert 32 bit binary number to long integer with prefix |

Bin2w() | Convert 16 bit binary number to integer without prefix |

Bin2u() | Convert 32 bit binary number to long integer without prefix |

Short examples of calling the most common functions for numeric values are shown below:

Mathematical functions

Numeric functions

Convert numeric values

Other functions for numeric values

Xbase++ provides eight functions that convert numeric values to binary numbers and binary numbers to numeric values. Binary numbers consist of sets of two or four ASCII characters which represent a number. A 16 bit binary number consists of two characters and a 32 bit binary number has four characters (1 character=8 bit).

When converting numeric values to binary numbers the signed prefix fills one bit of the binary representation. Because of this, the range of valid numbers differs for the various conversion functions.

The conversion of a binary number to a numeric value using Bin2u() and Bin2l() or Bin2i() and Bin2w() can provide different results for the same binary number.

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.