Mathematical Library Methods
ICSE Class 9 · Computer Applications · Chapter 7
What this chapter covers
- Library methods and the package java.lang
- Powers and roots: pow(), sqrt() and cbrt()
- Rounding methods: ceil(), floor(), round() and rint()
- Absolute value, larger and smaller: abs(), max() and min()
- Random numbers: random()
- The hypotenuse method: hypot()
- Exact arithmetic: addExact(), subtractExact() and multiplyExact()
- Java expressions using operators and Math methods
- Complete programs using Math methods
Key definitions
java.lang is the package that Java imports automatically into every program, which is why it is called the default package.
Key points
- A library method is a ready-made method stored in Java's class library; the `Math` class in the default package `java.lang` provides the mathematical ones.
- Math methods are static, so they are called with the class name, as in `Math.sqrt(x)`, and no `import` or object is needed.
- `Math.pow(x, y)` gives xy, `Math.sqrt(x)` gives the square root and `Math.cbrt(x)` gives the cube root; all three return a `double`.
- `Math.sqrt()` of a negative number returns `NaN`, while `Math.cbrt()` of a negative number returns a negative root.
- `Math.ceil(x)` moves up to the next whole number and `Math.floor(x)` moves down; both return a `double`, and with negative numbers "up" means towards zero.
- `Math.round(x)` returns the nearest whole number with halves rounded up, as a `long` (or an `int` for a `float` argument), so its output has no `.0`.
- `Math.rint(x)` returns the nearest whole number as a `double`, rounding an exact half to the even neighbour.
- `Math.abs(a)` returns the absolute value in the same type as its argument; `Math.max(a, b)` and `Math.min(a, b)` return the larger and smaller of two values, and are nested for three values.
- `Math.random()` returns a `double` from 0.0 up to, but not including, 1.0; `(int)(Math.random() * (high - low + 1)) + low` gives a whole number from `low` to `high`.
- `Math.hypot(a, b)` returns √(a2 + b2), useful for the hypotenuse and for the distance between two points.
- `Math.addExact()`, `Math.subtractExact()` and `Math.multiplyExact()` work on two `int` values and throw an `ArithmeticException` on overflow instead of giving a silently wrong answer.
- In an expression, each method call is evaluated to a value first, and then operators are applied by their usual precedence and type-promotion rules.
- When writing formulas, put numerators and denominators in brackets, write every `*`, and use a `double` operand in fractions such as `1.0 / 2` to avoid integer division.
Questions answered
Name the package that contains the `Math` class. Why is no `import` statement needed to use it?
The `Math` class is in the `java.lang` package. This package is imported automatically into every Java program, so it is called the default package and needs no `import` statement.
Why are the methods of the `Math` class called with the class name, as in `Math.sqrt(x)`, without creating an object?
They are static methods, which belong to the class itself. A static method is called using the class name followed by a dot, so no object of the `Math` class is needed.
Write the output: (a) `Math.pow(3, 4)` (b) `Math.sqrt(0.64)` (c) `Math.cbrt(-125)` (d) `Math.sqrt(-9)`
(a) `81.0` (b) `0.8` (c) `-5.0` (d) `NaN`, because a negative number has no real square root.
Give the output of `Math.ceil(x)`, `Math.floor(x)`, `Math.round(x)` and `Math.rint(x)` when x = -3.5.
`Math.ceil(-3.5)` is `-3.0`; `Math.floor(-3.5)` is `-4.0`; `Math.round(-3.5)` is `-3`, the floor of -3.0; `Math.rint(-3.5)` is `-4.0`, because -4 is the even neighbour.
State the return type of: (a) `Math.round(5.7)` (b) `Math.round(5.7f)` (c) `Math.abs(-12)` (d) `Math.ceil(5.2)`
(a) `long` (b) `int` (c) `int` (d) `double`.
What is the difference between `Math.round()` and `Math.rint()`? Give one value for which they differ.
Both return the nearest whole number. At an exact halfway value, `round()` goes to the larger number and returns a `long` or `int`, while `rint()` goes to the even number and returns a `double`. For 2.5, `Math.round(2.5)` is `3` but `Math.rint(2.5)` is `2.0`.
Write the output: `System.out.println(Math.max(-7, Math.min(-2, -5)) + Math.abs(-3.0));`
`Math.min(-2, -5)` is -5; `Math.max(-7, -5)` is -5; `Math.abs(-3.0)` is 3.0. The sum is -2.0, so the output is `-2.0`.
Write a Java expression that gives a random whole number from 20 to 30, both included.
`(int)(Math.random() * 11) + 20`, because there are 30 - 20 + 1 = 11 whole numbers in the range.
Past board exam questions
ICSE 2027 sample paper · 1 mark
Which Math method accepts a double argument and returns an Integer?
- ceil
- floor
- random
- round
Show answer
round
ICSE 2027 sample paper · 1 mark
The output of the Java statement Math.addExact(7, 9); is ____________.
- 15
- 16
- 17
- 18
Show answer
16