goソース読書ノート(math.3)

10029 ワード

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) = NaN
func Dim(x, y float64) float64

func dim(x, y float64) float64 {
    return max(x-y, 0)
}

func dim(x,y float 64)float 64は、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) = -0
func Max(x, y float64) float64

func 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) = -0
func Min(x, y float64) float64

func 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は主に1つの数の上界あるいは下界を求めます
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) = NaN
func Floor(x float64) float64

func 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 float 64)float 64は、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, NaN
func 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つの数を入力し、その数の整数部分と小数部分を返します.例えば、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) = NaN
func Ceil(x float64) float64

func ceil(x float64) float64 {
    return -Floor(-x)
}

func ceil(x float 64)float 64、書くのはとてもすばらしくて、参考にすることができて、-xの下界は実はxの上界の反対数です
感じmathこの章は基本的に数学の技巧です
// Trunc returns the integer value of x.
//
// Special cases are:
// Trunc(±0) = ±0
// Trunc(±Inf) = ±Inf
// Trunc(NaN) = NaN
func Trunc(x float64) float64

func trunc(x float64) float64 {
    if x == 0 || IsNaN(x) || IsInf(x, 0) {
        return x
    }
    d, _ := Modf(x)
    return d
}

func trunc(x float 64)float 64は、fの整数部分を返す