Grammer of Formula


4th Edition
Nov. 23, 2016

Variable

Naming Rule

Character in variable name is alphabet, underscore '_' or number. It must be started from alphabetic or underscore. This is case sensitive.

Variables with the same name of function can be used.

Constants

Predefined constants are as followings.

Constants defined by user are also available. But, "today" and "now" are defined as constants for in future. Please don't use these in this app version.

Arrays can be defined as constant.

You can not define a variable with the same name as constants.

Array

Arrays can be defined and used with the same naming rules of variable. The index value must be zero or more integer. It is specified between bracket '[' and ']'. The indices of multidimensional array are separated with comma ','. If an index value is real number, the decimal point number is rounded off.

The upper limit to the number of dimensions is three.

The upper limit to the number of array items is 100,000.

Example )
  A[5]
  A[1,2,3]
  A[1+2,1.2]

Definition

Definition in Variable Field

Variables used as inputting method to formula can be defined with "Variable" field in "Definition" page. That time, the description of variable can be omitted.

Attach @ mark at the top of the name to define a array. Only one dimensional array can be defined with "Variable" field, and the size cannot be specified. When the formula is used, the values of array are specified with comma ',' separating each values.

Definition in Formula

Temporarily use variables can be defined. For the detail, please refer Substitution or Array Definition in the next section.

Constant/Variable Page

In "Constant/Variable" page, constants to be commonly used in formulas or calculator can be defined. For the usage, please refer the manual from the page.

Calculating Formula

A calculating formula is composed of variables, constants, real numbers, comma',', brackets'(',')', or following operators and functions.

Operator

The following operators are available.

* cannot be abbreviated.

Multiple Expressions

By describing separated multiple of a formula with a semicolon ';', you will be able to calculate these at the same time. The result string are broken at the ';' positions.

Example )
  1+2; 3*4

Result )
  3
  12

Substitution

[Variable]=[Equation] style formula can be described. If [Variable] is not an array, a temporary variable is defined at the place. The result of equation will be assigned to the variable. If [Variable] is an array, it must be defined in advance. The result is never shown on the result field. The variable can be utilized in calculating formulas after this.

Example )
  x=3; x*4

Result )
  12
Example )
  A[4]=5;
  A[1+2]=6;
Example ) Solution for ax2+bx+c=0
  det=b*b-4*a*c;
  fg=GE(det,0)*NOT(EQ(a,0));
  fg;
  IF(fg,(-b+sqrt(det))/(2*a),0);
  IF(fg,(-b-sqrt(det))/(2*a),0);

Moreover, a variable with the same name defined in the variable definition field, can be put at the left side of a substitution equation. In this case, user entering value will be overridden.

Example)
  It is assumed that the variable x is defined.
  x=x+1;x

Result) If the user is entering x=1.
  2

Array Definition

A definition of array is described between '@' and ';'. The name of array is placed just after '@' and the each size of dimension are placed between '[' and ']'. The size can be described as an equation. Zero is assigned to all items.

Example )
  @A[5];
  @x[5,4];
  n=10;
  @y[n,n*2];

An one dimensional array can be defined with its values described in { }. Don't specify the size of array.

Example )
  @A={0,1,2};
  A[1]

Result )
  1

Size of array can be got with ASize function.

Comment

A comment string from " to " or to ; is outputted in the result field. A comment string from # to # or to ; is ignored. Two comment strings can be placed at the head and tail of line.

Example )
  #Comment (ignored);
  "Example of substitution;
  x=1;
  "x="x

Result )
  Example of substitution
  x=1

Example )
  "Total Fee: "100+200"$

Result )
  Total Fee: 300$

Function

Mathematical Function

In a mathematics funcion, C language function with the same name is called.

Trigonometric Function
( θ-angle[rad], a-base, b-height )
cos(θ)cos function
sin(θ)sin function
tan(θ)tan function

Inverse of Trigonometric Function
( a-base, b-height, r-hypotenuse return-angle[rad] )
acos(t)inverse of cos, t=a/r
asin(t)inverse of sin, t=b/r
atan(t)inverse of tan, t=b/a, return range:[-π/2,π/2]
atan2(b,a)inverse of tan, return range:[-π,π]

Logarithm
ln(x)natural logarithm: ln x
log(a,b)logarithmic function: logab (=ln(b)/ln(a))
log10(x)common logarithm: log10 x

