McCal Document

McCal is a programmable scientific calculator for Android. It can display mathematical formulae naturally on the fly, and share data with SpreadSheet programs. The features of McCal include:

  1. Natural Display
  2. 'Macro' simplifies recording/replay of complex key-strokes.
  3. Complex Number calculator
  4. Linear Algebra, Matrix calculator
  5. Programmable; scripting using a BASIC-like language

 

FAQ (Frequently Asked Questions) page is now available. Some of the questions are:

  1. Calculation Precision?
  2. Solving simultaneous equation
  3. Solving polynomial equations
  4. Eigenvectors and Eigenvalues

McCal-lite is a free version of McCal. We have now 'the differences of McCal-lite' document.

 

What's New?

Inspector

We are glad to release McCal Version 5, which now supports 'Inspector' to take a close look at the calculation results. Just press  <Ins>  button!!

 

Matrix operators: This version now sopports MATLAB-like matrix operators. Support for right/left division by Matrix, element-by-element operators, such as multiplication and division. Support for the inverse of matrices containing complex numbers.

Please read the Matrix Calculation document.

 

Equation Solver: McCal is now equipped with system functions for handling polynomial, and for solving polynomial equations, even if they contain complex numbers in the coefficients.

We have now Polynomial and Solving equations document.

 

1. Getting Started

Let's start up McCal, and you'll get a typical view as a scientific calculator; A display window at the top, and in the rest, many keys are arrayed in a grid-layout.

McCal is designed for institutive input of mathematical formulae and natural display of them in real-time. With this rendering ability of McCal, you can understand the mathematical formulae at a glance.

example

In this document, key strokes are represented as a sequence of boxes with labels, such as 1 8 0 . In this case, three keys for 1, 8, 0 are pressed in this order. The colors of the boxes and labels are similar to those in McCal keys. A yellow label means that you should press Shift key at the upper left corner of the key grid first, then press the corresponding key with that label, to select the shift-position of the key.

 

Action Bar

McCal now displays its action bar at the top of the display.

actionbar

It is possible to turn off the action bar. Pressing the <Bar> button toggles the action bar.

The icons shown in the action bar are tied to special roles. We also created buttons for those roles, although they are at the shift postions. The following is the table of icons and buttons.

You may not find some of the icons at the action bar. When enough space is not availabe for action bar icons, Android system put them at the bottom of the option menu.

Icon Button Description
  <Bar> Toggles between hidden and displayed action bar.
keypad   Cycles the calculatr keypads.
mcv <McV> Starts up McCal Viewer.
mcv <Fn.> Starts up Function Viewer.
mcv <KeyB> Toggles android's software keyboard. Touching the main calculator window also brings up the software keyboard.

Buttons of which labels are enclosed in parentheses, such as (format) and (radix) , display dialogs for setting or short-cuts to related functions.

 

McCal now supports two key layouts, each is targetted for phones or for tablets. When you start the version of McCal, please choose one of the key layouts by selecting "Select the calculator layout" from the option menu.

opmenu

 

1.1 Arithmetic operations and editing

First, lets operate McCal step-by-step. To calculate the following,

180 ÷ 6 (Answer) 30

press 180  ÷ 6 in this sequence. And then, pressing eval() key gives you the result.

 180  ÷ 6 eval()

frac

A rendered formula appears in the right-bottom corner of the display window. The key strokes are converted to the corresponding source script shown at the upper-left corner in black color.

(Note) In the position of '÷', McCal displays '/' in the source code editor. This is because McCal-Script is designed to use ASCII-code instead of multibyte Unicode as much as possible. Other examples include: '=' instead of '⇐', and '*' instead of '×'. Thus, McCal-Script uses a similar character set to Java and C languages'.

1.2 Last answer and History

Users can re-use the result of the previous calculation easily. Assume we multiply 2.76×10-7 with the previous answer.

 2.76  Exp  -  7  ×  Ans  eval() 

2.76E-7*#ans()
▶ 8.01E-6

 Ans  button and '#ans()' recalls the previous calculation result. It is possible to get the same result by using the  ×  button:

 ×  2.76  Exp  -  7  eval() 

*2.76E-7
▶ 8.01E-6

Users can also recall the past keystroke, not calculation result, easily. Assume we actually want to calculate sin(180÷6).

sin Hist Hist
#sin(180/6)

