Skip to main content

math

The Math package provides mathematical constants and functions.

You can import everything provided by the Math package with:

import * from math

Constants​

e​

e: Float

The mathematical constant e.

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

max_integer​

max_integer: Int

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

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

min_integer​

min_integer: Int

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

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

pi​

pi: Float

The pi mathematical constant.

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

Functions​

abs​

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

Returns the absolute value of an integer value.

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

absf​

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

Returns the absolute value of a floating-point value.

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

acos​

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

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

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

asin​

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

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

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

atan​

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

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

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

atan2 (8.10)​

atan2(y: Float, x: Float) -> (Float)

Computes the arc tangent of y / x using the signs of arguments to determine the correct quadrant.

var arctangent2 = atan2(y: 7, x: 0) // arctangent2 equals 1.5708

cos​

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

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

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

exp​

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​

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

Returns the natural logarithm of a value.

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

max​

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

Returns the largest of two given integer values.

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

maxf​

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

Returns the largest of two given floating-point values.

var maximumf = maxf(3.14, 12.7) // maximumf equals 12.7

min​

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

Returns the smallest of two given integer values.

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

minf​

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

Returns the smallest of two given floating-point values.

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

random​

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

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

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

randomf​

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

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

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

sin​

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

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

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

sqrt​

sqrt(_ value: Float) -> Float

Returns the square root function of the given value.

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