Other
exp(x)exponential function: ex
pow(x,a)power function: xa
abs(x)absolute function: |x|
sqrt(x)square root
√(x)square root
round(x)Rounding off
floor(x)discard decimals
fmod(x,y)remainder of x÷y

Logical Function

Logical functions are equipped. Non zero value is regarded as true. zero is regarded as false.

Logical Function
NOT(fg)negative function:returns 0 when fg is TRUE, or 1 when fg is FALSE.
EQ(a,b)comparison: a==b
LT(a,b)comparison: a<b
LE(a,b)comparison: a≤b
GT(a,b)comparison: a>b
GE(a,b)comparison: a≥b

Branching Function

The following functions are used to branch the process.

Branching Function
IF(fg,x,y)
judging function
return x when fg is TRUE(fg≠0), or y when fg is FALSE(fg=0).
SWITCH(i, eq0, eq1, ...)
Evaluate eq0 when i=0, eq1 when i=1, or ...

Σ, Π Operation

SIGMA function is an implementation of summation Σ. PI function is an implementation of infinite product Π.

A parameter to be used in an iterative operation is defined at the first argument. Its name must be different of user defined parameters. The second argument is the initial value, and the third is the end value. A equation for the iterative operation is placed at the fourth argument.

Σ, Π Function
( i-parameter, i0-initial value, i1-end value )
SIGMA(i,i0,i1,eq)
PI(i,i0,i1,eq)

Array Support Function

There are ASize function to get size of array and ALevel function that searches a value position in an one dimensional array.

Array Support Function
ASize(A, d)
Get array size.
A: array name.
d=0: get total size.
d>0: get size of the d-th index.
ALevel(A, fg, v)
Search value position in one dimensional array.
A: One dimensional array's name. The values must be set in ascending order.
(n is regarded as size of A in the followings.)
fg: Range specification type.
 fg=0 or 2: A[i]≤v<A[i+1]
 fg=1 or 3: A[i]<v≤A[i+1]
It is regarded that A[-1]= -∞ and A[n]= ∞.
v: Evaludated value.
Return:
i satisfying the above judgment formula is returned.
However, when fg=2 or 3, and i=-1, 0 is returned.
Note) If fg=2 or 3 is specified, range error will be avoided when the result value is used as the index of A.

Example ) ASize
  @A[2,3];
  ASize(A,0);
  ASize(A,1);
  ASize(A,2)

Result )
  6
  2
  3

Example ) ALevel
  BMI=27;
  @A={0,25,30,35,40};
  "Degree of Obesity="ALevel(A,0,BMI)

Result )
  Degree of Obesity=1

Extended Syntax

For more complex processing like iterative execution of formula or switching depending on conditions, a simple programing environment is equipped.

$ Header/Block

At first, Fomcal's parser divides formula string to lines at the position of line break. Next, from checking existence of $ at the head of line without white spaces, the parser judges which it is a line of extended syntax or not. If the head is $, the line is called $ header. Just after $, a command like IF or FOR is described. Lines without $ are connected again until appearing the next $ header, and it will be treated as a set of formula (Eq block). $END header is used in combination with other $ header. The range from the $ head to $END is called $ block. In a $ block, the other $ blocks or Eq blocks can be included. There are blocks with only one line like $OUT.

Example ) $IF block
  $IF EQ(x,1)
     y=1
  $ELSE
     y=2
  $END

Separation of Parameters in $ Header

Colon : is used as a separator to separate parameters in $ header.

Example )
  $FOR i:1:5
    i # Display i value
  $END

String

In $ header, the part enclosed with " or ' is treated as a string. The part not to be string, is evaluated as a equation. In $ header, plural equations cannot be described separated with ;.

Special characters can be output with backslash \.

Variable Definition in $ Block

When A variable is defined in a $ block, after this, it can also be used outside the $ block.

$ Bkick Details


$ Header List
$ HeaderFunction
$$Comment
$OUTString output
$PRINTString output with format
$IFConditional branch
$ELSEIFConditional branch (used with $IF)
$ELSEConditional branch (used with $IF)
$FORIterative processing
$WHILEIterative processing
$BREAKBreak iterative processing
$CONTINUEBreak iterative processing and go to the head of iterative processing
$STOPStop the processing
$ENDThe end of $ block

$$ Comment

Format

