This is a creation in Article, where the information may have evolved or changed.
Go source reading notes (math.2)
Floating point and shaping number conversion math/unsafe.go
While reading the math code, I found that float64bits and float64frombits used a lot, so I'll look at what these two functions do.
PackageMathImport "unsafe"//Float32bits returns the IEEE 754 binary representation of F.funcFloat32bits (ffloat32)UInt32{return*(*UInt32) (unsafe. Pointer (&f))}//Float32frombits returns the floating point number corresponding//To the IEEE 754 binary representation B.funcFloat32frombits (bUInt32)float32{return*(*float32) (unsafe. Pointer (&b))}//Float64bits returns the IEEE 754 binary representation of F.funcFloat64bits (ffloat64)UInt64{return*(*UInt64) (unsafe. Pointer (&f))}//Float64frombits returns the floating point number corresponding//The IEEE 754 binary representation B.funcFloat64frombits (bUInt64)float64{return*(*float64) (unsafe. Pointer (&b))}
Explain:
- (*float64) (unsafe. Pointer (&b)) is a standard notation that go provides, allowing a pointer to be converted to another pointer.
- * (*float64) (unsafe. Pointer (&B)) so the operation is to convert the UInt64 type B into float64
- Func float64frombits (b uint64) float64, converts UInt64 number to float64
- Func float64bits (f float64) UInt64, converts float64 number to UInt64
It should be because in the go language, if a bit operation is performed on float, a floating point cell is used, but it is converted to an integer type (the conversion process is fast and does not use a floating-point cell), the integer calculation unit can be used directly, compared to the floating-point cell, the integer calculation will be much faster. So that's what it says in the API.
Therefore, in the Math API, if involved in the operation can be solved, the basic will be float64bits () converted to integer type, and then the calculated results with Float64frombits () converted back to floating point number
Const.go
The code mainly records some mathematical constants, such as E, Pi, and some of the most common types of minimum values, such as MaxInt32, MinInt32
//Package math provides basic constants and mathematical functions. PackageMath//Mathematical constants.Const(E =2.71828182845904523536028747135266249775724709369995957496696763 //http://oeis.org/A001113Pi =3.14159265358979323846264338327950288419716939937510582097494459 //http://oeis.org/A000796Phi =1.61803398874989484820458683436563811772030917980576286213544862 //http://oeis.org/A001622Sqrt2 =1.41421356237309504880168872420969807856967187537694807317667974 //http://oeis.org/A002193Sqrte =1.64872127070012814684865078781416357165377610071014801157507931 //http://oeis.org/A019774SQRTPI =1.77245385090551602729816748334114518279754945612238712821380779 //http://oeis.org/A002161Sqrtphi =1.27201964951406896425242246173749149171560804184009624861664038 //http://oeis.org/A139339LN2 =0.693147180559945309417232121458176568075500134360255254120680009 //http://oeis.org/A002162LOG2E =1/LN2 Ln10 =2.30258509299404568401799145468436420760110148862877297603332790 //http://oeis.org/A002392LOG10E =1/LN10)//floating-point limit values.//Max is the largest finite value representable by the type.//Smallestnonzero is the smallest positive, Non-zero value representable by the type.Const(MaxFloat32 =3.40282346638528859811704183484516925440e+38 //2**127 * (2**24-1)/2**23SmallestNonzeroFloat32 =1.401298464324817070923729583289916131280e-45 //1/2** (127-1 +)MaxFloat64 =1.797693134862315708145274237317043567981e+308 //2**1023 * (2**53-1)/2**52SmallestNonzeroFloat64 =4.940656458412465441765687928682213723651e-324 //1/2** (1023-1 +))//Integer limit values.Const(MaxInt8 =1<<7-1MinInt8 =-1<<7MaxInt16 =1<< the-1MinInt16 =-1<< theMaxInt32 =1<< to-1MinInt32 =-1<< toMaxInt64 =1<< the-1MinInt64 =-1<< theMaxUint8 =1<<8-1MaxUint16 =1<< --1MaxUint32 =1<< +-1MaxUint64 =1<< --1)
Copysign.go
Returns the first parameter x with the sign (positive or negative) of the second parameter Y
// Copysign returns a value with the magnitude// of x and the sign of y.func Copysign(x, y float64) float64 { constsign163 return Float64frombits(Float64bits(x)&^sign | Float64bits(y)&sign)}
Float64bits (x) &^sign is to place the symbol position of x at 0
Float64bits (y) &sign is the sign bit to get Y