Math
Fundamental to any computer program is the ability to do mathematical computations.
The Math Category provides the repertoire of mathematical functions shown in Fig. 65.
Number
This value block represents a fixed number that is specified by editing the default number 123
in the block.
The number can be any valid integer or floating point number:
the number can be signed, that is, preceded by the character
+
(default and assumed if not present) or the character-
for negative numbersthere is no limit (other than computer memory) for how large the number can be
an integer in the form
123456
a floating point number in the form
123456.789
scientific notation in the form
1.234567e5
can be used and will be displayed in integer or floating point form as appropriate123456.7
Number Example
This example prints a number on the Kookaberry’s display:
Arithmetic
This value block operates on two input values or value blocks that represent numbers with the chosen arithmetic operator.
The operations that can be chosen from the drop-down list are:
addition (+)
subtraction (-)
multiplication (x)
division (÷)
and raised to the power of (^)
Arithmetic Example
This example prints the result of 2
raised to the power of 3
(ie. 2 cubed which is 8
) on the Kookaberry’s display:
Multiply and Add
This value block multiplies the first numerical value block input by the second numerical value block input and then adds the third numerical value input to the product of the first two inputs.
This block is a convenient way to achieve the same result as using two Arithmetic blocks as in the example below.
Both blocks will print the same result (10
).
Scale Function
The Scale value block will perform the necessary computations to convert the number on the first input from a scale defined by the second input, to another scale defined by the third input.
Scale Example
By way of example, this script using the Scale block will convert a Celsius water temperature sensor reading
(range freezing point 0
to boiling point 100
) into the equivalent degrees Fahrenheit
(range freezing point 32
F to boiling point 212
F) and print it on the Kookaberry’s display.
Math Function
This value block performs the chosen mathematical function on the numerical value input.
The options that are available are:
square root - gives the number that when multiplied by itself is equal to the input - see https://en.wikipedia.org/wiki/Square_root
absolute - the unsigned magnitude of the input value - see https://en.wikipedia.org/wiki/Absolute_value
- - changes the input number’s sign from positive to negative or negative to positive - the same as multiplying by
-1
ln - natural (base e) logarithm of the input number - see https://en.wikipedia.org/wiki/Natural_logarithm
log10 - base 10 logarithm of the input number - see https://en.wikipedia.org/wiki/Logarithm
e^ - the constant e raised to the power of the input number - see https://en.wikipedia.org/wiki/Exponential_function
10^ - 10 raised to the power of the input number - see https://en.wikipedia.org/wiki/Exponentiation
Trigonometric Function
This value block performs the basic selected trigonometric functions. on the input numerical angles.
The functions available for selection in the drop-down list are:
sin - sine of the input angle - see https://en.wikipedia.org/wiki/Sine_and_cosine
cos - cosine of the input angle - see https://en.wikipedia.org/wiki/Sine_and_cosine
tan - tangent of the input angle - see https://en.wikipedia.org/wiki/Trigonometric_functions
asin - arc-sine of the input value - the inverse of sine.
acos - arc-cosine of the input value - the inverse of cosine.
arc-tangent (
atan
) of the input value - the inverse of tangent.
The functions sin, cos and tan expect the input to be in degrees.
The outputs for these functions are floating point numbers between -1
and +1
inclusive.
The inverse functions asin, acos and atan expect the input to be floating point numbers between -1
and +1
.
The outputs will be in degrees ranging from -180
to +180
inclusive.
See also https://en.wikipedia.org/wiki/Trigonometric_functions
Special Constants
This value block provides several special constants that are important and often used numbers in mathematics.
To choose a constant use the drop-down list and select from
π - pi used in dealing with circles - see https://en.wikipedia.org/wiki/Pi
e - Euler’s number used in exponential function - see https://en.wikipedia.org/wiki/E_(mathematical_constant)
ϕ - the Golden Ratio phi - see https://en.wikipedia.org/wiki/Golden_ratio
sqrt(2) - the square root of 2 - see https://en.wikipedia.org/wiki/Square_root_of_2
sqrt(½) - the square root of ½ - see https://en.wikipedia.org/wiki/Square_root_of_2#Multiplicative_inverse
∞ - infinity - see https://en.wikipedia.org/wiki/Infinity
For a list of most of the mathematical special constants see https://en.wikipedia.org/wiki/List_of_mathematical_constants
Number Property Test
This value block gives a Boolean value of True
or False
depending on whether the numerical input value
has the chosen property or not.
The property to test is selected from the drop-down list which includes:
even - whether the input is divisible by
2
- see https://en.wikipedia.org/wiki/Parity_(mathematics)odd - whether the input is not divisible by
2
- see https://en.wikipedia.org/wiki/Parity_(mathematics)prime - whether the input is divisible only by
1
and itself - see https://en.wikipedia.org/wiki/Prime_numberwhole - whether the input when divided by
1
leaves no remainder - see https://en.wikipedia.org/wiki/Whole_numberpositive - whether the input is greater than
0
- see https://en.wikipedia.org/wiki/Sign_(mathematics)negative - whether the input is less than
0
- see https://en.wikipedia.org/wiki/Sign_(mathematics)divisible by - whether the input when divided by the number in the second input leaves no remainder. If divisible by is selected it will add a second input socket for the number to test against. - See https://en.wikipedia.org/wiki/Remainder
Round Number
This value block rounds the numerical input value to a whole number using the chosen method.
The method is chosen from the block’s drop-down list:
round - rounds the number in the standard manner, if the fraction is greater than or equal to 0.5 it rounds up to the next more positive whole number, and if the fraction is below 0.5 the block rounds down towards the negative direction.
round up - if there is a fractional component the block always rounds up to the next more positive whole number.
round down - removes any fractional component.
Input numbers are floating point and output numbers are integers.
round up means in the positive direction.
round down means in the negative direction.
List Operations
This block computes a mathematical function based on the content of a List which is connected to the input to the block.
The function to be used is selected from the drop-down list:
sum - computes the arithmetic sum of the members of the List - see https://en.wikipedia.org/wiki/Summation
minimum - returns the number with the minimum value from the List - see https://en.wikipedia.org/wiki/Maximum_and_minimum
maximum - returns the number with the maximum value from the List - see https://en.wikipedia.org/wiki/Maximum_and_minimum
average - returns the arithmetic mean of the items in the List - see https://en.wikipedia.org/wiki/Arithmetic_mean
median - returns the arithmetic median of the items in the List - see https://en.wikipedia.org/wiki/Median
modes - returns a List of the most numerous items in the List (example below) - see https://en.wikipedia.org/wiki/Mode_(statistics)
standard deviation - computes the statistical standard deviation of the items in the List - see https://en.wikipedia.org/wiki/Standard_deviation
random item - returns an item from the List that has been selected at random - see also https://en.wikipedia.org/wiki/Random_variable
Note
All functions except modes and random require that the input List contain only numerical or Boolean items.
Boolean items are evaluated as False
= 0
and True
= 1
.
The modes and random functions accept Lists with members of any type, i.e. numeric integer and floating point, boolean, and character strings.
List Operations Example
This is an example of the use of modes. The input List contains [-123, 123, 123, -123]
.
The block returns a List of the most numerous items in the List, being [-123, 123]
.
If we changed the input List to [-123, -123, 123, -123]
, the block would return [-123]
, a List of one item being the most numerous.
Remainder
This block returns the fractional portion of the number that results when the number at the first input is divided by the number at the second input.
For example, when 3
is divided by 2
the result is 1.5
. The remainder is the fractional portion which is 0.5
.
Constrain
This block constrains the number at the first input to be between the minimum number defined as the second input and the maximum number defined as the third input.
There are three possible outputs from this block:
if the input number is less than the minimum number, the output will be set to the minimum number.
if the input is between the minimum and maximum inclusive, the number is passed through as-is.
if the input number is greater than the maximum number, the output will be set to the maximum number.
Random Integer
This block generates an integer number that is constrained to be from a minimum integer defined by the first input, and a maximum integer defined by the second input.
For example, to simulate the roll of a six-sided die, set the minimum to 1
and the maximum to 6
.
Random Fraction
This value block creates a random floating point number from 0 up to but not including 1.
Atan2 of X
This value block returns the arc tangent of two numerical values at inputs x and y .
This function is similar to calculating the arc tangent of y/x, except that the signs of both arguments are used to determine
the quadrant of the result. The result is an angle expressed in degrees in the range -180
to +180
.
See also https://en.wikipedia.org/wiki/Atan2