github.com/primecitizens/pcz/std@v0.2.1/math/frexp.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  // Frexp breaks f into a normalized fraction
     8  // and an integral power of two.
     9  // It returns frac and exp satisfying f == frac × 2**exp,
    10  // with the absolute value of frac in the interval [½, 1).
    11  //
    12  // Special cases are:
    13  //
    14  //	Frexp(±0) = ±0, 0
    15  //	Frexp(±Inf) = ±Inf, 0
    16  //	Frexp(NaN) = NaN, 0
    17  func Frexp(f float64) (frac float64, exp int) {
    18  	return frexp(f)
    19  }
    20  
    21  func frexp(f float64) (frac float64, exp int) {
    22  	// special cases
    23  	switch {
    24  	case f == 0:
    25  		return f, 0 // correctly return -0
    26  	case IsInf(f, 0) || IsNaN(f):
    27  		return f, 0
    28  	}
    29  	f, exp = normalize(f)
    30  	x := Float64bits(f)
    31  	exp += int((x>>shift)&mask) - bias + 1
    32  	x &^= mask << shift
    33  	x |= (-1 + bias) << shift
    34  	frac = Float64frombits(x)
    35  	return
    36  }