Skip to main content

Math Module

The Math module provides mathematical constants and functions.

You can import everything provided by the Math module with:

import * from math

Constants

e: Float

The mathematical constant e.

print ("\{e}") // Prints 2.718281828459

max_integer: Int

The max_integer constant is the maximum signed Int value stored on 64 bits.

print ("\{max_integer}") // Prints 9223372036854775807

min_integer: Int

The min_integer constant is the minimum signed Int value stored on 64 bits.

print ("\{min_integer}") // Prints -9223372036854775808

pi: Float

The pi mathematical constant.

print ("\{pi}") // Prints 3.1415926535898

Functions

abs(_ value: Int) -> (Int)

Returns the absolute value of an integer value.

var a = abs(-12) // a equals 12

absf(_ value: Float) -> (Float)

Returns the absolute value of a floating-point value.

var f = abs(-34.5) // f equals 34.5

acos(_ value: Float) -> (Float)

Returns the arccosine of a floating-point value in radians.

var a = acos(1.0) // a equals 0.0

asin(_ value: Float) -> (Float)

Returns the arcsine of a floating-point value in radians.

var a = asin(0.0) // a equals 0.0

atan(_ value: Float) -> (Float)

Returns the arctangent of a floating-point value in radians.

var a = atan(1.5) // a equals 0.98279372324733

cos(_ value: Float) -> (Float)

Returns the cosine of a floating-point value in radians.

var a = cos(0.0) // a equals 1.0

exp(_ value: Float) -> (Float)

Returns the e to the power of the given value.

print("\{exp(1.0)}") // Prints the constant e, namely 2.718281828459

log(_ value: Float) -> (Float)

Returns the natural logarithm of a value.

var a = log(1.0) // a equals 0.0

max(_ value1: Int, _ value2: Int) -> (Int)

Returns the largest of two given integer values.

var a = max(3, 12) // a equals 12

maxf(_ value1: Float, _ value2: Float) -> (Float)

Returns the largest of two given floating-point values.

var a = max(3.14, 12.7) // a equals 12.7

min(_ value1: Int, _ value2: Int) -> (Int)

Returns the smallest of two given integer values.

var a = min(3, 12) // a equals 3

minf(_ value1: Float, _ value2: Float) -> (Float)

Returns the smallest of two given floating-point values.

var a = minf(3.14, 12.7) // a equals 3.14

random(from: Int, to: Int) -> (Int)

Generates a random integer value that is between the two given values, following a uniform distribution.

var a = random(from: -10, to: 1000) // a will be a random number between -10 and 1000

randomf(from: Float, to: Float) -> (Float)

Generates a random floating-point value that is between the two given values, following a uniform distribution.

var a = randomf(from: 45.4, to: 45.9) // a will be a random number between 45.4 and 45.9

sin(_ value: Float) -> (Float)

Returns the sine of a floating-point value in radians.

var a = sin(0.0) // a equals 0.0

sqrt(_ value: Float) -> Float

Returns the square root function of the given value.

var a = sqrt(4.0) // a equals 2.0