@EVAL[expression[=displayformat]]: Evaluates a mathematical expression and returns its value in the format specified by displayformat or in the default format. Parameter Interpretation below describes what expression may contain. Display precision and output format below explains the result format.
The expression can contain environment variables and other variable functions, and may use any of the operators listed below. @EVAL also supports parentheses (to control evaluation order), commas, hexadecimals and decimal separators. Parentheses can be nested. @EVAL will strip leading and trailing zeros from the result unless you use the output formatting operators.
@EVAL supports very large numbers. The maximum size is 30,000 digits (15,000 digits to the left of the decimal point and 15,000 decimal places). If you want to use more than the default decimal values you'll need to change your @Eval Precision configuration options or use the "=x.y" format in @EVAL. The integer-only operators (AND, OR, and XOR) are limited to 64-bit integers.
Expression may contain environment and internal variables, array variables, and variable functions. After all variables and functions have been expanded, it must be composed only of numeric strings and names of functions in Trigonometric and transcendental functions or Other functions, connected by Arithmetic operators and optionally grouped with parentheses.
@EVAL permits you to simplify expression by dropping the % percent mark in front of the names of environment variables. This also prevents the TCC parser from expanding (possibly erroneously) variables before passing them to @EVAL. You must include % for internal variables and variable functions. @EVAL also permits you to use characters which normally have special meaning for TCC e.g., & < > ^ | without disabling their special meaning or quoting them.
Note: To ensure that expression is interpreted correctly, spaces should be placed on both sides of each operator, and parentheses used liberally. For example:
%@eval[(20 %% 3) + 4]
%@eval[12 and 65]
@EVAL accepts numbers in the scientific notation exponent syntax; i.e. 1575e-2 = 15.75. You can specify scientific notation output with the syntax @eval[...=E]. For example:
You can combine =E with a display precision (see below):
If a string starts with the characters 0x it is interpreted as an integer in hexadecimal notation. If a string starts with the characters 0b it is interpreted as an integer in binary notation. Any other numeric string is considered to be a decimal number.
[c:\] echo %@eval[0x10 + 16]
You can specify hexadecimal output with the special syntax @eval[...=H]. For example:
will output 12 (hex). No leading 0x is included in the output. To convert between decimal and hexadecimal formats, see the @CONVERT function.
You can specify binary output with the special syntax @eval[...=B]. For example:
Hex and binary output is limited to 64-bit (signed) integers.
Every operator accepts both integer and non-integer parameters, except as noted below.
Operators accepting fractional parameters
|+||(with one parameter) sign of numeric parameter (e.g. +3)|
|+||(with two parameters) addition|
|-||(with one parameter) negation of symbolic parameter (e.g., -n) or sign of numeric parameter (e.g. -1, +3)|
|-||(with two parameters) subtraction|
Operators requiring integer parameters
|\||integer division (returns the integer part of the quotient)|
|MOD||modulo (returns the remainder when the first parameter is divided by the second)|
|%%||same as MOD|
|SHL||arithmetic left shift of the first parameter, truncated toward zero to an integer, by the number of bits specified by the second parameter|
|<<||same as SHL|
|SHR||arithmetic right shift of the first parameter, truncated toward zero to an integer, by the number of bits specified by the second parameter|
|>>||same as SHR|
Operators which truncate parameters to integer
|AND||bitwise and (returns 1 for each bit position where the corresponding bits in both parameters are 1)|
|&||same as AND|
|OR||bitwise or (returns 1 for each bit position where the corresponding bit in at least one parameter is 1)|
||||same as OR|
|XOR||bitwise exclusive or (returns 1 for each bit position where the corresponding bits of the two parameters are different)|
|^||same as XOR|
Expression may include the trigonometric and transcendental functions below. The argument is interpreted as radians.
The special string PI is a shortcut for the value 3.14159265358979323846.
abs(x) absolute value
gcd(x y) greatest common divisor
lcm(x y) least common multiple
ror(x y z) rotate x right y bits with a variable size of z (in bits)
rol(x y z) rotate x left y bits with a variable size of z (in bits)
|2.||expressions in matching parentheses|
|3.||functions listed in Trigonometric and transcendental functions|
|5.||multiplication, division, and MOD|
|6.||addition and subtraction|
|7.||>, <, AND, OR, XOR, NOT, SHL, and SHR|
When multiple consecutive expressions of a single precedence level are used, evaluation is left to right.
For example, 3 + 4 * 2 will be interpreted as 3 + 8, not as 7 * 2. To change this order of evaluation, use parentheses to specify the order you want.
@EVAL supports numbers up to 20,000 digits; it is highly unlikely you'll need greater precision than this!
The maximum display precision is 10,000 digits to the left of the decimal point and 10,000 digits to the right. You can alter the default decimal precision with the OPTION command, the @EVAL Precision configuration options, and with the SETDOS /F command. You can change the decimal separator with the decimal character configuration option or the SETDOS /G command.
You can alter the display format for the current instance of @EVAL by specifying displayformat.
Scientific notation display format
If displayformat is E, output will be in scientific notation. For example:
You can combine =E with a display precision (see below):
Hexadecimal display format
If displayformat is the letter H, output will be hexadecimal. If displayformat is X, the output will be hexadecimal with a leading 0x.
Binary display format
If displayformat is the letter B, output will be binary.
If displayformat is i.a, then:
•i must be a number which specifies the minimum decimal precision (the minimum number of decimal places displayed);
•a must be a number which sets the maximum decimal precision.
•the character separating i and a may be the comma if it is your decimal separator
You may specify either or both parameters i and a. If i >a, or if only i is specified, i is used as both the minimum and maximum precision, e.g. both =2 and =2.1 are equivalent to =2.2.
@eval[3 / 6=2.4]
@eval[3 / 6=4.4]
@eval[3 / 7]
@eval[3 / 7=.4]
@eval[3 / 7=2.2]
@eval[3 / 7=2]