go源碼閱讀筆記(math.1)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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) = NaNfunc 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}

求一個數的絕對值Abs()函數
主要地方在於
if x == 0 {
return 0 // return correctly abs(-0)
}

這裡是考慮當x=-0的情境,所以返回0

bits.go

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

數值表示遵循IEEE 754標準

  • uvnan,NaN, Not a Number
  • uvinf,正無窮大
  • uvneginf,負無窮大

函數func Inf(sign int) float64

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

如果sign是正數,就返回正無窮大,如果是負數就返回負無窮大

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}

該函數判斷一個數是否是NaN
如果不想使用浮點硬體,可以按照注釋所說的這種方式計算

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

uint32(x>>shift)&mask == mask,x的高12位為0x7FF
x != uvinf,x不是正無窮大
x != uvneginf,x不是負無窮大

func IsInf(f float64, sign int) bool

// 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.func IsInf(f float64, sign int) bool {    // 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}

這個不懂什麼意思

遺留問題,MaxFloat64和-MaxFloat64是多少呢?

另一個

Float64frombits(v)像是把一個數轉化成float64,是如何?的呢?

一些數學函數……

func Acosh(x float64) float64
返回x的反雙曲餘弦值
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.