Golang math.Abs syntax
func Abs(x float64) float64
In Go, 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
package main
import "fmt"
import "math"
func main() {
fmt.Println(math.Abs(-3))
}
3
func Abs(x float64) float64
| Input | A float64. Import the math package. |
|---|---|
| Result | A float64. Either infinity yields positive infinity and NaN yields 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.
package main
import (
"fmt"
"math"
)
func main() {
// Positive integer value
n := math.Abs(9)
fmt.Println("The absolute value of 9 is", n)
// Negative integer value
x := math.Abs(-6)
fmt.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.
package main
import (
"fmt"
"math"
)
func main() {
// Positive floating point value
n := math.Abs(9.3)
fmt.Println("The absolute value of 9.3 is", n)
// Negative floating point value
x := math.Abs(-6.3)
fmt.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.
package main
import (
"fmt"
"math"
)
func main() {
// Positive ∞ infinite value
n := math.Inf(+1)
n = math.Abs(n)
fmt.Println("The absolute value of +Inf is", n)
// Negative ∞ infinite value
x := math.Inf(-1)
x = math.Abs(x)
fmt.Println("The absolute value of -Inf is", x)
}
The absolute value of +Inf is +Inf The absolute value of -Inf is +Inf
A sensor returns a missing or undefined numerical reading. Check how this operation handles NaN before letting that result enter the daily summary.
package main
import (
"fmt"
"math"
)
func main() {
n := math.NaN()
n = math.Abs(n)
fmt.Println("The absolute value of NaN is", n)
}
The absolute value of NaN is NaN
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.
package main
import (
"fmt"
"math"
)
func main() {
scanned, expected := 47.0, 52.0
fmt.Println(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.
package main
import (
"fmt"
"math"
)
func main() {
expected, counted := 48.0, 45.0
fmt.Printf("%.0f chargers", math.Abs(counted - expected))
}
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.
package main
import (
"fmt"
"math"
)
func main() {
sunrise, midday := -3.0, 4.0
fmt.Printf("%.0f °C\n", math.Abs(midday - sunrise))
}
7 °C
Reference: Go numeric operations