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.
? 8 + 20 / 4 - 2 * 3 // result: 7
? (8 + 20) / (4 - 2) * 3 // result: 42
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
? Exp(1) // result: 2.72
? Log(10) // result: 2.30
? Sqrt(36) // result: 6.00
Numeric functions
? Abs(-9876) // result: 9876
? Int(987.654) // result: 987
? Max(98, 76) // result: 98
? Min(98, 76) // result: 76
? Round(987.654, 1) // result: 987.7
Convert numeric values
? Chr(65) // result: A
? Str(987.65, 4, 1) // result: 987.6
? Transform(9876.54, "9,999.9") // result: 9,876.5
Other functions for numeric values
? Empty(0) // result: .T.
? Type("1") // result: N
? Valtype(1) // result: N
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).
? "|"+ I2bin(65) +"|" // result: |A |
? Bin2i("A"+Chr(0)) // result: 65
? Valtype(65) // result: N
? Valtype(I2bin(65)) // result: C
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.
I2bin() - Bin2i() -32768 to +32767
W2bin() - Bin2w() 0 to 65535
L2bin() - Bin2l() -2147483648 to +2147483647
U2bin() - Bin2u() 0 to 4294967295
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.