$$[Comment]

Processing

The description from $$ to the end of line is ignored as a comment.

$OUT

Format

$OUT [Str/Eq]:[Str/Eq]: ...

Processing

Strings or values of equation are outputed. Plural strings or values can be outputed with colon : separator. Put "\ n" at the end when you want to break a line after output.

Example )
  x=1
  $OUT "The result is "
  $OUT "X=":x:".\n"

Result )
  The result is X=1.

$PRINT

Format

$PRINT [Format]:[Str/Eq]:[Str/Eq]: ...

Processing

Strings or values are output with a format to be described as the same style of printf function of C language. However, as a type specifier, c (character) and p (pointer) cannot be used.

Example )
  x=1.23
  $PRINT "The result is X=%f.\n":x

Result )
  The result is X=1.23.

$IF

Format

$IF [Equation]
  [Eq block or $ block] 
$ELSEIF [Equation]
  [Eq block or $ block] 
$ELSEIF [Equation]
  [Eq block or $ block] 
...
$ELSE
  [Eq block or $ block] 
$END

Processing

Conditional branch is executed. A equation judging the condition is placed in $IF or $ELESIF header. If the value of equation is 0, it means false, or if the value is not 0, the condition is judged as true. $ELSEIF and $ELSE can be omitted. Plural $ELSEIF can be placed.

Example ) Solution to ax^2+bx+c=0
  det=b*b-4*a*c;
  $IF EQ(det,0)
    -b/(2*a)
  $ELSEIF GT(det,0)
    (-b-sqrt(det))/(2*a);
    (-b+sqrt(det))/(2*a)
  $ELSE
    $OUT "No solution"
  $END

$FOR

Format

$FOR [Counter]:[Initial Value]:[End Value]
  [Eq block or $ block] 
$END

Processing

Eq block or $ blocks until $END are executed repeatedly. That time, an integer value from the initial value to the end value is sequentially assigned to the counter variable. If [Initial Value]>[End Value], the counter value will be decreased. If the counter variable has be defined before $FOR, the value is overridden in the $FOR block. Or not defined before $FOR, a counter variable will be defineded newly. The counter variable can be used after the $FOR block.

By the way, if the counter variable is overridden in the $FOR block, at the top of next loop, a new counter value is assigned to the counter variable. Therefore, even if a value exceeding the end value is set to the counter variable, the loop will continue. Use $BREAK block to stop the loop processing.

To prevent runaway, the number of loop counter is limited to 1,000,000. If it exceeds this, the process will be forcibly terminated.

$BREAK stops the loop forcibly. $CONTINUE breaks the loop and returns to the top.

Example )
  x=0
  $FOR i:1:10
     x=x+i
     $OUT x:"\n"
  $END

$WHILE

Format

$WHILE [Judgment Formula]
  [Eq block or $ block] 
$END

Processing

Until the value of [Judgment Formula] is equal to 0, [Eq block or $ block] is executed repeatedly. [Judgment Formula] is evaluated at the top of loop.

To prevent runaway, the number of loop counter is limited to 1,000,000. If it exceeds this, the process will be forcibly terminated.

$BREAK stops the loop forcibly. $CONTINUE breaks the loop and returns to the top.

Example )
  x=1
  i=1
  $WHILE LE(i,10) 
     $OUT x:"\n"
     x=x+i;i=i+1
  $END

$BREAK

Format

$BREAK

Processing

$BREAK is used to break $FOR or $WHILE loop. If $BREAK is placed the outside of $FOR or $WHILE, the all process will be stopped like $STOP.

Example )
  $FOR i:0:10
    $OUT i:"\n"
    $IF EQ(i,2)
      $BREAK
    $END
  $END

Result )
  0
  1
  2

$CONTINUE

Format

$CONTINUE

Processing

$CONTINUE is used in $FOR or $WHILE loop. When $CONTINUE is executed, the process will be returned to the top of loop. When the loop type is $FOR, the next value is set to the counter variable.

Example )
  $FOR i:0:4
    $OUT i:"\n"
    $IF EQ(i,2)
      $CONTINUE
    $END
  $END

Result )
  0
  1
  3
  4

$STOP

Format

$STOP

Processing

The processing will be end.

When the formula is used as a function, the function is stopped by $STOP, but the upper side process will be continued.


⬆︎
Ⓒ2016, Noboru Imai.