Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity
Kotlin

The Math library

Kotlin provides a set of functions to perform math calculations. They can be used in a wide variety of domains such as statistics, geometry, machine learning, cryptography, and more.


There are two mathematical libraries to be used:
  • Math inherited from Java (only for the Java platform)
  • kotlin.math developed specially for Kotlin (since Kotlin 1.2, it provides common math for the Java platform and JS)
Both libraries have similar math functions, which work in the same way. In this lesson, we will use the first library.

List of functions

The most common functions are listed below.
  • Math.abs(...) returns the absolute value of its argument;
  • Math.sqrt(...) returns the square root of its argument;
  • Math.cbrt(...) returns the cube root of its argument;
  • Math.pow(..., ...) returns the value of the first argument raised to the power of the second argument;
  • Math.log(...) returns the natural logarithm of its argument;
  • Math.min(..., ...) returns the smaller value of two arguments;
  • Math.max(..., ...) returns the greater value of two arguments;
  • Math.toRadians(...) converts an angle measured in degrees to an angle measured in radians (approximately);
  • Math.sin(...) returns the trigonometric sine of the given angle in radians;
  • Math.cos(...) returns the trigonometric cosine of the given angle in radians;
  • Math.tan(...) returns the trigonometric tangent of the given angle in radians;
  • Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0;
  • Math.floor(...) returns the largest double value that is less than or equal to its argument and is equal to an integer;
  • Math.ceil(...) returns the smallest double value that is greater than or equal to its argument and is equal to an integer;
  • Math.round(...) returns the closest integer to the argument (rounding according to the rules of arithmetic).

Also, this class has two useful constants:

  • Math.PI - the ratio of the circumference of a circle to its diameter;
  • Math.E - the base of the natural logarithm.

Examples

Let's see all the listed functions in practice.

val abs = Math.abs(-10)     // 10
val dabs = Math.abs(-10.33) // 10.33

val sqrt = Math.sqrt(2.0)   // 1.4142...
val cbrt = Math.cbrt(27.0)  // 3.0

val log = Math.log(Math.E * Math.E)  // 2 

val square = Math.pow(5.0, 2.0) // 25.0
val cube = Math.pow(2.0, 3.0)   // 8.0

val min = Math.min(11, 81) // 11
val max = Math.max(20, 30) // 30

val grad = Math.toRadians(30.0) // 0.5235...

val pi = Math.PI // pi is 3.1415...
val sin = Math.sin(pi / 2) // 1.0
val cos = Math.cos(pi) // 1.0
val tan = Math.tan(pi / 4) // 0.99999999... (an inaccurate result)

val random = Math.random() // a random value >= 0.0 and < 1.0

val floor = Math.floor(3.78) // 3.0
val ceil = Math.ceil(4.15)   // 5.0
val round = Math.round(4.15) // 4 (not 4.0!)

The Pythagorean Theorem

Let's assume we have a right triangle (one angle is 90 degrees). We know the lengths of both sides: a = 3.0 and b = 4.0.

To calculate the length of the hypotenuse, we can write the following code:

val a = 3.0
val b = 4.0
val c = Math.hypot(a, b) // c is 5.0

As you can see, the Math class has a lot of functions for performing numeric calculations.

How did you like the theory?
Report a typo