Why no max/min function for integer in GoLang

  sonic0002        2019-06-08 07:00:55       57,129        12         

You may notice that there is no max/min function provided to compare the maximum/minimum of two or more integers if you are a GoLang developer with some experience . In other languages, these functions are provided as part of the core lib functions. Have you wondered why? 

Indeed GoLang provides max/min function in math package, but they are used for comparing float64 data type. The signature of these two functions are

math.Min(float64, float64) float64
math.Max(float64, float64) float64

The reason why GoLang provides the max/min function for comparing float64 numbers are that floating numbers are very complicated to handle for most of developers, it's not that straightforward to compare two float numbers given the nature of computer architecture. To avoid the trouble brought to the application developed if developers implement their own logic of max/min for float numbers, they are provided by GoLang math package as built in functions.

As for int/int64 data types, the max/min logic is relative easy to implement. A developer with basic skill can implement these two functions easily for integer types. 

func Min(x, y int64) int64 {
 if x < y {
   return x
 }
 return y
}

func Max(x, y int64) int64 {
 if x > y {
   return x
 }
 return y
}

In addition, to keep GoLang as concise and clean as possible, there is no support of generics in GoLang. Since there is max/min implemented for float64 data types, there cannot be function with the same name but with different parameter type implemented in the same math package. So below two cannot exist at the same package.

math.Max(float64, float64) float64
math.Max(int64, int64) int64

This design consideration conforms to the design principle of GoLang, to make GoLang as concise and clean as possible.

GOLANG  INT64  MAX  INT 

       

  RELATED


  12 COMMENTS


Anonymous [Reply]@ 2019-06-09 06:43:02

it's not that straightforward to compare two float numbers given the nature of computer architecture

 

floating point equality is sometimes hard (you need some fuzziness sometimes), but checking greater than or less than is trivial -- compare the exponents, and if they are equal, compare the mantissas

Anonymous [Reply]@ 2019-11-29 12:18:41

well, you forgot to check sign first, so your algorithm failed. But yes, for floats the same function as the suggested for ints above could have been used, as there are already less/more then binary comparative operators for floats in golang.

ElGallo [Reply]@ 2019-08-25 10:54:14

They really should just make a `MaxInt` function given that it is a common primitive, similar to how they have an `Ints` function in the `sort` package.

Anonymous [Reply]@ 2020-01-17 06:33:16

This is why I dislike golang. You guys just don't write golang code, aren't you!

Anonymous [Reply]@ 2020-04-05 21:24:53

It is a problem, I have to write it so often...

Anonymous [Reply]@ 2020-07-19 12:31:20

coming from Python3. implementing these basic stuff over and over again is a pain

Ke Pi [Reply]@ 2020-07-25 09:17:59

ya, that's why generic is needed for such cases.

Anonymous [Reply]@ 2020-08-12 05:46:00

"make GoLang as concise and clean as possible." - what a fanboy statement to make.

This omission is ridiculous.

Anonymous [Reply]@ 2020-08-14 21:10:53

Luckily generic support is on the way

Anonymous [Reply]@ 2022-04-03 17:45:47

Lol but generics are totally overload for such a feature, you could just overload the method for different types. This language is ridiculous in some aspects, I can't understand the decisions behind such design in the era of expressive syntaxes and rich standard library. This is really putting me off from an otherwise good company (Datadog), just because I can't imagine myself dealing with such stupid decisions.

Anonymous [Reply]@ 2022-02-02 23:58:08

I want to use the language. I don't want to write these naive student exercise for  my work

Ke Pi [Reply]@ 2022-02-07 06:50:05

it soon has generic support, then we may have some library solving this kind of problems.



  RANDOM FUN

IBM subsidiary?

Never heard of that OBM now enters into retail industry. Is this an IBM's side project? LOL....