ODCalc
Operators

Operators

The calculator has operators for expressing calculations, such as plus or minus. The full list of operators appears below. They appear essentially in the order of precedence in which they are interpreted in the calculator.

Unit Operators

u:², u:³Unit square, unit cube
√:u, ∛:u, ∜:uUnit square root, unit cube root, unit fourth root
u^:nUnit power
u/:u, u per uUnit divide
u*:u, u⋅u, u-:uUnit multiply
n in uConversion operator

Mathematical Operators

+n, -nUnary plus, unary minus
n'Transpose
n%, n‰, n‱Percent, Permille, Basis points
n², n³Square, cube
∠nAngle of
√n, ∛n, ∜nSquare root, cube root, fourth root
n^n, n**nMatrixwise power
n.^n, n.**nElementwise power
n√nnth root
n*n, n×n, n∗nMatrixwise times
n.*nElementwise times
n/n, n∕nMatrixwise divide
n./n, n.∕n, n÷nElementwise divide
n\nLeft divide
n%nModulus
n+n, n-nPlus, minus
n∠nAngle entry
n<<nShift left
n>>nShift right
n>>>nUnsigned shift right
n<nLess than
n<=n, n≤nLess than or equal to
n>nGreater than
n>=n, n≥nGreater than or equal to
n==nEquals
n!=n, n~=n, n≠nNot equals
n≈nApproximately equals
n||n, n∥nParallel
n:n:nRange (specified step)
n:nRange (step of 1)

Logical Operators

!b, ~bNot
b==b,Equals
b!=b, b~=b, b≠bNot equals
b&b, b∧bAnd
b⊕bExclusive or
b|b, b∨bOr
b&&bShortcut and
b||b, b∥bShortcut or
b?n:nChoice operator

String Operator

s+sCombine (Concatenate)

Assignment Operators

n++, n--Increment, decrement
++n, --nIncrement, decrement
x^=nPower assignment
x*=n, x×=n, x∗=nMatrixwise multiply assignment
x.*=nElementwise multiply assignment
x/=n, x∕nMatrixwise divide multiply assignment
x./=nElementwise multiply assignment
x%=nModulus assignment
x+=n, x-=nPlus assignment, Minus assignment
x&=b, x∧=nAnd assignment
x|=b, x∨=nOr assignment
x&&=nShortcut and assignment
x||=n, x∥=nShortcut or assignment
x<<=nLeft shift assignment,
x>>=nright shift assignment,
x>>>=nunsigned right shift assignment
x=nAssignment

Unit Operators

Conversion Operator: in

Units

The in operator can be used to convert a value in one unit to another unit or display the value in another base or form. For example convert a length in metres to feet

2m in ft
6.56168 ft

If a scale factor is used in the unit to convert to then the value will be displayed using that scale factor even if it is not the optimal scale factor for that value

10A in uA
10000000 µA

Note that the in operator only needs to be used to show a value in a particular unit, the calculator automatically manages conversion of units to work consistently when applying mathematical operators

2m + (6ft in m)
3.8288 m
2m + 6ft
3.8288 m

In can only convert between units that are measures of the same thing (specifically have the same dimensions)

2kg in ft
Can't convert quantity into specified unit because the new and old units have different dimensions: gram, foot

The in operator can also be used to attach a unit to dimensionless (without unit) quantities for example if it is generated using a function or other operator

1:3 in A
[1, 2, 3] A
rand() in A
154.23547 mA

Similarly the unit of any quantity can be removed by using the in operator with unit 'dimensionless' or 'one' on the right hand side

33A in dimensionless
33

Note that removing units from quantities in this way is not generally recommended because it doesn't usually make physical sense and because it requires making assumptions about exactly which unit the value is in.

Bases

The in operator can also be used to display a result in a base other than that currently chosen as the default in the calculator. For example to display a value in hexadecimal

2748 in hex
0xabc

The supported bases are shown in the following table

BaseShort formLong form
2binbinary
8octoctal
10decdecimal
16hexhexadecimal

Several in operators can be chained up for expression in a more specific forms, however only the right most unit in, for example, sets the unit will actually appear

100lbs in tonnes in oz in g in hex
0xb12f.3cac1 g

Parts-per Form

The in operator can also be used to display dimensionless quantities in forms such as percent or parts per million

0.1 in %
10 %

The supported forms appear in the following table

Parts perShort formLong form
Percent100%, pc, pct, pct.percent
Permille1000permille, permill, permil
Basis points10000‱, bpbasis points
Parts per million1000000ppm
Parts per billion109ppb
Parts per trillion1012ppt
Parts per quadrillion1015ppq

Note that, as with base conversions, parts-per conversions don't change the value itself, they merely change the way that it is displayed.

(1 in %) + 1
2
(1%) + 1
1.01

Display Precision

Usually when doing real world calculations you don't need a very high degree of precision in the result because most physical systems don't operate with such precision. To make it easy to use the results of the calculations ODCalc by default rounds its answers to about 5 decimal places (less are shown if the final digits would be zeros).

