Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages
mpexpr(TCL) mpexpr(TCL)

mpexpr - Evaluate an expression with multiple precision math

package require Mpexpr
mpexpr arg ?arg arg ...?
mpformat formatString ?arg arg ...?
global mp_precision

Mpexpr is based on Tcl's native expr command, and shares many similarities with expr. Mpexpr performs all of its calculations using an arbitrary precision math package.
Mpexpr concatenates arg's (adding separator spaces between them), evaluates the result as a Tcl expression, and returns the value. The operators permitted in Tcl expressions are a subset of the operators permitted in C expressions, and they have the same meaning and precedence as the corresponding C operators. Expressions almost always yield numeric results (integer or floating-point values). For example, the expression
mpexpr 8.2 + 6
evaluates to 14.2. Tcl expressions differ from C expressions in the way that operands are specified. Also, Tcl expressions support non-numeric operands and string comparisons.

A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands and operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x). If an operand does not have one of the integer formats given above, then it is treated as a floating-point number if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler (except that the ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in most installations). For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it).
Operands may be specified in any of the following ways:
As an numeric value, either integer or floating-point.
As a Tcl variable, using standard $ notation. The variable's value will be used as the operand.
As a string enclosed in double-quotes. The expression parser will perform backslash, variable, and command substitutions on the information between the quotes, and use the resulting value as the operand
As a string enclosed in braces. The characters between the open brace and matching close brace will be used as the operand without any substitutions.
As a Tcl command enclosed in brackets. The command will be executed and its result will be used as the operand.
As a mathematical function whose arguments have any of the above forms for operands, such as `` sin($x)''. See below for a list of defined functions.
Where substitutions occur above (e.g. inside quoted strings), they are performed by the expression processor. However, an additional layer of substitution may already have been performed by the command parser before the expression processor was called. As discussed below, it is usually best to enclose expressions in braces to prevent the command parser from performing substitutions on the contents.
For some examples of simple expressions, suppose the variable a has the value 3 and the variable b has the value 6. Then the command on the left side of each of the lines below will produce the value on the following line:
mpexpr 3.1 + $a
mpexpr 2 + "$a.$b"
mpexpr 4*[llength "6 2"]
mpexpr {{word one} < "word $a"}

The valid operators are listed below, grouped in decreasing order of precedence:
-  +  ~  !
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operands may be applied to string operands, and bit-wise NOT may be applied only to integers.
*  /  %
Multiply, divide, remainder. None of these operands may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor.
+  -
Add and subtract. Valid for any numeric operands.
<<  >>
Left and right shift. Valid for integer operands only. Integers in mpexpr are not limited to a machine word and do not use two's complement format. Therefore shifting will not include a sign bit.
<  >  <=  >=
Boolean less, greater, less than or equal, and greater than or equal. Each operator produces 1 if the condition is true, 0 otherwise. These operators may be applied to strings as well as numeric operands, in which case string comparison is used.
==  !=
Boolean equal and not equal. Each operator produces a zero/one result. Valid for all operand types.
Bit-wise AND. Valid for integer operands only.
Bit-wise exclusive OR. Valid for integer operands only.
Bit-wise OR. Valid for integer operands only.
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point).
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point).
If-then-else, as in C. If x evaluates to non-zero, then the result is the value of y. Otherwise the result is the value of z. The x operand must have a numeric value.
See the C manual for more details on the results produced by each operator. All of the binary operators group left-to-right within the same precedence level. For example, the command
mpexpr 4*2 < 7
returns 0.
The &&, ||, and ?: operators have ``lazy evaluation'', just as in C, which means that operands are not evaluated if they are not needed to determine the outcome. For example, in the command
mpexpr {$v ? [a] : [b]}
only one of [a] or [b] will actually be evaluated, depending on the value of $v. Note, however, that this is only true if the entire expression is enclosed in braces; otherwise the Tcl parser will evaluate both [a] and [b] before invoking the expr command.

