I’m new to Golang but i found a strange behavior (and correct me if i am wrong)
The function math.RoundToEven() supposed to return a rounded even number but unexpectedly it returns odd number
I am running Golang 1.21.6 on ubuntu machine
I have reported the issue on Golang github issues,but the answer was strange!!
the issue on github
Any idea what is the problem
Do u think this is a bug!!!!!!?
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.RoundToEven(17.1))
//answer supposed to be 18 but it returned 17!!!
}
2
Answers
RoundToEven
returns the nearest integer, rounding ties to even (a tie is essentially exactly half way between two whole numbers, so .5). Which is a confusing statement, so an example to demonstrate:If you want a function that actually rounds to even numbers only, use the following:
Playground