4 ms·
Note that min/max were already available in the math package, but they’re a bit inconvenient to use (lots of casting to do)
by thiht 3y ago
Note that min/max were already available in the math package, but they’re a bit inconvenient to use (lots of casting to do)
- dchest 3y agoWhile it's true that it had math.Min/Max, it's not just the inconvenience of castings that this solves. You can't cast uint64 to float64 and expect the function to return the correct result, e.g.: x1 := uint64(9007199254740993) x2 := uint64(9007199254740992) fmt.Println(uint64(math.Max(float64(x1), float64(x2)))) will print 9007199254740992. min/max and math.Min/Max are not equivalent. The correct way to write min/max before was to use comparison operators. math.Min/Max was there because it requires tricky special cases for handling -Inf, Inf, NaN, and negative zero.