Go source reading notes (math.3)

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

Go source reading notes (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) = NaNfuncfloat64float64funcfloat64float64 {    return max(x-y, 0)}

Func Dim (x, y float64) float64, returns the larger of X-y and 0

Here we can see that the called Function Max () makes a variety of exception judgments, so the dim function here does not require a variety of anomaly judgments, here can be a bit of experience

//Max returns the larger of x or Y.////Special cases is://MAX (x, +inf) = Max (+inf, x) = +inf//MAX (x, Nan) = Max (Nan, x) = Nan//MAX (+0,±0) = Max (±0, +0) = +0//Max ( -0, -0) = 0funcMax (x, yfloat64)float64funcMax (x, yfloat64)float64{//Special cases    Switch{ CaseIsinf (x,1) || Isinf (Y,1):returnInf(1) CaseIsNaN (x) | | IsNaN (y):returnNaN () Casex = =0&& x = = y:ifSignbit (x) {returnY}returnX}ifX > Y {returnX}returnY

I don't know why I want to judge here case x == 0 && x == y: , maybe I need to see signbit () What this function is for.

//Min returns the smaller of x or Y.////Special cases is://min (x,-inf) = min (-inf, x) =-inf/min (x, Nan) = min (nan, x) = Nan//min ( -0,±0) = min (±0, -0) = 0funcMin (x, yfloat64)float64funcMin (x, yfloat64)float64{//Special cases    Switch{ CaseIsinf (x,-1) || Isinf (Y,-1):returnInf( -1) CaseIsNaN (x) | | IsNaN (y):returnNaN () Casex = =0&& x = = y:ifSignbit (x) {returnX}returnY}ifX < y {returnX}returnY

This same with Max

Legacy issues

What to judge case x == 0 && x == y: , I need to see signbit () What does this function do?

Floor.go

Floor.go The main requirement is the upper or lower bounds of a number

 PackageMath//Floor Returns the greatest integer value less than or equal to X.////Special cases is://Floor (±0) =±0//Floor (±inf) =±inf//Floor (Nan) = NanfuncFloor (xfloat64)float64funcFloor (xfloat64)float64{ifx = =0|| IsNaN (x) | | Isinf (x,0) {returnX}ifX <0{d, Fract: = MODF (-X)ifFract! =0.0{d = d +1}return-D} D., _: = MODF (x)returnD

Func floor (x float64) float64, returns the largest integer less than or equal to X

MODF () What does this function do? Look at the function first

//MODF returns integer and fractional floating-point numbers//That sum to F. Both values has the same sign as F.////Special cases is://MODF (±inf) =±inf, NaN//Modf (Nan) = Nan, NanfuncMODF (ffloat64) (int float64, Fracfloat64)funcMODF (ffloat64) (int float64, Fracfloat64) {ifF <1{Switch{ CaseF <0:int, Frac = MODF (-f)return-int,-frac Casef = =0:returnF, F//Return-0,-0 when f = =-0}return0, f} x: = Float64bits (f) E: =UINT(X>>shift) &mask-bias//Keep the top 12+e bits, the integer part; clear the rest.    ifE < --12{x &^=1<<( +-12-e)-1}int= Float64frombits (x) frac = f-int    return}

But,,, I watched for a long while still did not see understand, I study carefully, understand and then explain

Even so, as can be seen from the above code, MODF () is passed in a number, and then returns the integer part and fractional part of the number, for example, 1.5 returns 1.0 and 0.5,-1.5 returns-1.0 and 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) = NaNfuncfloat64float64funcfloat64float64 {    return -Floor(-x)}

Func ceil (x float64) float64, write very good, can refer to, is probably the lower bound of X is actually the inverse number of the upper bounds

Feel math This chapter is basically math skills

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

Func trunc (x float64) float64, which returns the integral part of F

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.