Unravel โบ Java concepts โบ Numbers and math
Math and number utilities
Math is a toolbox of static methods: max, min, abs, pow, sqrt, round, floor, ceil, trigonometry and logarithms.
Learn these first
At a glance
A calculator on the desk: handy and fast, but it answers in floating point, and some keys behave in ways you only learn by getting burned.
- 1Math.round(2.5) is 3 and Math.round(-2.5) is -2 (it rounds half up toward positive infinity)Math.floor rounds down, Math.ceil rounds up, and both return doubles.
- 2Math.pow(2, 10) is 1024.0, a double; for integer powers of two, shifts (1 << 10) are exactMath.sqrt(-1) is NaN.
- 3Math.max(a, b), min and abs are overloaded for int, long, float and doubleUse Math.clamp(value, min, max) (Java 21) to keep a number in a range.
- 4Ceiling division of integers: (a + b - 1) / b, because Math.ceil(a / b) divides as integers first
- Math.random() returns a double in [0, 1) and is fine for casual use; for tests use a seeded Random, for security use SecureRandom
- Math.abs(Integer.MIN_VALUE) is negative, and Math.round returns long for a double (or int for float), so casting to int can overflow
- Many Math methods are intrinsics: the JIT replaces Math.sqrt or Math.abs with a single CPU instruction
public class Geometry { static double distance(double x1, double y1, double x2, double y2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); } static int pages(int items, int perPage) { return (items + perPage - 1) / perPage; } static long cents(double euros) { return Math.round(euros * 100); }}- line 3 uses Math, the core of Math and number utilities
- line 6 uses Math, the core of Math and number utilities
Read the full explanation (about 1 min)
Math is a toolbox of static methods: max, min, abs, pow, sqrt, round, floor, ceil, trigonometry and logarithms. Most return a double, and some have surprising edge cases.
A mental model
A calculator on the desk: handy and fast, but it answers in floating point, and some keys behave in ways you only learn by getting burned.
How it works
Math.round(2.5) is 3 and Math.round(-2.5) is -2 (it rounds half up toward positive infinity). Math.floor rounds down, Math.ceil rounds up, and both return doubles.
Math.pow(2, 10) is 1024.0, a double; for integer powers of two, shifts (1 << 10) are exact. Math.sqrt(-1) is NaN.
Math.max(a, b), min and abs are overloaded for int, long, float and double. Use Math.clamp(value, min, max) (Java 21) to keep a number in a range.
Ceiling division of integers: (a + b - 1) / b, because Math.ceil(a / b) divides as integers first.
Going deeper
Math.random() returns a double in [0, 1) and is fine for casual use; for tests use a seeded Random, for security use SecureRandom.
Math.abs(Integer.MIN_VALUE) is negative, and Math.round returns long for a double (or int for float), so casting to int can overflow.
Use StrictMath or strictfp semantics (the default since Java 17) when you need identical results on all platforms.
Under the hood
Many Math methods are intrinsics: the JIT replaces Math.sqrt or Math.abs with a single CPU instruction. Math.pow and trigonometric functions use fast, accurate native routines.
Common mistakes
- Dividing two ints and then calling
Math.ceil. - Expecting
Math.round(-0.5)to be-1. - Using
Math.powfor exact integer results.
How Unravel recognises it
When you paste code, Unravel looks for these signs of math and number utilities. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
Math - Method calls:
Math.sqrt()Math.pow()Math.round()Math.floor()Math.ceil()Math.hypot()Math.log()Math.signum()Math.clamp()
Part of a learning path
- Text, numbers and arrays ยท previous: Autoboxing and wrapper types ยท next: Random numbers and UUIDs
Related concepts
Frequently asked questions
What is Math and number utilities?
Math is a toolbox of static methods: max, min, abs, pow, sqrt, round, floor, ceil, trigonometry and logarithms. Most return a double, and some have surprising edge cases.
What are the most common mistakes with Math and number utilities?
Dividing two ints and then calling Math.ceil. Expecting Math.round(-0.5) to be -1. Using Math.pow for exact integer results.
How can I see Math and number utilities in real code?
Paste your own Java into Unravel and it will point out where math and number utilities appears, explain how it works at the detail level you choose, and draw the flow of the code around it.