The last input, '180/6' recalled by Hist key is high-lighted as a region in the source code editor. Therefore, pressing Hist repeatedly recalls the past inputs in the reverse order. McCal can remember 50 past inputs, whereas McCal-lite remembers 4.

Then pressing eval() key returns the result (0.5).

#sin(180 / 6)
▶ 0.5

In the source code editor, user can use cursor, selected region, and backspace BS ) like a standard text editor.

Let's change the source code to #sin(180/2). By pressing and/or several times, move the blinking cursor after '6'. Then, pressing BS 2 eval() results in:

sin180-2

The new result '1' is added after the text. The previous result ('0.5') is left as is for comparison.

(NOTE) The default angle mode of trigonometric functions is 'degree', which is shown in the upper right corner of the Android display. To change the angle mode, press Shift (angle) . A dialog will appear for angle mode selection.

In summary, McCal can translate user key strokes into the corresponding source code text. The user can recall/edit the past inputs and results in the text form.

1.3 McCal Viewer for History

Besides the Hist button described above, McCal has "McCal Viewer" for easy access to the past calculation history.

To start McCal Viewer, press the McViewer icon mcvicon, or the <McV> button.

hist-1

McCal can remember 50 past calculations, whereas McCal-lite, 4. Please not that the expression is rendered in 'natural display' mode.

Tapping on a list item brings up a dialog (shown below) for managing the respective key history. Users can paste the result or expression into the McCal calculator.

hist1-dialog

The help document for McViewer is available.

In summary, McCal is designed for reducing the tedious key pushing, and for re-using the past input.

 

1.4 Function and Input box

