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

     1  package math32
     2  
     3  // Modf returns integer and fractional floating-point numbers
     4  // that sum to f. Both values have the same sign as f.
     5  //
     6  // Special cases are:
     7  //
     8  //	Modf(±Inf) = ±Inf, NaN
     9  //	Modf(NaN) = NaN, NaN
    10  func Modf(f float32) (int float32, frac float32) {
    11  	return modf(f)
    12  }
    13  
    14  func modf(f float32) (int float32, frac float32) {
    15  	if f < 1 {
    16  		switch {
    17  		case f < 0:
    18  			int, frac = Modf(-f)
    19  			return -int, -frac
    20  		case f == 0:
    21  			return f, f // Return -0, -0 when f == -0
    22  		}
    23  		return 0, f
    24  	}
    25  
    26  	x := Float32bits(f)
    27  	e := uint(x>>shift)&mask - bias
    28  
    29  	// Keep the top 9+e bits, the integer part; clear the rest.
    30  	if e < 32-9 {
    31  		x &^= 1<<(32-9-e) - 1
    32  	}
    33  	int = Float32frombits(x)
    34  	frac = f - int
    35  	return
    36  }