Source file src/math/frexp.go

     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  	if haveArchFrexp {
    19  		return archFrexp(f)
    20  	}
    21  	return frexp(f)
    22  }
    23  
    24  func frexp(f float64) (frac float64, exp int) {
    25  	// special cases
    26  	switch {
    27  	case f == 0:
    28  		return f, 0 // correctly return -0
    29  	case IsInf(f, 0) || IsNaN(f):
    30  		return f, 0
    31  	}
    32  	f, exp = normalize(f)
    33  	x := Float64bits(f)
    34  	exp += int((x>>shift)&mask) - bias + 1
    35  	x &^= mask << shift
    36  	x |= (-1 + bias) << shift
    37  	frac = Float64frombits(x)
    38  	return
    39  }
    40  

View as plain text