Kotlin Math.abs syntax
Math.abs(x)
In Kotlin, Math.abs returns the magnitude of a number. For example, the absolute value of −6 is 6, and the absolute value of 0 is 0.
Math.abs
fun main() {
println(Math.abs(-3))
}
3
Math.abs(x)
| Input | A signed numeric value. |
|---|---|
| Result | The corresponding numeric type. The minimum Int or Long value stays negative because its positive magnitude does not fit. Floating-point NaN remains NaN. |
A shop records a surplus as a positive count and a shortage as a negative count. The reconciliation report asks how many items differ in each case, regardless of direction.
fun main(args: Array<String>) {
// Positive integer value
val n = Math.abs(9)
println("The absolute value of 9 is " + n)
// Negative integer value
val x = Math.abs(-6)
println("The absolute value of -6 is " + x)
}
The absolute value of 9 is 9 The absolute value of -6 is 6
A scale reports deviations on either side of its reference weight. Find the size of each deviation while keeping the fractional part so small differences remain visible.
fun main(args: Array<String>) {
// Positive floating point value
val n = Math.abs(9.3)
println("The absolute value of 9.3 is " + n)
// Negative floating point value
val x = Math.abs(-6.3)
println("The absolute value of -6.3 is " + x)
}
The absolute value of 9.3 is 9.3 The absolute value of -6.3 is 6.3
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
fun main(args: Array<String>) {
// Positive ∞ infinite value
val n = Math.abs(Double.POSITIVE_INFINITY)
println("The absolute value of Infinity is " + n)
// Negative ∞ infinite value
val x = Math.abs(Double.NEGATIVE_INFINITY)
println("The absolute value of -Infinity is " + x)
}
The absolute value of Infinity is Infinity The absolute value of -Infinity is Infinity
A stocktake compares what the scanner found with what the inventory system expected. The report needs to distinguish a shortage, a surplus and an exact match before deciding whether to investigate.
fun main() {
val scanned = 47
val expected = 52
println(kotlin.math.abs(scanned - expected))
}
5
The scanner found 47 items against 52 on the manifest. The discrepancy is five items. Keep the signed difference separately to distinguish a shortage from a surplus.
A school library expects 48 loan chargers on its shelf after class. A student counts 45. The handover form has a separate field for the size of the discrepancy, whether the shelf is short or over.
fun main() {
val expected = 48
val counted = 45
println((Math.abs(counted - expected)).toString() + " chargers")
}
3 chargers
The signed difference is minus three. Its magnitude gives the three chargers the next shift needs to account for.
A class logs the courtyard temperature at sunrise and again before lunch. The report asks how far apart the readings are, even when one is below freezing.
The two readings sit seven one-degree intervals apart on the same scale.
fun main() {
val sunrise = -3
val midday = 4
println("${kotlin.math.abs(midday - sunrise)} °C")
}
7 °C
Reference: Java Math API used in this example