Math functions
Square roots, powers and more.
Math functions
Square roots, powers and more.
0
0
Checkbox to mark video as read
Mark as read

Previously, we have seen how with some mathematical operators we can add or multiply, but, what about more complex mathematical operations like square root, power, or trigonometric functions?

Like any other programming language, Swift provides the most common mathematical functions.

  • Square Root.
  • Raise to any power.
  • Pi number and Euler constant.
  • Sine, cosine, and tangent.
  • Arcsine, arccosine, and arctangent.
  • Maximum and minimum.
  • Rounding.
  • Absolute value and sign.
  • exp, log, and log10.

Square Root

The sqrt() function returns the square root of the number we pass as a parameter.

sqrt(121) // 11

Raise to any power

With the pow() function, we can pass the base as the first parameter and the exponent as the second parameter.

pow(4, 3) // 64

Pi number and Euler constant

We can find both the Pi number and the Euler constant as system constants.

print(Double.pi) // Console shows: 3.141592653589793
print(M_E) // Console shows: 2.718281828459045

Sine, cosine, and tangent

To use these functions, we must pass our angle in radians, hence why we multiply the 40-degree angle by pi divided by 180.

var angle = 40.0 * Double.pi / 180

sin(angle) // 0.6427876096865393
cos(angle) // 0.7660444431189781
tan(angle) // 0.8390996311772798

Arcsine, arccosine, and arctangent

Just like sine, cosine, and tangent functions work with radians, the following functions will also return results in radians. So, if we use the results from the previous point and convert them back to degrees, we should get 40 in all three cases.

asin(0.6427876096865393) * 180 / Double.pi // 39.99999999
acos(0.7660444431189781) * 180 / Double.pi // 39.99999999
atan(0.8390996311772798) * 180 / Double.pi // 39.99999999

Since a Double cannot store all decimals, we have lost some precision.

Maximum and minimum

We can obtain the maximum or minimum between two or more numbers in case we need to delimit a value.

let minimum = min(1, 5, 2, 19, 0)
let maximum = max(9, 2, 23, 12, 9)

print(minimum) // Console shows: 0
print(maximum) // Console shows: 23

Rounding

If we have a value with decimals and want to remove them to get an integer, we have different options. We can round to the nearest integer, depending on whether the decimals are greater or less than 0.5. We can also round to the highest or lowest integer, regardless of the decimals, with the floor and ceil functions.

round(2.1) // 2
round(2.7) // 3

floor(2.1) // 2
floor(2.7) // 2

ceil(2.1) // 3
ceil(2.7) // 3

Absolute value and sign

abs() returns the absolute value of a number, and signum() returns the sign, with 1 for positive and -1 for negative.

abs(9) // 9
abs(-8) // 8

7.signum() // 1
-5.signum() // -1

exp, log, and log10

Lastly, we have the functions exp(), log(), and log10(), to which we must pass a Double as a parameter.

let myNumber: Double = 3.0

exp(myNumber) // 20.08553692318767
log(myNumber) // 1.09861228866811
log10(myNumber) // 0.4771212547196624

course

Quiz Time!

0 Comments

Join the community to comment
Sign Up
I have an account
Be the first to comment

Accept Cookies

We use cookies to collect and analyze information on site performance and usage, in order to provide you with better service.

Check our Privacy Policy