github.com/aavshr/aws-sdk-go@v1.41.3/internal/sdkmath/floor_go1.9.go (about)

     1  //go:build !go1.10
     2  // +build !go1.10
     3  
     4  package sdkmath
     5  
     6  import "math"
     7  
     8  // Copied from the Go standard library's (Go 1.12) math/floor.go for use in
     9  // Go version prior to Go 1.10.
    10  const (
    11  	uvone    = 0x3FF0000000000000
    12  	mask     = 0x7FF
    13  	shift    = 64 - 11 - 1
    14  	bias     = 1023
    15  	signMask = 1 << 63
    16  	fracMask = 1<<shift - 1
    17  )
    18  
    19  // Round returns the nearest integer, rounding half away from zero.
    20  //
    21  // Special cases are:
    22  //	Round(±0) = ±0
    23  //	Round(±Inf) = ±Inf
    24  //	Round(NaN) = NaN
    25  //
    26  // Copied from the Go standard library's (Go 1.12) math/floor.go for use in
    27  // Go version prior to Go 1.10.
    28  func Round(x float64) float64 {
    29  	// Round is a faster implementation of:
    30  	//
    31  	// func Round(x float64) float64 {
    32  	//   t := Trunc(x)
    33  	//   if Abs(x-t) >= 0.5 {
    34  	//     return t + Copysign(1, x)
    35  	//   }
    36  	//   return t
    37  	// }
    38  	bits := math.Float64bits(x)
    39  	e := uint(bits>>shift) & mask
    40  	if e < bias {
    41  		// Round abs(x) < 1 including denormals.
    42  		bits &= signMask // +-0
    43  		if e == bias-1 {
    44  			bits |= uvone // +-1
    45  		}
    46  	} else if e < bias+shift {
    47  		// Round any abs(x) >= 1 containing a fractional component [0,1).
    48  		//
    49  		// Numbers with larger exponents are returned unchanged since they
    50  		// must be either an integer, infinity, or NaN.
    51  		const half = 1 << (shift - 1)
    52  		e -= bias
    53  		bits += half >> e
    54  		bits &^= fracMask >> e
    55  	}
    56  	return math.Float64frombits(bits)
    57  }