2010-12-03 05:34:57 +01:00
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package math
|
|
|
|
|
|
|
|
|
|
// Frexp breaks f into a normalized fraction
|
|
|
|
|
// and an integral power of two.
|
|
|
|
|
// It returns frac and exp satisfying f == frac × 2**exp,
|
|
|
|
|
// with the absolute value of frac in the interval [½, 1).
|
2011-01-21 19:19:03 +01:00
|
|
|
|
//
|
|
|
|
|
// Special cases are:
|
|
|
|
|
// Frexp(±0) = ±0, 0
|
|
|
|
|
// Frexp(±Inf) = ±Inf, 0
|
|
|
|
|
// Frexp(NaN) = NaN, 0
|
2010-12-03 05:34:57 +01:00
|
|
|
|
func Frexp(f float64) (frac float64, exp int) {
|
2012-01-12 02:31:45 +01:00
|
|
|
|
return frexp(f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func frexp(f float64) (frac float64, exp int) {
|
2010-12-03 05:34:57 +01:00
|
|
|
|
// special cases
|
|
|
|
|
switch {
|
|
|
|
|
case f == 0:
|
|
|
|
|
return f, 0 // correctly return -0
|
2012-02-09 09:19:58 +01:00
|
|
|
|
case IsInf(f, 0) || IsNaN(f):
|
2010-12-03 05:34:57 +01:00
|
|
|
|
return f, 0
|
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
f, exp = normalize(f)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
x := Float64bits(f)
|
2011-01-21 19:19:03 +01:00
|
|
|
|
exp += int((x>>shift)&mask) - bias + 1
|
2010-12-03 05:34:57 +01:00
|
|
|
|
x &^= mask << shift
|
2011-01-21 19:19:03 +01:00
|
|
|
|
x |= (-1 + bias) << shift
|
2010-12-03 05:34:57 +01:00
|
|
|
|
frac = Float64frombits(x)
|
|
|
|
|
return
|
|
|
|
|
}
|