Swift Tutorial Swift References

Swift - Math Module



The Swift has a Math module which contains number of methods and constants that are available for mathematical operation. The methods and constants of Math module are listed below:

Trigonometric Functions

FunctionsDescription
sin() Returns the trigonometric sine of an angle in radians.
cos() Returns the trigonometric cosine of an angle in radians.
tan() Returns the trigonometric tangent of an angle in radians.
asin() Returns the arc sine of a value.
acos() Returns the arc cosine of a value.
atan() Returns the arc tangent of a value.
atan2() Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).

Hyperbolic Functions

FunctionsDescription
sinh() Returns the hyperbolic sine of a value.
cosh() Returns the hyperbolic cosine of a value.
tanh() Returns the hyperbolic tangent of a value.
asinh() Returns the inverse hyperbolic sine of a value
acosh() Returns the inverse hyperbolic cosine of a value
atanh() Returns the inverse hyperbolic tangent of a value

Exponential & Logarithmic Functions

FunctionsDescription
exp() Returns the exponent of e.
expm1() Returns the exponent of e minus 1, i.e., ex-1.
exp2() Returns the exponent of 2, i.e., 2x.
log() Returns the natural logarithm of a given number.
log1p() Returns the natural logarithm of (1+number), i.e., log(1+number).
log10() Returns the base-10 logarithm of a given number.
log2() Returns the base-2 (binary) logarithm of a given number.

Rounding & remainder Functions

FunctionsDescription
ceil() Rounds the given number up to the nearest integer.
floor() Rounds the given number down to the nearest integer.
fmod() Computes remainder of a division.
remainder() Computes remainder of a division.
remquo() Computes remainder and quotient of a division.
trunc() Rounds the given number towards zero.
rint() Rounds the given number to nearby integral value.
lrint() Rounds and casts the given number to Int value.
llrint() Rounds and casts the given number to Int64 value.
round() Rounds the given number to nearby integral value.
lround() Rounds and casts the given number to Int value.
llround() Rounds and casts the given number to Int64 value.
nearbyint() Rounds the given number to nearby integral value.

Power Functions

FunctionsDescription
pow() Returns base raised to the power of exponent.
sqrt() Returns the square root of the given number.
cbrt() Returns the cube root of the given number.
hypot() Returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2).

Error & gamma Functions

FunctionsDescription
erf() Returns the error function of the argument.
erfc() Returns the complementary error function of the argument.
tgamma() Returns gamma function of the argument.
lgamma() Returns log-gamma function of the argument.

Other Functions

FunctionsDescription
abs() Returns the absolute (positive) value of the given number.
fma() Fused multiply-add operation.
fmax() Returns the number with maximum value.
fmin() Returns the number with minimum value.
fdim() Returns positive difference.
frexp() Get significand and exponent.
scalbn() Scale significand using floating-point base exponent (Int32).
scalbln() Scale significand using floating-point base exponent (Int).
nextafter() Returns next representable value.
nexttoward() Returns next representable value toward precise value.
logb() Returns the floating-point base logarithm of a given number.
ilogb() Returns the integer part of the floating-point base logarithm of a given number.
copysign() Returns a number with magnitude of first argument and sign of second argument.
modf() Break the given number into fractional and integral parts.