For more complex mathematical calculations, we can reference the
Math library which is inherited from Java. It consists of various mathematical functionalities that cover a range of topics from logarithms, trigonometry, statistics, and more. Let’s take a look at some of the most commonly used functions it provides.
Math.pow() returns the first value raised to the power of the second value. It accepts and returns
Math.pow(5.0, 3.0) // 125.0
Math.min() returns the minimum value from two given numbers.
Math.min(5, 3) // 3
Alternatively, Math.max() determines the maximum value.
Math.random() returns a randomly generated number between
Math.random() // 0.3335735290527727
Math.round() accepts a single
Double value and returns the result of rounding that value to the nearest Integer.
Math.round(15.7) // 16
Explore the various other
Math functions in the code editor on the right. As you run the code and witness the output, write a description in comment format below each function describing what you think it does.
Check your answers against the ones in the Hint when you’re ready.