Mpexpr supports the following mathematical functions in expressions. x and y are integer or floating point values; i, j and c are integer values;
Math functions compatible with expr:
Arc cosine of x.
Arc sine of x.
Arc tangent of x.
Arc tangent of x / y.
Least integral value greater than or equal to x.
Cosine of x.
Hyperbolic cosine of x.
Exponential function e ** x.
Greatest integral value less than or equal to x.
Remainder of x divided by y.
Euclidean distance of sqrt( x * x + y * y).
Natural logarithm of x.
Base-10 logarithm of x.
x raised to the y power.
Sine of x.
Hyperbolic sine of x.
Square root of x.
Tangent of x.
Hyperbolic tangent of x.
Returns the absolute value of x. x may be either integer or floating-point, and the result is returned in the same form.
If x is a floating value, returns x, otherwise converts x to floating and returns the converted value.
If x is an integer value, returns x, otherwise converts x to integer by truncation and returns the converted value.
If x is an integer value, returns x, otherwise converts x to integer by rounding and returns the converted value.
Additional mpexpr functions:
The yth root of x.
Remove all occurance of factory from number x.
Inverse of x modulo y.
Greatest common divisor of x and y.
Least common multiple of x and y.
Maximum of x and y.
Minimum of x and y.
Value of pi.
Fibonacci number of integer i.
Factorial of integer i.
Product of prime numbers up to integer i.
Lowest prime factor of integer i, trying count c times.
Integer root j of integer i.
Relatively prime of greatest common divisior of i divided by j.
Permutations of i taking j at a time: i ! / ( i - j ) !.
Combinations of i taking j at a time: i ! / ( j ! * ( i - j ) ! ) .
Return 0 if i is not prime, return 1 if i probably is prime. Test for primality count c times. The chance of a non-prime passing this test is less than (1/4)^count. For example, a count of 100 fails for only 1 in 10^60 numbers.
Return 1 if i and j are relatively prime to each other, 0 otherwise.

Computations are performed using arbitrary fixed and floating point values. Native machine values ( int, long, IEEE 754 floating point, etc. ) and instructions are not used. Conversion among internal representations for integer, floating-point, and string operands is done automatically as needed. For arithmetic computations, integers are used until some floating-point number is introduced, after which floating-point is used. For example,
mpexpr 5 / 4
returns 1, while
mpexpr 5 / 4.0
mpexpr 5 / ( [string length "abcd"] + 0.0 )
both return 1.25. Floating-point values are always returned with a ``.'' or an ``e'' so that they will not look like integer values. For example,
mpexpr 20.0/5.0
returns ``4.0'', not ``4''.
The global variable mp_precision determines the number of significant digits that are retained during evaluation. If mp_precision is unset then 17 digits of precision are used. The maximum value of mp_precision is 10000. Note that larger values for mp_precision will require increasingly longer execution times. Setting mp_precision to an illegal value will generate an error.

String values may be used as operands of the comparison operators, although the expression evaluator tries to do comparisons as integer or floating-point when it can. If one of the operands of a comparison is a string and the other has a numeric value, the numeric operand is converted back to a string using the C sprintf format specifier %d for integers and %g for floating-point values. For example, the commands
mpexpr {"0x03" > "2"} mpexpr {"0y" < "0x12"}
both return 1. The first comparison is done using integer comparison, and the second is done using string comparison after the second operand is converted to the string ``18''. Because of Tcl's tendency to treat values as numbers whenever possible, it isn't generally a good idea to use operators like == when you really want string comparison and the values of the operands could be arbitrary; it's better in these cases to use the string compare command instead.
mpformat formats a string in the style of Tcl's native format command. Mpformat will interpret numeric arguments as arbitrary precision numbers. Mpformat performs limited % substitution on the output string. The following may be specified:
% [-] [width[.precision]] formatChar
Specifies left justification; right justification is the default.
Specifies optional width and precision. Default precision is 8. Width and/or precision may be specified as *, in which the next argument will be used for the width or precision value.
Format character and result
Format next argument as integer, truncating after the decimal point.
Format next argument in decimal floating point.
Format next argument in scientific notation.
r, R
Format next argument as rational fraction x / y.
Format next argument as numerator only of rational fraction x / y.
Format next argument as denominator only of rational fraction x / y.
Format next argument in octal format, with leading '0'; floating point argument formatted as octal rational fraction x / y.
Format next argument in hexadecimal format, with leading '0x'; floating point formatted argument as hexadecimal rational fraction x / y.
Format next argument in binary format, with leading '0b'; floating point argument formatted as binary rational fraction x / y.
Format next argument as string.
Format next argument as single character value.
Format single literal %.
Other characters in format string
Format ASCII newline.
Format ASCII carriage return.
Format ASCII tab.
Format ASCII form feed.
Format ASCII vertical tab.
Format ASCII backspace.

Mpexpr is based on Tcl 7.6 'tclExpr.c' and David Bell's 'Calc' program. This man page is largely borrowed from Tcl 7.6 as well, as is the mpexpr test suite.
See the files README and INSTALL for additional information.
Tcl 7.6 is Copyright (c) 1987-1994 The Regents of the University of California and Copyright (c) 1994 Sun Microsystems, Inc.
Calc is Copyright (c) 1994 David I. Bell.

Tom Poindexter,, Talus Technologies, Inc., Highlands Ranch, CO.
Version 1.0 released November, 1998.
Copyright 1998 Tom Poindexter. See the file 'LICENSE.TERMS' for additional copyright and licensing terms.
8 January 1998 Tcl

Search for    or go to Top of page |  Section other |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.