gitee.com/quant1x/num@v0.3.2/math32/floor.go (about)

     1  package math32
     2  
     3  // Floor returns the greatest integer value less than or equal to x.
     4  //
     5  // Special cases are:
     6  //
     7  //	Floor(±0) = ±0
     8  //	Floor(±Inf) = ±Inf
     9  //	Floor(NaN) = NaN
    10  func Floor(x float32) float32 {
    11  	return floor(x)
    12  }
    13  
    14  func floor(x float32) float32 {
    15  	if x == 0 || IsNaN(x) || IsInf(x, 0) {
    16  		return x
    17  	}
    18  	if x < 0 {
    19  		d, fract := Modf(-x)
    20  		if fract != 0.0 {
    21  			d = d + 1
    22  		}
    23  		return -d
    24  	}
    25  	d, _ := Modf(x)
    26  	return d
    27  }
    28  
    29  // Ceil returns the least integer value greater than or equal to x.
    30  //
    31  // Special cases are:
    32  //
    33  //	Ceil(±0) = ±0
    34  //	Ceil(±Inf) = ±Inf
    35  //	Ceil(NaN) = NaN
    36  func Ceil(x float32) float32 {
    37  	return ceil(x)
    38  }
    39  
    40  func ceil(x float32) float32 {
    41  	return -Floor(-x)
    42  }
    43  
    44  // Trunc returns the integer value of x.
    45  //
    46  // Special cases are:
    47  //
    48  //	Trunc(±0) = ±0
    49  //	Trunc(±Inf) = ±Inf
    50  //	Trunc(NaN) = NaN
    51  func Trunc(x float32) float32 {
    52  	return trunc(x)
    53  }
    54  
    55  func trunc(x float32) float32 {
    56  	if x == 0 || IsNaN(x) || IsInf(x, 0) {
    57  		return x
    58  	}
    59  	d, _ := Modf(x)
    60  	return d
    61  }