@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 2,147,483,647 digits in Windows x64. (Windows x86 will be limited by memory to much less). 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.
Trigonometric and transcendental functions
Precision of internal calculations
Display precision and output format
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-RT 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-RT 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:
echo %@eval[1.4567e+4*7.6541e+2=E]
You can combine =E with a display precision (see below):
echo %@eval[1.4567e+4*7.6541e+2=E1.20]
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.
For example:
[c:\] echo %@eval[0x10 + 16]
32
You can specify hexadecimal output with the special syntax @eval[...=H]. For example:
echo %@eval[3*6=H]
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:
echo %@eval[3*6=B]
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 |
* | multiplication |
/ | division |
** | exponentiation |
! | boolean not |
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 |
> | greater than |
< | less than |
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 |
~ | unary NOT |
Trigonometric and transcendental functions
Expression may include the trigonometric and transcendental functions below. The argument is interpreted as radians.
log(x) | natural logarithm |
log2(x) | binary logarithm |
log10(x) | log 10 |
exp(x) | exponential |
sin(x) | sine |
asin(x) | arcsine |
sinh(x) | hyperbolic sine |
cos(x) | cosine |
acos(x) | arccosine |
cosh(x) | hyperbolic cosine |
tan(x) | tangent |
atan(x) | arctangent |
tanh(x) | hyperbolic tangent |
The special string PI is a shortcut for the value 3.14159265358979323846.
abs(x) absolute value
ceil(x) ceiling
fact(x) factorial
floor(x) floor
gcd(x y) greatest common divisor (max 64-bit integer)
lcm(x y) least common multiple (max 64-bit integer)
ror(x y z) rotate x right y bits with a variable size of z (in bits) (max 64-bit integer)
rol(x y z) rotate x left y bits with a variable size of z (in bits) (max 64-bit integer)
1. | variables |
2. | expressions in matching parentheses |
3. | functions listed in Trigonometric and transcendental functions |
4. | exponentiation |
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.
Precision of internal calculations
@EVAL supports numbers up to 30,000 digits; it is highly unlikely you'll need greater precision than this! A few functions (gcd, lcm, ror, rol) use 64-bit integers.
Display precision and output format
The maximum display precision is 15,000 digits to the left of the decimal point and 15,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:
echo %@eval[1.4567e+4*7.6541e+2=E]
You can combine =E with a display precision (see below):
echo %@eval[1.4567e+4*7.6541e+2=E1.15]
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.
Explicit precision
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.
If the last character of the displayformat is +, @EVAL will prefix positive numbers with a +.
Expression |
Value |
@eval[3 / 6=2.4] |
0.50 |
@eval[3 / 6=4.4] |
0.5000 |
@eval[3 / 7] |
0.4285714286 |
@eval[3 / 7=.4] |
0.4286 |
@eval[3 / 7=2.2] |
0.42 |
@eval[3 / 7=2] |
0.42 |
@eval[3 / 7=2+] |
+0.42 |