Go source reading notes (Math.1)

Source: Internet
Author: User
Tags mathematical functions
This is a creation in Article, where the information may have evolved or changed.

Go source reading notes (Math.1)

Abs.go

Func Abs (x float64) float64

 PackageMath//Abs returns the absolute value of x.////Special cases is://Abs (±inf) = +inf//Abs (Nan) = NanfuncAbs (xfloat64)float64{//Todo:once golang.org/issue/13095 is fixed, change this to:    //Return Float64frombits (Float64bits (x) &^ (1 <<))    generates better code and can also be inlined:    ifX <0{return-X}ifx = =0{return0 //Return correctly abs ( -0)}returnX

Find the absolute value of a number abs () function
The main place is
if x == 0 {
return 0 // return correctly abs(-0)
}

Here is considered when x=-0 the scene, so return 0

Bits.go

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

Numeric representation follows the IEEE 754 standard

    • Uvnan,nan, not a number
    • Uvinf, Zheng Infinity
    • Uvneginf, Negative infinity

function func Inf (sign int) float64

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

If sign is positive, it returns positive infinity and returns negative if it is negative infinity

Func NaN () float64

754 ``not-a-number''return Float64frombits(uvnan) }

Nan () returns a Nan value

Func IsNaN (f float64) (is bool)

is754 ``not-a-number'' value.func IsNaN(f float64) (is bool) {    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}

This function determines whether a number is Nan
If you do not want to use floating-point hardware, you can calculate it in the same way as the note says

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

UInt32 (x>>shift) &mask = = Mask,x High 12 bits for 0X7FF
X! = Uvinf,x is not a positive infinity
X! = Uvneginf,x is not a negative infinity

Func Isinf (f float64, sign int) bool

//Isinf reports whether F is a infinity, according to sign.//If sign > 0, Isinf reports whether F are positive infinity.//If sign < 0, Isinf reports whether f are 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}

Judge whether a number is infinitely large number

Similarly, if you do not want to use floating-point hardware, you can calculate it in the same way as the comment says

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

anormalnumberandexp// satisfying x == y × 2**exp. It assumes x is finite and non-zero.exp int) {    2.2250738585072014e-308 // 2**-1022    if Abs(x) < SmallestNormal {        return x * (152), -52    }    return0}

这个不懂什么意思

What are the remaining questions, MaxFloat64 and-maxfloat64?

Another

Float64frombits (v) like to convert a number into a float64, how is it achieved?

Some mathematical functions ...

Func Acosh (x float64) float64
Returns the inverse hyperbolic cosine of 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.