Skip to main content
Version: v20

Number (Real, Longint, Integer)

Number is a generic term that stands for:

  • Real field, variable or expression. The range for the Real data type is ±1.7e±308 (13 significant digits).
  • Long Integer field, variable or expression. The range for the Long Integer data type (4-byte Integer) is -2^31..(2^31)-1.
  • Integer field, array or expression. The range for the Integer data type (2-byte Integer) is -32,768..32,767 (2^15..(2^15)-1).

Note: Integer field values are automatically converted in Long integers when used in the 4D Language.

You can assign any Number data type to another; 4D does the conversion, truncating or rounding if necessary. However, when values are out of range, the conversion will not return a valid value. You can mix Number data types in expressions.

Note: In the 4D Language Reference manual, no matter the actual data type, the Real, Integer, and Long Integer parameters in command descriptions are denoted as number, except when marked otherwise.

Number literals

A numeric literal constant is written as a real number. Here are some examples of numeric constants:

27
123.76
0.0076

The default decimal separator is a period (.), regardless of the system language. If you have checked the "Use regional system settings" option in the Methods Page of the Preferences, you must use the separator defined in your system.

Negative numbers are specified with the minus sign (-). For example:

-27
-123.76
-0.0076

Number operators

OperationSyntaxReturnsExpressionValue
AdditionNumber + NumberNumber2 + 35
SubtractionNumber - NumberNumber3 – 21
MultiplicationNumber * NumberNumber5 * 210
DivisionNumber / NumberNumber5 / 22.5
Longint divisionNumber \ NumberNumber5 \ 22
ModuloNumber % NumberNumber5 % 21
ExponentiationNumber ^ NumberNumber2 ^ 38
EqualityNumber = NumberBoolean10 = 10True
10 = 11False
InequalityNumber # NumberBoolean10 #11True
10 # 10False
Greater thanNumber > NumberBoolean11 > 10True
10 > 11False
Less thanNumber < NumberBoolean10 < 11True
11 < 10False
Greater than or equal toNumber >= NumberBoolean11 >= 10True
10 >= 11False
Less than or equal toNumber <= NumberBoolean10 <= 11True
11 <= 10False

Modulo

The modulo operator % divides the first number by the second number and returns a whole number remainder. Here are some examples:

  • 10 % 2 returns 0 because 10 is evenly divided by 2.
  • 10 % 3 returns 1 because the remainder is 1.
  • 10.5 % 2 returns 0 because the remainder is not a whole number.
danger

