next up previous contents
Next: Statements and Control Structures Up: The Hoc Language Previous: The Hoc Language

Arithmetic Expressions

Hoc as described here is based on arithmetic expressions, similar to C. The grammar for these expressions is the following:

expr:   number
    |   variable
    |   variable = expr 
    |   array [ expr ]
    |   ( expr )
    |   expr binop expr
    |   unop expr
    |   function ( arguments )

(This is to be read: '' An expression is formed by either a number or a variable or an array-name followed by [ followed by an expression followed by ] or ...)

Note that this notation is recursive in nature, allowing familiar expressions like e.g. sin(exp(b[i] * -1.5E-3)).

Numbers are either floating point values in their usual notation (see scanf(3)): digits, decimal point, digits, e or E, exponent with sign, or written in hexadecimal notation just as in ''C'' (e.g. 0xFF meaning 255). Note, however, that hex constants are stored as floating point values as well.

All identifiers (i.e. names for variables, arrays or functions) consist of a letter, followed by an arbitrary number of letters, digits or ''_'' (Well, the maximum length is 128 characters).

binop means a binary operator (involving two operands), like addition or test for equality, unop means a unary operator like ! (logical NOT) or unary minus (switching the sign). See 5.1 for a list of available operators.


 
Table 5.1: List of hoc Operators with Decreasing Priority
Oparator Purpose
^ Exponentiation (like FORTRAN **
! - (unary) logical and arithmetic complement
* / % multiplication, division and modulo
+ - addition and subtraction
>> << bitwise shift left, shift right
> >= comparison: greater than, greater or equal
> >= comparison: less than, less or equal
== != comparison: equal, not equal. (all the same precedence)
& bitwise AND
| bitwise OR
&& logical AND
|| logical OR
++ -- (post- and pre-) increment, decrement
= += -= *= /= assignment
 


 
Table 5.2: List of Available Built-in Arithmetical hoc Functions
Function Purpose
sin(x) sine of x (in radian)
cos(x) cosine of x (in radian)
tan(x) tangent of x (in radian)
asin(x) arcus sine of x
acos(x) arcus cosine of x
atan(x) arcus tangent
atan2(y, x) arcus tangent of y/x
sinh(x) hyperbolic sine
cosh(x) hyperbolic cosine
tanh(x) hyperbolic tangent
log(x) natural logarithm of x (base e)
log10(x) logarithm of x base 10
exp(x) exponential of x (ex)
sqrt(x) square root of x ($\sqrt{x}$)
int(x) truncates floating point part of x
abs(x) absolute value of x
ceil(x) smallest integer not smaller than x
floor(x) largest integer not greater than x
fmod(x,y) computes the modulo remainder
lgamma(x) natural logarithm of the gamma function (x > 0)
chi2dist(n,x) chi squared distribution for n degrees of freedom (n,x > 0)
randomFlat() random number, flat distributed in (0, 1)
randomGauss() random number, gauss distributed
 


 
Table 5.3: List of Available Miscellaneous Functions
printf(format,...) print arguments to stdout according to format
sprintf(format,...) print arguments according to format into a string
  the string is returned (only allowed as argument to other functions)
read(varname) read a number from the terminal window and
  assign the value to variable varname
xread("description",default_value) read a number from an X window (which will pop up). Returns entered value, the description string is mandatory, the default value may be ommitted.
xread_string("description","default") read a string from an X window (which will pop up). Returns pointer to entered string, the description string is mandatory, the default value may be ommitted.
read_string("description") read a string from the terminal. Returns pointer to entered string, the description string is not mandatory.
fp=fopen("filename","mode") open a file with name filename for read (mode=r), write (mode=w) or append (mode=a) and return file pointer
fclose(fp) close a file opened with fopen
fprintf(fp,"format",...) like printf, but print to file and not to stdout.
id = ijopen("filename") opens a IJ_data lookup table file
val= ijget(id, x,y) make a lookup at (x,y)
ijclose(id) close a lookup table file
exit_yoda() terminates yoda
exp_num() the current experiment number
run_num() the current run number
evt_num() the current event number
evt_size() the size of the current event in bytes
write_event(level) write the current event to the YODA output stream. If level is given subevents created by callbacks with priority value greater than level are not written. Default level is 0 (only raw data)
abort_event() abort processing of the current event, subsequent callbacks are not called
dedx(energy) calculates the differential energy loss for plastic scintillator (in MeV/cm) for protons of kinetic energy (in MeV/c2).
setup_dedx(I,Z/A,Rho,z,M,a,m,X1,X0,-C) setup parameters for subsequent calls to dedx, where
  I = mean excitation potential (eV)
  Z/A = atomic number of absorbing material/ atomic weight of absorbing material
  Rho = density of absorbing Material (g/cm3)
  z = charge of incident particle in units of e
  M = mass of incident particle (MeV) parameters for density correction: (see leo's book for examples) a, m, X1, X0, -C
shell("sh shell command") execute string as shell command, using sh
 

Logical expressions have the values 1.0 (true) and 0.0 (false). Like in C, every value not equal to 0.0 is considered true. Note, that due to the floating point representation, every result coming from a computation cannot be guaranteed to be exactly zero! There are also some predefined constants available, see 5.1.


 
Table 5.4: List of Available Built-in Constants
Name Value Meaning
PI 3.14159265358979323846 $\pi$, ratio of circle circumference to its diameter
E 2.71828182845904523536 e, base of natural logarithms
GAMMA 0.57721566490153286060 Euler-Mascheroni constant
DEG 57.29577951308232087680 $180/\pi$, degrees per radian
PHI 1.61803398874989484820 $(1+\sqrt{5})/2$, the golden ratio
PID arbitrary current pid of YODA
 


next up previous contents
Next: Statements and Control Structures Up: The Hoc Language Previous: The Hoc Language
Heiko Rohdjess
2001-07-19