« February 2008 | Main

March 19, 2008

Basic Math Operations

The following math functions are available in Smallworld Magik.  These are useful to know because many spatial programming methods require some sort of calculation.

Addition
2 +3 = 5

Subtraction
2 –3 = -1

Multiplication
2 *3=6

Division
2 / 3= 2/3
4/2 = 2

Exponentiation
2**3=8

Division of whole numbers
21 _div  5 = 4

Remainder
21 _mod 5 = 1 

Rounding
1.234.round(2) = 1.23
1.234.round(0) = 1
1.234.rounded = 1 (rounded returns nearest whole number) 

Ceiling (smallest integer greater than _self)
1.2.ceiling = 2 

Floor (largest whole number less than _self)
3.4.floor = 3 

Negative & Positive
4.negative? = False
0-4.negative? = True
4.positive? = True
0-4.positive? = False

Absolute Value
(0-4).abs = 4 

Force Float
2/3.as_float = 0.6666666667

Radians & Degrees
100.degrees_to_radians = 1.745329252
3.141892654.radians_to_degrees.rounded = 180

Greatest Common Divisor
15.gcd(25) = 5
100.gcd(75) = 25

Lowest Common Multiple
2.lcm(3) = 6
5.lcm(10) = 10

Hypotenuse  (returns sqrt(_self*_self + other*other))
3.hypot(4) = 5

Inverse
5.inverse = 1/5
5.inverse.as_float = 0.2

Square Root
4.sqrt = 2
5.sqrt = 2.236067977

Integer Square Root
4.isqrt = 2
5.isqrt = 2

Log base 10 and Natural Log
5.ln = 1.609437912
5.log10 = 0.6989700043

Max & Min
3.min(7) = 3
3.max(7) = 7

Even & Odd
3.even? = False
3.odd? = True

Prime
3.prime? = True
4.prime? = False

There are some other mathematical functions available, including basic trigonometry functions as well as some basic calculus functions.  Happy calculating.