The modulo operator % returns significant values with numbers that are in the Long Integer range (from minus 2^31 to 2^31 minus one). To calculate the modulo with numbers outside of this range, use the [Mod(https://doc.4d.com/4dv20/help/command/en/page98.html)] command.

Longint division

The longint division operator \ returns significant values with integer numbers only.

Real comparison

To compare two reals for equality, the 4D language actually compares the absolute value of the difference with epsilon. See the SET REAL COMPARISON LEVEL command.

note

For consistency, the 4D database engine always compares database fields of the real type using a 10^-6 value for epsilon and does not take the SET REAL COMPARISON LEVEL setting into account.

Precedence

The order in which an expression is evaluated is called precedence. 4D has a strict left-to-right precedence, in which algebraic order is not observed. For example:

 3+4*5

returns 35, because the expression is evaluated as 3 + 4, yielding 7, which is then multiplied by 5, with the final result of 35.

To override the left-to-right precedence, you MUST use parentheses. For example:

 3+(4*5)

returns 23 because the expression (4 * 5) is evaluated first, because of the parentheses. The result is 20, which is then added to 3 for the final result of 23.

Parentheses can be nested inside other sets of parentheses. Be sure that each left parenthesis has a matching right parenthesis to ensure proper evaluation of expressions. Lack of, or incorrect use of parentheses can cause unexpected results or invalid expressions. Furthermore, if you intend to compile your applications, you must have matching parentheses—the compiler detects a missing parenthesis as a syntax error.

Bitwise operators

The bitwise operators operates on Long Integer expressions or values.

If you pass an Integer or a Real value to a bitwise operator, 4D evaluates the value as a Long Integer value before calculating the expression that uses the bitwise operator.

While using the bitwise operators, you must think about a Long Integer value as an array of 32 bits. The bits are numbered from 0 to 31, from right to left.

Because each bit can equal 0 or 1, you can also think about a Long Integer value as a value where you can store 32 Boolean values. A bit equal to 1 means True and a bit equal to 0 means False.

An expression that uses a bitwise operator returns a Long Integer value, except for the Bit Test operator, where the expression returns a Boolean value. The following table lists the bitwise operators and their syntax:

OperationOperatorSyntaxReturns
Bitwise AND&Long & LongLong
Bitwise OR (inclusive)|Long | LongLong
Bitwise OR (exclusive)^ |Long ^ | LongLong
Left Bit Shift<<Long << LongLong (see note 1)
Right Bit Shift>>Long >> LongLong (see note 1)
Bit Set?+Long ?+ LongLong (see note 2)
Bit Clear?-Long ?- LongLong (see note 2)
Bit Test??Long ?? LongBoolean (see note 2)

Notes

  1. For the Left Bit Shift and Right Bit Shift operations, the second operand indicates the number of positions by which the bits of the first operand will be shifted in the resulting value. Therefore, this second operand should be between 0 and 31. Note however, that shifting by 0 returns an unchanged value and shifting by more than 31 bits returns 0x00000000 because all the bits are lost. If you pass another value as second operand, the result is non-significant.
  2. For the Bit Set, Bit Clear and Bit Test operations , the second operand indicates the number of the bit on which to act. Therefore, this second operand must be between 0 and 31; otherwise, the result of the expression is non-significant.

The following table lists the bitwise operators and their effects:

OperationDescription
Bitwise ANDEach resulting bit is the logical AND of the bits in the two operands. Here is the logical AND table:
  • 1 & 1 --> 1
  • 0 & 1 --> 0
  • 1 & 0 --> 0
  • 0 & 0 --> 0
  • In other words, the resulting bit is 1 if the two operand bits are 1; otherwise the resulting bit is 0.
    Bitwise OR (inclusive)Each resulting bit is the logical OR of the bits in the two operands.Here is the logical OR table:
  • 1 | 1 --> 1
  • 0 | 1 --> 1
  • 1 | 0 --> 1
  • 0 | 0 --> 0
  • In other words, the resulting bit is 1 if at least one of the two operand bits is 1; otherwise the resulting bit is 0.
    Bitwise OR (exclusive)Each resulting bit is the logical XOR of the bits in the two operands.Here is the logical XOR table:
  • 1 ^ | 1 --> 0
  • 0 ^ | 1 --> 1
  • 1 ^ | 0 --> 1
  • 0 ^ | 0 --> 0
  • In other words, the resulting bit is 1 if only one of the two operand bits is 1; otherwise the resulting bit is 0.
    Left Bit ShiftThe resulting value is set to the first operand value, then the resulting bits are shifted to the left by the number of positions indicated by the second operand. The bits on the left are lost and the new bits on the right are set to 0. Note: Taking into account only positive values, shifting to the left by N bits is the same as multiplying by 2^N.
    Right Bit ShiftThe resulting value is set to the first operand value, then the resulting bits are shifted to the right by the number of position indicated by the second operand. The bits on the right are lost and the new bits on the left are set to 0.Note: Taking into account only positive values, shifting to the right by N bits is the same as dividing by 2^N.
    Bit SetThe resulting value is set to the first operand value, then the resulting bit, whose number is indicated by the second operand, is set to 1. The other bits are left unchanged.
    Bit ClearThe resulting value is set to the first operand value, then the resulting bit, whose number is indicated by the second operand, is set to 0. The other bits are left unchanged.
    Bit TestReturns True if, in the first operand, the bit whose number is indicated by the second operand is equal to 1. Returns False if, in the first operand, the bit whose number is indicated by the second operand is equal to 0.

    Examples

    OperationExampleResult
    Bitwise AND0x0000FFFF & 0xFF00FF000x0000FF00
    Bitwise OR (inclusive)0x0000FFFF | 0xFF00FF000xFF00FFFF
    Bitwise OR (exclusive)0x0000FFFF ^ | 0xFF00FF000xFF0000FF
    Left Bit Shift0x0000FFFF << 80x00FFFF00
    Right Bit Shift0x0000FFFF >> 80x000000FF
    Bit Set0x00000000 ?+ 160x00010000
    Bit Clear0x00010000 ?- 160x00000000
    Bit Test0x00010000 ?? 16True