As a scientific calculator, MacCal is equipped with many functions. Even the values of π and e (Napier's constant, or base of natural logarithm) are obtained by calling a respective function.

Let's take a look at the Pi function. Press π key, then you will get '#pi()' in the source code editor, and 'π' in white color at the formula display.

 π eval()
pi-eval

In McCal-Script, '#' indicates the beginning of a function. The following alphabetical or numerical characters are the name for the function (in this case, 'pi'). Please note that, if '#' is not present, 'pi' means a multiplication product of variable 'p' and imaginal unit, 'i'.

In the following examples,

 2  π ,  π  2 ,  2 ×  π 
2#pi() 
#pi()2
2×#pi()

all return the same value, 6.283185... The × operator can be omitted in McCal.

Some functions require argument(s). Arguments should be given after the function name with a pair of parentheses and commas.

#max(3.2, 3.3)
▶ 3.3

It's tedious to remember the number of arguments for each function. To simplify input of a function and its arguments, McCal has a mechanism called 'Input box'. When you press the key for a function (below is an example screen shot with the key), you get the function name and input boxes in the source code editor prompting the input of the required arguments.

sqrt-root

An input box is actually a Unicode character for a box. It can be rendered as a box, but before the evaluation of the function, it must be substituted with an appropriate text.

Let's try an example using the square root function (#sqrt) and the fraction function (#frac).

root-exp

Assume we want to calculate the value of the above formula. Press the root key (square root, #sqrt(), ), then you get the following display:

sqrt-root

The Input box is selected as a region in the editor. Next, we want to enter the fraction part. Press the fraction key frac

 frac 

sqrt-frac

The 'input box' of the root function is replaced with the fraction function with two input boxes, which are required for numerator and denominator of fraction. McCal automatically selects the first input box.

Then press  4  +  8  to enter the numerator part,

 4  +  8 

sqrt-frac-48

Since we now want to enter the denominator part, press the key for cursor movement to the next input box ( →□ , located at the upper-right corner of the keypad). With this key, McCal tries to find the next input box.

 →□ 

sqrt-bunbo

As the last step, enter the denominator part and the evaluation key eval() .

1 + 2 eval()

sqrt-ans

The above sequence of key-strokes can be summarized as follows.

frac 4 + 8 →□ 1 + 2 eval()

As for the fraction function, it can be replaced with parentheses and a divide operator as below:

#sqrt( (4+8) / (1+2) )

Both return the same result, but in general, you need less number of key-pressing with #frac().

In McCal, an opening parenthesis is closed with a pairing parentheis automatically. In order to move the cursor outside of the function, you will need to press 'cursor-move-to-next-input-box' key ( →□ ) or .

 

1.5 User-defined Function

McCal supports user-defined functions. Programs in BASIC language-like script can be defined/excuted in user-functions. McCal functions can have local variables insulated from outside, and return multiple results.

To define a function, there are two ways. First, you can declare a function in the calculator window using 'Function' statement. The  Func  key is located in the second key pad, which is obtained by swiping the main key pad horizontally.

Function #Usr.distance(a,b) { Return #abs(a-b) } 

The second way is to use McCal Function Viewer. It displays system built-in functions as well as user-defined functions. It also has its own source code editor.

McCal Function now supports multiple returned values. Just list the returned values (comma-separated) after "Return" statement.

Function #Test.test(a) { ...; Return x, y, #sin(a)}

To catch the multiple returned values, the script looks like this:

[x,m,s] = #Test.test(2.5)
x = #Test.test(10.5)

In the first script example, the function #Test.test(a) returns three values, and they are assigned to three variables, x, m, and s, respectively. In the latter example, only the first value is saved.

 

1.6 Angle and number formatting modes

User can choose the unit of angle and the format of number-presentation.

Angle mode

angle-format

The default unit of angle for triangular functions is degree. You can change it to 'radian' or 'grad' in the angle mode dialog by pressing (angle) button (in the shift position), or 'Setting' from the Option Menu.

Floating point number format

The number formatting dialog can be displayed by pressiong the (format) button, or the 'Setting..' option in the option menu.

dialog-format

 

The easiest way to view a number in different formats is 'Inspector'. Just Press  <Insp>  button

Insp-fp

 

1.7 Radix conversion

McCal can handle hexadecimal, octal, and binary numbers directly by attaching a respective prefix before the number. These prefixes are the same with Java's and C's. The  0x  key is at the shift position of  0 .

Radix Prefix Example ⇨ value in decimal format
16 0x 0xFF ⇨ 255; 0x36afc ⇨ 223996
10 (None) 1234 ⇨ 1234
8 0 (zero) 0377 ⇨ 255
2 0b 0b11111111 ⇨ 255

(Caution) Please do not confuse the character for hexadecimal numbers (from '0' to 'F') with variable names (variables are explained in the next section). For example, '0xffg' means the multiplification product of hexadecimal '0xFF' and variable 'g' in McCal.The character 'g' is not used in hexadecimal numbers, McCal assumes the hexadecimal number ends before 'g'.

In a similar context, 078 means 7*8 (=56), 07.2 means 7*0.2 (= 1.4) in McCal. '8' is not in the characters for octal numbers, and floating point number is not allowed in the octal format.

0x100 * 0b101
▶ 1280 	
The above example is bitwise-AND between a hexadecimal and a binary number.
0xFFFF & 0x3aF 	
▶ 943

 

Only decimal and hexadecimal number formats support the floating point notation. 'E' and 'p' are used to indicate the exponent of 10 and 2, respectively.

Notation Value in decimal format Mathematical expression
3.14E-4 3.14E-4 3.14×10-4
0x1p1 2 1×21
0xF.2Ap-3 1.895508 0xF.2A × 2-3
07.2 1.400000 07 * .2 (multiplication of octal 7 and decimal 0.2)

Hexadecimal floating-point numbers are useful because they bypass decimal to internal binary floating-point conversions, which sometimes have rounding errors. They also allow for direct control of the exact contents of floating-point values. Please note that the numbers after 'E' and 'p' are decimal.

 

Number to String Conversion

'Inspector' displays a set of converted strings at various radices of the integer from the previous calculation. Just Press  <Insp>  button.

insp-int

 

Conversion in script

McCal has several functions for radix conversion between number and the corresponding string, which can be utilized in scripts.

Radix Conversion function Ex. ⇨ decimal value
16 #hex("string") #hex("FF FF") ⇨ 65,535; #hex("3 6a fc") ⇨ 223996
8 #oct("string") #oct("377") ⇨ 255

White spaces in the argument string are ignored, so that they can be used for easy-reading in these functions. We use the same function for converting from number to string. The same functions are used for converting number to hexadecimal or octal string.

Radix Function Example
16 #hex(...) #hex(0377) ⇨ "0xFF"; #hex(223996) ⇨ "0x36AFC"; #hex(0.5) ⇨ "0x1.0p-1"
8 #oct(...) #oct(255) ⇨ "0377"; #oct(0xFF) ⇨ "0377"
The number in the argument of the octal function must be integer.

Pressing  (radix)  (in the shift position of ) brings up the dialog for radix-conversion functions and bit operators.

 

1.8 DMS conversion

DMS (Degrees-Minutes-Seconds), or HMS (Hours-Minutes-Seconds), type sexagesimal representations are supported by McCal. Internally, arithmetic operations are performed in degrees (or hours) using the following expression.

Degree + minutes/60 + seconds/3600

To enter DMS-type numbers, use the  °'"  key. The following example shows how to calculate Sin(23degree-15minute-32.5seconds + 5minutes-11seconds).

 sin   23   °'"   15   °'"   32.5   +   0   °'"   5   °'"   11   °'"  eval()

dms

Please note that, in the rendering display, degrees-minutes-seconds are marked by °, ', ", respectively. Entering 0 dgree before 5minutes-11seconds is nescessary as shown in the above example.  °'" key after seconds is optional.

Function #dms() converts an angle in degrees to a formatted degrees°seconds'minutes" string. Function #hms() converts a value (as a time-period in hours) to a formatted HH:MM:SS string.

 23   °'"   15   °'"   32.5  eval()  DMS   Ans  eval()
23°15°32.5
▶ 23.259028
#dms(#ans())
▶ "23°15'32.5" "
#hms(23°15°32.5)
▶ "23:15:32.5"

 

1.9 Variable

'Variable' is a place for storing a value. In some other scientifc calculators, this place may be otherwise called 'memory'. In McCal, variables are designated using a single character ('a' to 'z' except 'i', or 'A' to 'Z'), whereas McCal-lite can use only four variables (a, b, c, k).

Please note that 'i' is reserved for the imaginal unit, so that variable 'i' is not legitimate. In the version 3 and above, a user can use a word for the name of a global variable by adding the access-control prefix (@), such as '@name' and '@age'. (explained later)

 

Assigning a value

To store a value in a variable, McCal uses the substitution operator, , or post-substitution operator, .

a ⇐ -2i-3

In this case, variable 'a' holds a complex number '-3-2i'. To enter this formula, press keys as follows.

a - 2 i - 3 eval() .

a = -2i-3
▶ -3-2i

Text 'a' is entered from the Android software keyboard. Actually, the whole text ("a=2i-3") can be entered solely using the software keyboard. Note that '=' is used in McCal Script for the place of .

In contrast, the post-substitution operator, ⇒ (STO), requires a variable at the right side of the operator. When used at the beginning, the post-substitution operator assigns the value of the last calculation (last answer) to the variable.

⇒ a
▶ -3-2i

'STO' stands for "Store". Together, these operators for substitution are, in a certain way, similar to the 'Min' (memory-in) or 'STO' (store) key in some other calculators.

 

Getting the value

To use the value stored in a variable, simply use the variable name in the expression. For example, a eval() returns the content of variable 'a' (in this case, -3-2i).

a eval()
a
▶ -3-2i

More examples:

c b a × 2 eval()
c = b = a * 2
▶ -6-4i

Now, the variable 'b' and 'c' hold the same value, -6-4i

 

Implicit Multiplication

In McCal-Script, it is possible to omit the multiplication operator, × . In the above case, actually, McCal calculates '2i' as a multiplication product of '2' and 'i'. The following examples (here, a = -3-2i) illustrate the 'hidden' multiplication:

b 2 a eval()
b = 2a 
▶ -6-4i
b 3 + a b eval()
3 + ab 
▶ 13 +24i
b ( a + 3 ) ( a - 6 ) eval()
(a+3)(a-6)
▶ -4 +18i

(NOTE) McCal-Script supports an operator priority rule similar to Java's. Multiplication is calculated prior to addition. Furthermore, this 'hidden' multiplication has a priority over the explicit multiplication/division in McCal-Script. In the following example, the hidden multiplication operator between 'aa' is calculated first, and the explicit multiplies/divides are calculated from the left:

1 ÷ a a × b

priority

The natural display feature of McCal reduces the possibility of confusion.

 

Upper-case variable

Upper case variables, such as 'A' and 'X', are supported in Version 4. However, caution is required when they are used in implicit multplication, since upper-case letters are also used for designating the beggining of a keyword, such as 'Print' and 'For'.

F=2; o=3; r=4
F or
▶ 24
F*or
▶ 24

In the above examples, space character or multiplication operator '*' is placed after 'F', so that McCal treats 'F' as a variable.

For
▶ Error

In the third case, however, 'For' is recognized as a keyword. 'For', as a keyword command, is explained in McCal Script Help document.

 

Data Types

A variable can hold a value of integer, floating point, complex number, array, or string. Calculation between these data types is done automatically. For example, only addition (+), but not substraction (-) or multiplication (*), is allowed for string type. Addition of two strings results in the concatenation of the strings. A number is converted to the corresponding string before the addition to a string.

 

Word name and Global Variable

In the version 3 and above, McCal supports long names for variables, which are indicated by preceeding '@'.
@age = 4
▶ 4
@name = "MilimaCode"
▶ "MilimaCode"
@intro = @name + ", "+ 
@age + " years"
▶ "MilimaCode, 4 years"

'@' indicates that the variable is global. '@age' and '@name' in the above example belong to the global variable name space, which is accessible from the main calculator. On the other hand, each user function can have its own name space for local variables. The global and local name spaces are explained in the "McCal Script Help".

 

1.10 Array

Declaration of arrays

To use an array, a variable should first be declared as an array using 'Dim' keyword, which stands for 'dimension'.

Dim a[20]

The index of an array starts with zero '0'. In the example above, a[0] ~ a[19] are created.

Accessing to the elements of arrays

Dim b[4][3], c
b[0][0] = 1560; b[0][1] = 
"Abdul-Jabbar"; 
b[0][2] = "Kareem"
b[1][0] = 1072; b[1][1] = 
"Jordan"; b[1][2] = "Michael"
b[2][0] = 1045; b[2][1] = 
"Chamberlain"; b[2][2] = "Wilt"

Two-dimensional array (b, in the above case) is declared in addition to a non-array variable (c) in the above example. Each element of an array can take a Real, Complex, or String value.

b
 ▶ array[4][3]︎

When the result is an array, McCal simply tells that it is an array with its size. To view the elements, use the 'Print' command or Inspector.

Print b
▽1560  ▽Abdul-Jabbar  ▽Kareem  
▽1072  ▽Jordan  ▽Michael 
▽1045  ▽Chamberlain  ▽Wilt 
▽0  ▽0  ▽0
 ▶ array[4][3]︎

Pressing the  <Ins>  button fires up 'Inspector'. It shows the result in a spread-sheet view, if it is an array.

McCal Viewer also shows the contents of variables graphically. It even allows 'copy-and paste' to and from the spread-sheet programs of Android.

mv
Dim @name[50][100];
In this case, McCal allocates the array (name) in the global variable name space. McCal Viewer accesses to the global variables only. Please see the section "scope" in the second chapter.

Initial value for each element of an array can be defined using box brackets. For 2D-array, use nested box brackets.

a = [[1,0,0],[0,1,0],[0,0,1]] 
Print a
▽1 ▽0 ▽0
▽0 ▽1 ▽0
▽0 ▽0 ▽1

In the above example, the size of the array 'a' is 3X3. When the size is specified in the Dim statement, it has a prioritey over the size of '[..]' construct. In the example below, undefined elements are set to zero, or un-used elements in the '[...]' construct is thrown away, when '[...]' is larger than Dim size designation.

Dim a[4][4] = [[1,0,0],[0,1,0],
[0,0,1]] 
Print a
▽1 ▽0 ▽0 ▽0
▽0 ▽1 ▽0 ▽0
▽0 ▽0 ▽1 ▽0
▽0 ▽0 ▽0 ▽0

In the present implementation, the size of an array is restricted at most 2000 (50 for McCal-lite), because an array can consumes a lot of main memory of your Android system.

When you do not need an array anymore, please re-declare the variable name as a non-array variable. The contents of the array is discarded. Or, delete the variable using the Variable tab of McCal Viewer.

It is now possible to move data between a two-dimensional array in McCal and spread sheet files. To do this, users can 'copy-and-paste' between McCal and spread sheet programs, such as QuickOffice. Please see the section 1.10, McCal Viewer, and the help document of McCal Array Viewer.

 

1.10.1 The colon operator

McCal now supports the colon operator, with which a region of Matrix is easily designated. The colon operator is modeled after that of MATLAB's, but the index starts with 0 (zero) in McCal.

We explain the operator with the variable, @soccer, shown below.

mv

A construct with the colon operator usually looks like: (start):(end). For example, '0:5' means a region from 0 to 5. '@soccer[0:5,1]' indicates the vertical column with number 1.

Print @soccer[0:5,1]
▽Messi ▽43 ▽28 ▽46 ▽50 ▽31
▶︎array[6]

Its possible to designate an incremental value.

a:b When a<b, [a, a+1, a+2,..,b]. When a>b, 'a:b' means empty.
a:k:b [a, a+k, a+2k, a+3k, ...a+m*k], m = (b-a)/k

When 'a', or 'b' is omitted, it means 0 or the array size minus one, respectively. With this feature, users can easily get the column(s) or row(s) of a matrix.

@soccer[:, 1] Returns the column with number 1, ["Messi",43,28,46,50,31]
@soccer[2, :] Returns the row with number 2, [2013,28,55]
@soccer[:, :] Same as @soccer.
@soccer[0:1, 1:2] Returns the square region, ["Messi","Ronald],[43,31]].
@soccer[1:, 2] Returns the number 2 column, but from element 1: [31,55,46,40,53]

 

The colon operator is also used outside of array index. In this case, it returns a region in the array format.

d=1:0.5:3
Print d
▽1 ▽1.500 ▽2.000 ▽2.500 ▽3.000
▶︎array[1][5]

Please note that the colon operator above returns an 1xN array. It is a 2D array with only one row. The transpose operator change it to 1D array (vector).

Print (1:0.5:3)'
▽1 ▽1.500 ▽2.000 ▽2.500 ▽3.000
▶︎array[5]

McCal has an extensive set of operators and functions for matrix calculation (linear algebra). Please see the document of Matrix calculation.

Example

Here we use the colon operators and #sstat(), simple statistics function, together. This function add the total, average, and standard deviation in a table format.

 @re = #sstat(@soccer[1:,1:]) 

In the above example, we used the colon operator to obtain the square region (marked by a red box in the screen shot below).

The returned value is shown below. The original square region is copied in the returned array, indicated by a box in red. Please note that a set of total, average, and SD is added to each lines, so that the size is larger than that of the given array by 3 lines horizontally and vertically.

In the following screen-shot, we copy-and-pasted a part of the result to the original array, and added labels.

 

1.11 Multiple statements

Several statements can be combined into a single line by separating them with semicolon ';', and can be evaluated at once. To enter a semicolon character, select (punc) key (a dialog for punctuation characters will appear), or use the Android software keyboard. In the following example, the area of a circle and the volume of a sphere of which radius is took from the last answer.

Var.  c  ⇐  Ans ;  Print  Var.  a  ⇐  π Var.  c  x2 ; Var.  b  ⇐ 4 ÷ 3 ×  π Var.  c Shift  x3

multistatement

Variable 'a' holds the area of the circle, and variable 'b' holds the volume of the sphere after the evaluation of the above multiple statements. The result of the last expression (in this case, the volume of the sphere) is displayed (▶), whereas intermediate values of a multiplexed statemet (in this case, 'a') can be displayed by the 'Print' command (▽).

'Print' key is in the second key pad. Swipe the keyboard horizontally.

Please note that the first character of 'Print' command is in upper case. Like the function name which starts with '#', a word starting with an upper case letter is assumed to be a command or statement keyword. These keywords include 'Dim(ension)', and 'Function'.

Well, these examples are complex enough... You can record multiple statements in a sigle line, and recall it using the 'Macro' functionality of McCal explained in the following chapter.

 

1.12 Macro execution

'Macro' grealy simplifies repetitive input and excution of multiple expressions. Users can register a set of multiple statements under the 'Macro' tab of McCal Viewer, and recall it by simply pressing the Mac button. The following is an example:

Assume, in the following circuit consisting of a resistor and a capacitor (RC circuit), you want to calculate the electric voltage across the capacitor (Vc) at various frequencies. Theoretically, Vc is calculated as in the following figure.

circuit

When f = 2000Hz, the expression can be entered as below.

 f  ⇐  2000  ;  c  ⇐  1 Exp  -  8  ;  r  ⇐  5 Exp  3  ;  v  ⇐ frac  10  →□   1  +  i  2  π  f  c  r 

circuit2k

(Note) The result (71269568 -4.504772i) is a complex number. In the field of electric engineering, the absolute and argument of this value means the magnitude, and phase of the alternating current, respectively.

The above expression is very long, but you can register it for later re-use. First let's change the value '2000' for variable 'f' to an input box.

Press  Hist  button, then the expression appears in the main window. Next remove the value '2000' by pressing cursor movement keys and Backspace button. And finally enter the input box, as shown below. (The input box can be entered from the dialog for (punc) button.)

circuit-polar

Press eval() button. Then the expression appears under the history tab of McCal-Viewer, although evaluation fails since the input button is included. By tapping on the item under the history tab, a dialog for managing the history item appears. You can move the expression under the macro tab using "Copy the expression to Macro tab" button.

hist1-dialog

The following is the screen-shot of the macro tab of McCal Viewer. A name ("Capacitor valtage in RC") was given to the macro.

viewer-macro

When you recalled this macro, McCal is ready for entering the value for frequency.

macro2

This expression can be recalled repetitively by pressing Mac button.

 

1.13 looping functions and code block

Calculator-users often need to perform repetitive tasks. Besides the Macro explained in the previous chapter, McCal has many functions for repetitive calculation. Assume we need to calculate the voltage in the above example at various frequencies, from 1000Hz to 3000Hz, at each 100 Hz step.

Press the  times key, which is in the second key pad (swipe the key pad horizontally).

 times 

times

The 'times' function takes two arguments. The first one is for the number of repetition. The second one is enclosed in a pair of curly braces. In McCal, this is called as 'code block', which can pass a set of multiple statements to the supporting functions for execution. Times function excutes the code block in the second argument 'n'-times: 'n' is the number in the first argument.

Let's excute the macro described in the previous section 20 times. The results are saved in an array:

 Dim   a   [20]   [2] 

Dim a[20][2]

We increase the frequency from 1000Hz by 100Hz. Times function treats the variable 'k' as a counter: the value of 'k' is set to zero at the beginning of the repetition, and incremented at the end of each repeat. Thus, the expression, f = 1000 + 100k, does the trick. 'k' is also used for the index of the array.

We save the frequencies and voltages in the array variable, a.

 times   20   →□   a   [k][0]   =   f   =   1000   +   100   k   ;   a   [k]   [1]   =   #abs   #frac   10   →□   1   +   2   i   π   f   c   r 

times20

We assume that the variables 'c' and 'r' are already set with the appropriate values. Let's take a look at the results using McCal Viewer.

Press  (McV)  button to start McCal Viewer. Then, choose the variable tab, and tap on the variable 'a'. Press "McCal Array Viewer" button in the pop-up dialog. You will see McCal Array Viewer is activated, and the table below is displayed.

times-3

McCal has other loop-control functions. We explain them in the chapter 2 of this document. These functions are designed for easy excution of short multiple statements. For longer programs, we recommend the use of a function, since the user can take advantage of the BASIC-like flow control statements explained later.

#if Conditional branching. #if(condition, {then_clause}, {else_clause}); McCal-only.
#while Repeats execution of the code block while the given condition is true. #while({condition}, {main_body}); McCal-only.
#loop Executes the given code block repeatedly. Unlike #times, #loop stops at the end of code block, and asks the user to continue or to stop. #loop({main_body}); McCal-only.
#sigma(Σ) Summation. #sigma(start, end, {main_body});
#repeat General repetition. #repeat(start, end, {initializer}, {main_body}, {finalizer});

In these functons, variable 'k' is used as the counter variable.

 

1.13 Defining a user-function

In this section, we define a function using the Function statement in the calculator window. The statement looks like:

Function #Package.function(a,r,g,s) { code block }

In McCal, related functions are organized into a 'Package'. To designate a function, the user is required to provide both package and function names. Arguments are listed between a pair of parentheses, and a code block follows, which describes the logic of the program. McCal expects that a package name starts with a upper-case letter, and a function name with a lower-case.

Here, we define a function for calculating the example listed in the previous section. First, press the  Func  key, which is located in the second key pad, and then enter the following package, function names, and an argument.

Function #■.■(■) { ■ }

 Func  elc  →□  volRC  →□  f  →□ 

Function #elc.volRc(f) { ■ }

We entered "Elc" in the package (the first charcter is automatically capitalized), "volRC" in the function name, and "f" in the argument list.

Caution: in McCal-lite, only one function, "Usr.test()", is allowed.

Then, start up MacCal Viewer, and select the macro explained in the section 1.11. We also added a #polar() function to show the amplitude and the phase of the voltage. Then we removed the "f = ■" expression, because the value for 'f' is supplied as the argument for the function.

Function #elc.volRC(f) {c=1E-8;
 r=5E3; v=#frac(10, 
1-i2#pi()fcr);#polar(v); 
Return v }

Is the new function registered in McCal? Let's start McCal Viewer ( (McV) ) to find out. Select #Fn." tab ⇨ Select "Elc" package. Now, you will see function "volRC(f) is listed in the view. Tap on the function, and select "Use the function" in the pop-up dialog. McCal calculator will show:

#Elc.volRC(■) 

Let's just fill the input box:

 2000  eval() 

Elc-func-user

In this example, a function was defined using Function statement in McCal. In the next section, we will define/edit functions using the source code editor of McCal Viewer.

 

1.14 Editing McCal Script

First, let's see whether McCal Viewer recognizes the function defined in the previous section. Press FV icon in the action bar, or  <Fn.>  button, then FunctionViewer starts up. The "Elc" package is actually present.

FunctionViewer hierarchically displays the system- and user-functions. At the top level, FunctionViewer shows an expandable list for the packages of the functions. Let's tap on the item.

funcList

As shown above, the source code defined in the previsous section appears in the Source View of FunctionViewer.

There are also two buttons, "Edit" and "Run", in the Source View of FunctionViewer. Pressing "Run" button leads to the main calculator window with the pasted string for the function name. "Edit" button results in activating McCal Editor for the function.

Here, let's make a new function which calls #Elc.volRC(f). Select the "Add a New Function" from the option menu of FunctionViewer. A display asking the name of the new function appears.

newfunc

Enter the name for the new function, "test()", and press OK. McCal Editor starts up. Write a program as shown below.

funcedit

After completing the function, press the menu button (upper-right corner), and then push the "Use the function" button. The function name appears in the main calculator window. Pressing the  eval()  key starts the function execution.

Tidbits about function editing:

McCal editor is modeled after a text editor. There are cursor-movement keys (indecated by the red-line above), and  CR  is for Carriage Return. To make the editor window large, the key pad was divided into five pages. You can switch the pages using the page-shift keys.

The fifth page is for BASIC-style control words. Please note that these control words start with upper-cased characters, eg. "For" and "If". These words are modeled after BASIC, so that the program is excuted on line-by-line basis. In contrast, the functions explained in section "Repetitive calculation and Code block" start with '#'. Those functions are designed for excuting very short code blocks, which should fit in one line.

Example of editing a program:

Let's make a program consisting of one "For" loop. Pressing  For  button in the fifthkey pad gives you a "For" loop template like below. "Step" is dispensable (in that case, "Step 1" is assumed).

 For 
For ■=■ To ■ Step ■
  
Next

We use variable "k" for the counter of the loop here.

k →□ 1000 →□ 3000 →□ 200
For k=1000 To 3000 Step 200
  
Next

As shown above, input boxes can be easily replaced with your text.

 ↓   Println  k,  <Fn.> 

Then we start up Function Viewer to get the function we defined in the previous section. This viewer is read-only. Select #Elc.volEC(f), and replace the input box with "k".

For k=1000 To 3000 Step 200
  Println k, #Elc.volRC(k)
Next

Now we get the above program. Press the menu button at the right-upper corner of the display, and try the function.

Another sample program:

The following function, #id(n) returns the identity matrix of n-by-n size. The n is limited to at most 20. As you can see, strings after "//" are comments.

// size-check
If n > 20 Then
  Println "Too Large"
  Return 0
End If
// declare an array
Dim a[n][n]
// Fill all elements for 
//  identity matrix.
For j=1 To n Step 1
  For k=1 To n Step 1
    If j==k Then
      a[j-1][k-1] = 1
    Else
      a[j-1][k-1] = 0
    End If
  Next
Next
// an array can be returned
Return a

 

1.16 Matrix calculation

McCal supports mathematical matrix calculation using two-dimensional array.

It has many useful Matrox operators and functions. Please see the Matrix Calculation Document.

 

1.16 Summary

One of the design goals of McCal is to provide users a familiar look and feel of a scientific calculator. Secondly, McCal is designed so that arithmetic equasions can be easily entered, and beautifully displayed in the way they would normally be written.

McCal remembers the script code translated from user-inputs. Users can recall them, and edit/recalculate them later. Indeed, McCal is equipped with an interpreter of McCal-Script. Since users can define and reuse a chunk of code in a single line of McCal-Scipt (called 'Macro' in McCal), it is easy to execute routine calculations repeatedly.

The following figure shows how the sub-programs of McCal calculator are activated. activities

 

1.17 Disclaimer

While every effort is made to ensure the correct operation and accuracy, MilimaCode cannot be held responsible for any errors resulting from its use.