pi
3.14159

Internally ODCalc works to a much higher degree of precision (specifically it uses double precision floating point numbers). If more precision in the result is needed then this can be seen using the 'in full' operator

pi in full
3.141592653589793

Other Unit Operators

The other unit operators – unit power, unit multiply, unit divide, unit square, cube, square root, cube root and fourth root – allow complex units to be entered. They also allow the unit of a value to be modified, for example (1m + 1ft) /: second = 1.3048 m/s, however this use is discouraged, (1m + 1ft) / 1 second = 1.3048 m/s makes more physical sense. Further details of these unit operators can be seen in the entering numbers section

Mathematical Operators

ODCalc supports a wide set of mathematical operators, many of which will be familiar from use of a desktop calculator or other programming language. There are a few differences, however, such as the matrixwise vs elementwise operators, parallel operator, range operator, percent operator, etc., which will be covered in more detail in this section.

Adding and Subtracting

Addition and subtraction are performed using the + and - operators

2 + 2 – 1
3

They also act as unary minus (i.e. change sign) and unary plus

-5 + 3
-2

If both the arguments are arrays or matrices of the same size then the operation is performed elementwise, that is each element of the result is obtained by applying the operator to the corresponding element of each argument array or matrix

[1, 2, 3, 4] + [9, 8, 7, 6]
[10, 10, 10, 10]

If the arguments were of different sizes in the previous example the calculator would have shown an error message. If one of the arguments is a scalar (singular) value and the other an array or matrix then the result is obtained by applying the operator between each element of the array or matrix and the scalar value

10 + [1, 2, 3, 4]
[11, 12, 13, 14]
[11, 12, 13, 14] – 10
[1, 2, 3, 4]

Multiplication and Division

Multiplication and division use the * and / operators, however these come in two forms, matrixwise (* and /) and elementwise (.* and ./). If either argument is a scalar then both forms of the operators work identically

2.*3
6
[1, 2, 3] / 2
[0.5, 1, 1.5]
7 * [1, 2, 3]
[7, 14, 21]

However if both arguments are arrays or matrices then the operators work differently. The elementwise operators apply to the corresponding elements of each array or matrices (both must be the same size)

[1, 2, 3] .* [1, 10, 100]
[1, 20, 300]
[1, 2, 3] ./ [1, 10, 100]
[1, 0.2, 0.03]

where as the matrixwise operators perform matrix multiply and matrix solve respectively

[1, 2; 3, 4] * [4, 3; 2, 1]
[8, 5; 20, 13]

Powers

Like multiply and divide, the power operator ^ or ** comes in an elementwise and a matrixwise forms. In both cases the second argument must be a scalar, where as the first can be a scalar or a vector or matrix. The elementwise form applies the power to each element in turn

2^8
256
[1,2,3].^2
[1, 4, 9]

where as the matrixwise form (will!) perform matrix multiplication on the first arg the number of times specified by the second argument (thus the first argument must be square and the second must be a positive integer).

Operators for specific powers (square ², cube ³, square root √, cube root ∛ and fourth root ∜) all work in an elementwise manner

√16 + 2²
8

Parts-per Operators

The percent %, permille‰, and basis points ‱ operators can be used to enter values as, for example, percentages, dividing the value that precedes the operator by 100.

5%
0.05

When used on arrays or matrices the operator applied to each element individually

[1,2,3]%
[0.01, 0.02, 0.03]

Angle Entry

The angle entry operator ∠ can be used to enter a complex number using its magnitude and argument (angle)

1∠45deg
0.70711 +0.70711i

The angle argument should be a number with units of angle, or (less preferred) can be a dimensionless number in which case the assumed angle unit is taken from settings within the calculator.

1∠(pi/4 in rad)
0.70711 +0.70711i
1∠pi/4
0.70711 +0.70711i

The magnitude argument can take a unit if desired. It may also be a complex number in which case the complex number is rotated in the complex plane by the given angle

1km∠45deg
707.10678 +707.10678i m
(1+i)∠90deg
-1 +1i

Either or both of the inputs may be complex numbers in which case the operator acts in an elementwise fashion as discussed in the multiplication and division section.

1∠(0deg:60deg:360deg)
[1, 0.5 +0.86603i, -0.5 +0.86603i, -1, -0.5 -0.86603i, 0.5 -0.86603i, 1]

Angle Of

The angle of operator ∠ can be used get the argument (angle) of a complex number

∠(-1+i)
2.35619 rad

The value returned has dimensions of angle, the unit to be used in specified in settings within the calculator, though of course it can be converted to any other angle unit using the in operator

∠(-1+i) in deg
135 º

The input may be an array or matrix in which case the result is an array or matrix with the same size as the input and each element set by applying the angle of operators to the corresponding input element

∠[1, i, -1, -i] in deg
[0, 90, 180, -90] º

Parallel

The parallel operator (∥ or two pipe symbols ||) performs the common engineering function of giving the equivalent impedance of two elements in parallel, i.e. applies the following formula
A∥B=1/(1/A+1/B)

