BAIT: val command

NAME

val - floating point function evaluator

SYNOPSIS

val [int] [baby] [radian] [verify] [mod=#] [pmod=#] [format=s] string

DESCRIPTION

val evaluates the string (or strings) in the argument and prints the floating point result to the standard output. The arithmetic operators ( *, /, +, - ) are all allowed with the usual precedence rules. The ^ character is the exponentiation operator. Expressions to be evaluated separately may be enclosed in parentheses. IMPORTANT These characters are often interpreted by the shell and so they often must be quoted.

The functions ( sin, cos, tan, log, log10, exp, sqrt, fabs) are available. The trigonometric functions accept arguments in degrees, unless overridden by the radian flag. Also the function `hms2d() can convert hour:minute:second expressions into floating point numbers.

If val cannot evaluate a string to floating point number, it prints out an error message and generates a non-zero exit code.

The logical operators (&, |, !, &&, ||, >, < ) are also interpreted in a manner similar to C. Except for < and > , the operands are first converted to integer by floor(x+0.5) then the operation performed, and the result converted to floating point. In addition since the results have only integer values, the print mode is set to integer whenever any of these operators appear.

FLAGS

int - prints out the value rounded to the nearest integer

baby - prints out the number in babylonian format e.g. +12:35:12

radian - uses radian arguments to the trigonometric functions.

verify - shows the arguments before evaluation.

format=s - changes output format for floating point numbers from %.4f to that in the command line. Note the Cconvention is used.

pmod=# - assumes that the answer lies 0. < x < mod . mod=# - assumes that the answer lies -mod/2 < x < mod/2. .

help - displays options.

EXAMPLES

val "hms2d(12:34)+1.34" baby

converts the babylonian value 12:34 to decimal, adds 1.34, converts back to babylonian and prints out the value. Note: the quotation marks are needed to prevent the shell from seeing them.

val "(4*15.4) > 26.5"

Takes the product of 4*15.4 , sees that it is bigger than 26.5 and prints out the value 1. If the statement were not true val would print out 0. Again note the quotes.

val "10^($mags/2.5)" format=%.2f

converts the shell variable `mag' to a factor and prints out the result to two significant figures. The shell can `see through' the double quotes and perform variable substitution.

BUGS

The expression evaluator gets confused with the distinction between negative numbers and the subtraction operator. You may have to experiment with adding parentheses to get it to work.

Formats are not checked for the correct type, and may lead to bizarre results if mistyped.