Why go does not have function to calculate absolute value for integer datatype?

Piy*_*gal -1 casting go absolute-value

In go, why there is no function which directly calculates absolute value for integer datatypes. Currently all integer values has to be type casted to float64 and then it has to be used as a parameter in math.Abs() function. And it returns float64 datatype only, which again has to be type casted into integer datatype.

Like this code is giving error as Go is statically typed language, so does not allows different datatype.

./prog.go:12:39: cannot use x (type int64) as type float64 in argument to math.Abs


import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Hello, playground")
    var x int64 = -10

    fmt.Println("Abolute value ",math.Abs(x))   
}
Run Code Online (Sandbox Code Playgroud)

小智 10

绝对值只是绝对差[1]的一种特殊情况,其中第二个值为零。这是整数的绝对值函数,以及整数的绝对差函数。Bonus 是无符号整数的绝对差函数:

package math

func absInt(x int) int {
   return absDiffInt(x, 0)
}

func absDiffInt(x, y int) int {
   if x < y {
      return y - x
   }
   return x - y
}

func absDiffUint(x, y uint) uint {
   if x < y {
      return y - x
   }
   return x - y
}
Run Code Online (Sandbox Code Playgroud)
  1. https://wikipedia.org/wiki/Absolute_difference


小智 5

From Go's FAQ,

The standard library's purpose is to support the runtime, connect to the operating system, and provide key functionality that many Go programs require, such as formatted I/O and networking. It also contains elements important for web programming, including cryptography and support for standards like HTTP, JSON, and XML.

There is no clear criterion that defines what is included because for a long time, this was the only Go library. There are criteria that define what gets added today, however.

New additions to the standard library are rare and the bar for inclusion is high. Code included in the standard library bears a large ongoing maintenance cost (often borne by those other than the original author), is subject to the Go 1 compatibility promise (blocking fixes to any flaws in the API), and is subject to the Go release schedule, preventing bug fixes from being available to users quickly.

Most new code should live outside of the standard library and be accessible via the go tool's go get command. Such code can have its own maintainers, release cycle, and compatibility guarantees. Users can find packages and read their documentation at godoc.org.

In response to how easy it is to create integer versions of the math package's float functions, Go team member Russ Cox once quipped,

Ceil,Floor和Trunc更加轻松!

合理的解释是,由于编写此函数很简单(如果x <0,x = -x),则不符合包含的标准。与浮动版本比较:

func Abs(x float64) float64 {
    return Float64frombits(Float64bits(x) &^ (1 << 63))
}
Run Code Online (Sandbox Code Playgroud)

将其包含在标准库中是一个很有用但又不明显的理由。

  • 无论该功能是否(对于某人)实现起来“微不足道”,可能都不是一个好的衡量标准。更好的衡量标准是看看人们犯错的可能性。考虑到我看到的这个问题的有问题的答案以及人们撰写博客文章试图回答这个问题的事实,我想说它值得包含在标准库中,因为人们很可能会弄错。 (11认同)
  • 同意@borud - 构建编程语言+随附标准库的目的不仅仅是“提供复杂的功能” - 为什么要让标准库的每个用户重写相同的函数? (5认同)