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

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

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

dim.go

package math// Dim returns the maximum of x-y or 0.//// Special cases are://  Dim(+Inf, +Inf) = NaN//  Dim(-Inf, -Inf) = NaN//  Dim(x, NaN) = Dim(NaN, x) = NaNfunc Dim(x, y float64) float64func dim(x, y float64) float64 {    return max(x-y, 0)}

func dim(x, y float64) float64 ,返回x-y與0的較大者

這裡我們可以看出,調用的函數max()進行了各種異常判斷,所以在dim函數這裡就不需要各種異常判斷了,這裡可以作為一點經驗

// Max returns the larger of x or y.//// Special cases are://  Max(x, +Inf) = Max(+Inf, x) = +Inf//  Max(x, NaN) = Max(NaN, x) = NaN//  Max(+0, ±0) = Max(±0, +0) = +0//  Max(-0, -0) = -0func Max(x, y float64) float64func max(x, y float64) float64 {    // special cases    switch {    case IsInf(x, 1) || IsInf(y, 1):        return Inf(1)    case IsNaN(x) || IsNaN(y):        return NaN()    case x == 0 && x == y:        if Signbit(x) {            return y        }        return x    }    if x > y {        return x    }    return y}

這裡看不太懂為什麼要判斷case x == 0 && x == y:,可能我需要看看Signbit()這個函數是做什麼用的吧。

// Min returns the smaller of x or y.//// Special cases are://  Min(x, -Inf) = Min(-Inf, x) = -Inf//  Min(x, NaN) = Min(NaN, x) = NaN//  Min(-0, ±0) = Min(±0, -0) = -0func Min(x, y float64) float64func min(x, y float64) float64 {    // special cases    switch {    case IsInf(x, -1) || IsInf(y, -1):        return Inf(-1)    case IsNaN(x) || IsNaN(y):        return NaN()    case x == 0 && x == y:        if Signbit(x) {            return x        }        return y    }    if x < y {        return x    }    return y}

這個同max

遺留問題

什麼要判斷case x == 0 && x == y:,我需要看看Signbit()這個函數是做什麼用?

floor.go

floor.go主要求的是一個數的上界或者下界

package math// Floor returns the greatest integer value less than or equal to x.//// Special cases are://  Floor(±0) = ±0//  Floor(±Inf) = ±Inf//  Floor(NaN) = NaNfunc Floor(x float64) float64func floor(x float64) float64 {    if x == 0 || IsNaN(x) || IsInf(x, 0) {        return x    }    if x < 0 {        d, fract := Modf(-x)        if fract != 0.0 {            d = d + 1        }        return -d    }    d, _ := Modf(x)    return d}

func floor(x float64) float64,返回小於等於x的最大整數

Modf()這個函數做什麼用?先查看一下該函數

// Modf returns integer and fractional floating-point numbers// that sum to f.  Both values have the same sign as f.//// Special cases are://  Modf(±Inf) = ±Inf, NaN//  Modf(NaN) = NaN, NaNfunc Modf(f float64) (int float64, frac float64)func modf(f float64) (int float64, frac float64) {    if f < 1 {        switch {        case f < 0:            int, frac = Modf(-f)            return -int, -frac        case f == 0:            return f, f // Return -0, -0 when f == -0        }        return 0, f    }    x := Float64bits(f)    e := uint(x>>shift)&mask - bias    // Keep the top 12+e bits, the integer part; clear the rest.    if e < 64-12 {        x &^= 1<<(64-12-e) - 1    }    int = Float64frombits(x)    frac = f - int    return}

但是,,,我看了半天還是沒看明白,我仔細研究一下,懂了之後再解釋

就算這樣,通過上面代碼可以看出來,Modf()是傳入一個數,然後返回這個數的整數部分和小數部分,譬如1.5返回1.0和0.5,-1.5返回-1.0和-0.5

// Ceil returns the least integer value greater than or equal to x.//// Special cases are://  Ceil(±0) = ±0//  Ceil(±Inf) = ±Inf//  Ceil(NaN) = NaNfunc Ceil(x float64) float64func ceil(x float64) float64 {    return -Floor(-x)}

func ceil(x float64) float64,寫的很妙,可以參考,大概就是-x的下界其實就是x上界的相反數

感覺math這章基本是數學技巧

// Trunc returns the integer value of x.//// Special cases are://  Trunc(±0) = ±0//  Trunc(±Inf) = ±Inf//  Trunc(NaN) = NaNfunc Trunc(x float64) float64func trunc(x float64) float64 {    if x == 0 || IsNaN(x) || IsInf(x, 0) {        return x    }    d, _ := Modf(x)    return d}

func trunc(x float64) float64,返回的就是f的整數部分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.