# hoc(1) [plan9 man page]

HOC(1) General Commands Manual HOC(1)NAME

hoc - interactive floating point languageSYNOPSIS

hoc [ file ... ]DESCRIPTION

Hoc interprets a simple language for floating point arithmetic, at about the level of BASIC, with C-like syntax and functions. The named files are read and interpreted in order. If no file is given or if file is hoc interprets the standard input. Hoc input consists of expressions and statements. Expressions are evaluated and their results printed. Statements, typically assignments and function or procedure definitions, produce no output unless they explicitly call print. Variable names have the usual syntax, including the name by itself contains the value of the last expression evaluated. The variables E, PI, PHI, GAMMA and DEG are predefined; the last is 59.25..., degrees per radian. Expressions are formed with these C-like operators, listed by decreasing precedence. ^ exponentiation ! - ++* / % +--> >= < <= == != && || = +=-*= /= %= Built in functions are abs, acos, asin, atan (one argument), cos, cosh, exp, int, log, log10, sin, sinh, sqrt, tan, and tanh. The function read(x) reads a value into the variable x and returns 0 at EOF; the statement print prints a list of expressions that may include string constants such as "hello ". Control flow statements are if-else, while, and for, with braces for grouping. Newline ends a statement. Backslash-newline is equivalent to a space. Functions and procedures are introduced by the words func and proc; return is used to return with a value from a function. Within a func- tion or procedure, arguments are referred to as $1, $2, etc.; all other variables are global.-=EXAMPLES

func gcd() { temp = abs($1) % abs($2) if(temp == 0) return abs($2) return gcd($2, temp) } for(i=1; i<12; i++) print gcd(i,12)SOURCE

/sys/src/cmd/hocSEE ALSO

bc(1), dc(1) B. W. Kernighan and R. Pike, The Unix Programming Environment, Prentice-Hall, 1984BUGS

Error recovery is imperfect within function and procedure definitions. HOC(1)

## Check Out this Related Man Page

ICALC(1) General Commands Manual ICALC(1)NAME

icalc - interactive calculatorSYNOPSIS

icalc [ file ]DESCRIPTION

Icalc is a algebraic calculator designed primarily for interactive use. Each formula definition file is read and compiled. The standard input is then read, expressions are evaluated and results are sent to the standard output. If a newline is escaped using a backslash, input is continued on the next line. An expression contains real numbers, variable names, function calls, and the following operators: + - * / ^ Operators are evaluated left to right, except '^', which is right associative. Exponentiation has the highest precedence; multiplication and division are evaluated before addition and subtraction. Expressions can be grouped with parentheses. Each result is assigned a num- ber, which can be used in future expressions. For example, the expression ($3*10) is the result of the third calculation multiplied by ten. A dollar sign by itself may be used for the previous result. All values are double precision real. In addition, variables and functions can be defined by the user. A variable definition has the form: var = expression ; Any instance of the variable in an expression will be replaced with its definition. A function definition has the form: func(a1, a2, ..) = expression ; The expression can contain instances of the function arguments as well as other variables and functions. Function names can be passed as arguments. Recursive functions can be defined using calls to the defined function or other functions calling the defined function. To define a constant expression, simply replace the equals sign ('=') with a colon (':') in a definition. Constant expressions are evalu- ated only once, the first time they are used. This avoids repeated evaluation of expressions whose values never change. Ideally, a con- stant expression contains only numbers and references to previously defined constant expressions and functions. Constant function defini- tions are are replaced by their value in any expression that uses them with constant arguments. All predefined functions and variables have the constant attribute. Thus, "sin(PI/4)" in an expression would be immediately replaced by ".707108" unless sin() or PI were rede- fined by the user. (Note that redefining constant expressions is not a recommended practice!) A variable or function's definition can be displayed with the '?' command: ? name If no name is given, all definitions are printed. The '>' command writes definitions to a file: > file Similarly, the '<' command loads definitions. The following library of predefined functions and variables is provided: PI the ratio of a circle's circumference to its diameter. if(cond, then, else) if cond is greater than zero, then is evaluated, otherwise else is evaluated. This function is necessary for recursive defini- tions. select(N, a1, a2, ..) return aN (N is rounded to the nearest integer). This function provides array capabilities. If N is zero, the number of avail- able arguments is returned. rand(x) compute a random number between 0 and 1 based on x. floor(x) return largest integer not greater than x. ceil(x) return smallest integer not less than x. sqrt(x) return square root of x. exp(x) compute e to the power of x (e approx = 2.718281828). log(x) compute the logarithm of x to the base e. log10(x) compute the logarithm of x to the base 10. sin(x), cos(x), tan(x) trigonometric functions. asin(x), acos(x), atan(x) inverse trigonometric functions. atan2(y, x) inverse tangent of y/x (rangeto pi).-piAUTHOR

Greg WardSEE ALSO

ev(1), rcalc(1), tabfunc(1)RADIANCE

2/3/95 ICALC(1)