# go源码阅读笔记（math.1）

qq_15437667 · · 4050 次点击 · · 开始浏览

# go源码阅读笔记（math.1）

## abs.go

func Abs(x float64) float64

``````package math

// Abs returns the absolute value of x.
//
// Special cases are:
//  Abs(±Inf) = +Inf
//  Abs(NaN) = NaN
func Abs(x float64) float64 {
// TODO: once golang.org/issue/13095 is fixed, change this to:
// return Float64frombits(Float64bits(x) &^ (1 << 63))
// But for now, this generates better code and can also be inlined:
if x < 0 {
return -x
}
if x == 0 {
return 0 // return correctly abs(-0)
}
return x
}``````

```if x == 0 { return 0 // return correctly abs(-0) }```

## bits.go

``````const (
uvnan    = 0x7FF8000000000001
uvinf    = 0x7FF0000000000000
uvneginf = 0xFFF0000000000000
shift    = 64 - 11 - 1
bias     = 1023
)``````

• uvnan,NaN, Not a Number
• uvinf,正无穷大
• uvneginf,负无穷大

``````// Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
var v uint64
if sign >= 0 {
v = uvinf
} else {
v = uvneginf
}
return Float64frombits(v)
}``````

func NaN() float64

``````// NaN returns an IEEE 754 ``not-a-number'' value.
func NaN() float64 { return Float64frombits(uvnan) }``````

NaN()返回NaN值

func IsNaN(f float64) (is bool)

``````// IsNaN reports whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float64) (is bool) {
// IEEE 754 says that only NaNs satisfy f != f.
// To avoid the floating-point hardware, could use:
//  x := Float64bits(f);
//  return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
return f != f
}``````

``````x := Float64bits(f);
return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf``````

x != uvinf，x不是正无穷大
x != uvneginf，x不是负无穷大

``````// IsInf reports whether f is an infinity, according to sign.
// If sign > 0, IsInf reports whether f is positive infinity.
// If sign < 0, IsInf reports whether f is negative infinity.
// If sign == 0, IsInf reports whether f is either infinity.
// Test for infinity by comparing against maximum float.
// To avoid the floating-point hardware, could use:
//  x := Float64bits(f);
//  return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
return sign >= 0 && f > MaxFloat64 || sign <= 0 && f < -MaxFloat64
}``````

func normalize(x float64) (y float64, exp int)

``````// normalize returns a normal number y and exponent exp
// satisfying x == y × 2**exp. It assumes x is finite and non-zero.
func normalize(x float64) (y float64, exp int) {
const SmallestNormal = 2.2250738585072014e-308 // 2**-1022
if Abs(x) < SmallestNormal {
return x * (1 << 52), -52
}
return x, 0
}``````

`这个不懂什么意思`

Float64frombits(v)像是把一个数转化成float64，是如何实现的呢？

## 一些数学函数……

func Acosh(x float64) float64

func Asin(x float64) float64
func Acos(x float64) float64
func Asinh(x float64) float64
func Atan(x float64) float64
func Atan2(y, x float64) float64
func Atanh(x float64) float64

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

# go源码阅读笔记（math.1）

## abs.go

func Abs(x float64) float64

``````package math

// Abs returns the absolute value of x.
//
// Special cases are:
//  Abs(±Inf) = +Inf
//  Abs(NaN) = NaN
func Abs(x float64) float64 {
// TODO: once golang.org/issue/13095 is fixed, change this to:
// return Float64frombits(Float64bits(x) &^ (1 << 63))
// But for now, this generates better code and can also be inlined:
if x < 0 {
return -x
}
if x == 0 {
return 0 // return correctly abs(-0)
}
return x
}``````

```if x == 0 { return 0 // return correctly abs(-0) }```

## bits.go

``````const (
uvnan    = 0x7FF8000000000001
uvinf    = 0x7FF0000000000000
uvneginf = 0xFFF0000000000000
shift    = 64 - 11 - 1
bias     = 1023
)``````

• uvnan,NaN, Not a Number
• uvinf,正无穷大
• uvneginf,负无穷大

``````// Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
var v uint64
if sign >= 0 {
v = uvinf
} else {
v = uvneginf
}
return Float64frombits(v)
}``````

func NaN() float64

``````// NaN returns an IEEE 754 ``not-a-number'' value.
func NaN() float64 { return Float64frombits(uvnan) }``````

NaN()返回NaN值

func IsNaN(f float64) (is bool)

``````// IsNaN reports whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float64) (is bool) {
// IEEE 754 says that only NaNs satisfy f != f.
// To avoid the floating-point hardware, could use:
//  x := Float64bits(f);
//  return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
return f != f
}``````

``````x := Float64bits(f);
return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf``````

x != uvinf，x不是正无穷大
x != uvneginf，x不是负无穷大

``````// IsInf reports whether f is an infinity, according to sign.
// If sign > 0, IsInf reports whether f is positive infinity.
// If sign < 0, IsInf reports whether f is negative infinity.
// If sign == 0, IsInf reports whether f is either infinity.
// Test for infinity by comparing against maximum float.
// To avoid the floating-point hardware, could use:
//  x := Float64bits(f);
//  return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
return sign >= 0 && f > MaxFloat64 || sign <= 0 && f < -MaxFloat64
}``````

func normalize(x float64) (y float64, exp int)

``````// normalize returns a normal number y and exponent exp
// satisfying x == y × 2**exp. It assumes x is finite and non-zero.
func normalize(x float64) (y float64, exp int) {
const SmallestNormal = 2.2250738585072014e-308 // 2**-1022
if Abs(x) < SmallestNormal {
return x * (1 << 52), -52
}
return x, 0
}``````

`这个不懂什么意思`

Float64frombits(v)像是把一个数转化成float64，是如何实现的呢？

## 一些数学函数……

func Acosh(x float64) float64

func Asin(x float64) float64
func Acos(x float64) float64
func Asinh(x float64) float64
func Atan(x float64) float64
func Atan2(y, x float64) float64
func Atanh(x float64) float64