Golang math.Ceil syntax
func Ceil(x float64) float64
In Go, math.Ceil rounds toward positive infinity. The ceiling of 2.3 is 3, the ceiling of −2.3 is −2, and 2 stays 2.
math.Ceil
package main
import "fmt"
import "math"
func main() {
fmt.Println(math.Ceil(2.3))
}
3
func Ceil(x float64) float64
| Input | A float64. Import the math package. |
|---|---|
| Result | A float64. Infinities and NaN pass through unchanged. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
package main
import (
"fmt"
"math"
)
func main() {
var num1 = math.Ceil(1.0)
var num2 = math.Ceil(1.1)
var num3 = math.Ceil(1.2)
var num4 = math.Ceil(1.3)
var num5 = math.Ceil(1.4)
var num6 = math.Ceil(1.5)
var num7 = math.Ceil(1.6)
var num8 = math.Ceil(1.7)
var num9 = math.Ceil(1.8)
var num10 = math.Ceil(1.9)
var num11 = math.Ceil(2.0)
fmt.Println("The ceiling value of 1.0 is", num1);
fmt.Println("The ceiling value of 1.1 is", num2);
fmt.Println("The ceiling value of 1.2 is", num3);
fmt.Println("The ceiling value of 1.3 is", num4);
fmt.Println("The ceiling value of 1.4 is", num5);
fmt.Println("The ceiling value of 1.5 is", num6);
fmt.Println("The ceiling value of 1.6 is", num7);
fmt.Println("The ceiling value of 1.7 is", num8);
fmt.Println("The ceiling value of 1.8 is", num9);
fmt.Println("The ceiling value of 1.9 is", num10);
fmt.Println("The ceiling value of 2.0 is", num11);
}
The ceiling value of 1.0 is 1 The ceiling value of 1.1 is 2 The ceiling value of 1.2 is 2 The ceiling value of 1.3 is 2 The ceiling value of 1.4 is 2 The ceiling value of 1.5 is 2 The ceiling value of 1.6 is 2 The ceiling value of 1.7 is 2 The ceiling value of 1.8 is 2 The ceiling value of 1.9 is 2 The ceiling value of 2.0 is 2
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
package main
import (
"fmt"
"math"
)
func main() {
var num1 = math.Ceil(1.5698)
var num2 = math.Ceil(1.3093)
var num3 = math.Ceil(1.0087)
var num4 = math.Ceil(1.1038)
var num5 = math.Ceil(1.9573)
var num6 = math.Ceil(1.7462)
var num7 = math.Ceil(1.6427)
var num8 = math.Ceil(1.4128)
var num9 = math.Ceil(1.8099)
var num10 = math.Ceil(1.2743)
var num11 = math.Ceil(2.0001)
fmt.Println("The ceiling value of 1.5698 is", num1);
fmt.Println("The ceiling value of 1.3093 is", num2);
fmt.Println("The ceiling value of 1.0087 is", num3);
fmt.Println("The ceiling value of 1.1038 is", num4);
fmt.Println("The ceiling value of 1.9573 is", num5);
fmt.Println("The ceiling value of 1.7462 is", num6);
fmt.Println("The ceiling value of 1.6427 is", num7);
fmt.Println("The ceiling value of 1.4128 is", num8);
fmt.Println("The ceiling value of 1.8099 is", num9);
fmt.Println("The ceiling value of 1.2743 is", num10);
fmt.Println("The ceiling value of 2.0001 is", num11);
}
The ceiling value of 1.5698 is 2 The ceiling value of 1.3093 is 2 The ceiling value of 1.0087 is 2 The ceiling value of 1.1038 is 2 The ceiling value of 1.9573 is 2 The ceiling value of 1.7462 is 2 The ceiling value of 1.6427 is 2 The ceiling value of 1.4128 is 2 The ceiling value of 1.8099 is 2 The ceiling value of 1.2743 is 2 The ceiling value of 2.0001 is 3
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
package main
import (
"fmt"
"math"
)
func main() {
var num1 = math.Ceil(-1.5698)
var num2 = math.Ceil(-1.3093)
var num3 = math.Ceil(-1.0087)
var num4 = math.Ceil(-1.1038)
var num5 = math.Ceil(-1.9573)
var num6 = math.Ceil(-1.7462)
var num7 = math.Ceil(-1.6427)
var num8 = math.Ceil(-1.4128)
var num9 = math.Ceil(-1.8099)
var num10 = math.Ceil(-1.2743)
var num11 = math.Ceil(-2.0001)
fmt.Println("The ceiling value of -1.5698 is", num1);
fmt.Println("The ceiling value of -1.3093 is", num2);
fmt.Println("The ceiling value of -1.0087 is", num3);
fmt.Println("The ceiling value of -1.1038 is", num4);
fmt.Println("The ceiling value of -1.9573 is", num5);
fmt.Println("The ceiling value of -1.7462 is", num6);
fmt.Println("The ceiling value of -1.6427 is", num7);
fmt.Println("The ceiling value of -1.4128 is", num8);
fmt.Println("The ceiling value of -1.8099 is", num9);
fmt.Println("The ceiling value of -1.2743 is", num10);
fmt.Println("The ceiling value of -2.0001 is", num11);
}
The ceiling value of -1.5698 is -1 The ceiling value of -1.3093 is -1 The ceiling value of -1.0087 is -1 The ceiling value of -1.1038 is -1 The ceiling value of -1.9573 is -1 The ceiling value of -1.7462 is -1 The ceiling value of -1.6427 is -1 The ceiling value of -1.4128 is -1 The ceiling value of -1.8099 is -1 The ceiling value of -1.2743 is -1 The ceiling value of -2.0001 is -2
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)
fmt.Println("The ceiling value of +Inf is", math.Ceil(n));
// Negative ∞ infinite value
m := math.Inf(-1)
fmt.Println("The ceiling value of -Inf is", math.Ceil(m));
}
The ceiling value of +Inf is +Inf The ceiling value of -Inf is -Inf
A teacher has 73 passengers on the museum trip, including the adults. Each minibus seats 24. She needs enough vehicles for the last passenger as well as the first 72.
package main
import (
"fmt"
"math"
)
func main() {
passengers, seats := 73, 24
buses := math.Ceil(float64(passengers) / float64(seats))
fmt.Printf("%.0f minibuses", buses)
}
4 minibuses
Three minibuses seat 72 people. Dividing before rounding up reserves the fourth vehicle for the remaining passenger.
Reference: Go numeric operations