100ohm || 100ohm
50 Ω
abs( 10kohm || (1/(i*2*pi*500kHz*1nF)) )
318.14875 Ω

If either of the arguments are arrays or matrices the operator is applied in an elementwise manner as explained the multiplication and division section.

1kohm || [680, 1k, 1.5k] ohm
[404.7619, 500, 600] Ω

Range

The range operator is useful for creating arrays of values between specified limits, for example between ten and fifteen

10:15
[10, 11, 12, 13, 14, 15]

The gap between entries can also be specified by using the three argument form, for example between ten and fifteen with a gap of two

10:2:15
[10, 12, 14]

Note here that it will fill the range until it encounters a value bigger than the last argument.

The operator is particularly useful for marking off repeated distances during mechanical design, for example two inch divisions between 1.2 and 1.6 metres

1.2m : 2" : 1.5m
[1.2, 1.2508, 1.3016, 1.3524, 1.4032, 1.454] m

or the position of the pins of a 4 way 0.1” gap connector starting at 20mm in an electrical design

20mm + 0.1"*(0:3)
[20, 22.54, 25.08, 27.62] mm

The two argument form can also be used with units in which case the gap is the size of unit of the first argument

1ft:2m
[0.33333, 0.66667, 1, 1.33333, 1.66667, 2] yd

Comparison Operators

The comparison operators equals (==), not equals (!=, ~= or ≠), approximately equals (≈), less than (<), less than or equal to (<= or ≤), greater than (>) and greater than or equal to (>= or ≥) allow comparison of number values by size, returning logical values as to whether the conditions are true or false.

1 == 1
true

Units are supported in which case both arguments must have units with the same dimensions

1inch == 25.4mm
true

Either or both arguments may be arrays or matrices in which case the operator is applied in an elementwise manner as explained in the multiplication and division section.

2 < [1, 2, 3]
[false, false, true]

If the argument are complex numbers then both the real and imaginary parts of the number must be the same for equals and approximately equals to return true and for not equals to return false

1+i == 1
false
1+i == 1+i
true

The greater than, less than, greater than or equal to and less than or equal to operators, however, ignore the imaginary part of numbers when making comparisons

2 > 1+2i
true
2 > 3+2i
false

The approximately equals operator is included in ODCalc to address machine precision in comparisons. Calculations in computers are performed to within a certain accuracy (they can only keep track of a finite number of digits) meaning that small errors can creep into calculations. For example consider the following sum which results in a number that is very close to but not exactly zero as it should be

5.55 - 3.33 - 2.22
-4.44089e-16

The approximately equals operator is tolerant of these small errors when performing comparisons

5.55 - 3.33 - 2.22 == 0
false
5.55 - 3.33 - 2.22 ≈ 0
true

This is particularly useful when comparing quantities that require conversion between units. (Precisely the approximately equals operator returns true if the quantities are within 100 machine epsilons of each other.)

Logical Operators

Logic operators work on logical values, all returning logical values as a result. When arguments are arrays or matrices the operators are performed elementwise as described in the multiplication and division section.

Not

The not operator, ! or ~ inverts the value

!true
false
![false, true]
[true, false]

And, Or, Xor

The and operator (& or ∧), or operator (| or ∨) and xor operator (⊕) perform the and, or and xor functions as explained in the following truth table results

[false, false, true, true] & [false, true, false, true]
[false, false, false, true]
[false, false, true, true] | [false, true, false, true]
[false, true, true, true]

Shortcut And and Or

The shortcut and (&&) and or (||) operators work similarly to the standard and and or operators except that they don't evaluate the second argument if the result is already determined by the first argument. For example when evaluating true || false, once the initial true has been found it is not necessary to examine the second argument to determine the result. For constant arguments it makes little difference which form is used (though the shortcut forms as slightly faster)

true | false
true
true || false
true

However these forms are particularly useful when using functions that either modify the state of the system or require the arguments to have certain forms. Note that the shortcut and and or operators only work with scalar arguments.

String Operator

Combine (Concatenate)

The string append operator (+) combines two strings together into a single longer string

"abc"+"def"
"abcdef"

If either of the arguments are variables of some type other than string they will first be converted to strings before being appended

"I guess your weight to be " + (rand() in tonnes)
"I guess your weight to be 0.14748 t"
true + " | " + false + " = " + (true | false)
"true | false = true"

For arrays and matrices append works in an elementwise manner

"try " + (1:3)
["try 1", "try 2", "try 3"]

Assignment Operators

The assignment operators store values as variables in the context such as the workspace. In ODCalc variables must be single words without spaces that don't start with a number or an operator character. The must also not begin with the name or symbol of a currency. Once defined they can be used in further calculations until they either go out of scope or are cleared from the workspace.

Equals Assignment

Equals is the most common assignment operator which stores a value in a variable

aaa = 1
1
aaa + 2
3

Unless specified, variables in ODCalc can take any type and be changed to different types even after being declared.

aaa = "happy"
"happy"

See Also

Getting Started Entering Data Units Operators Accessing Open Data Vectors Functions