1 // Copyright 2010 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.
7 // Logb(x) returns the binary exponent of x.
13 func Logb(x float64) float64 {
14 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
15 // when compiler does it for us
20 case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
22 case x != x: // IsNaN(x):
25 return float64(ilogb(x))
28 // Ilogb(x) returns the binary exponent of x as an integer.
31 // Ilogb(±Inf) = MaxInt32
32 // Ilogb(0) = MinInt32
33 // Ilogb(NaN) = MaxInt32
34 func Ilogb(x float64) int {
35 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
36 // when compiler does it for us
41 case x != x: // IsNaN(x):
43 case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
49 // logb returns the binary exponent of x. It assumes x is finite and
51 func ilogb(x float64) int {
52 x, exp := normalize(x)
53 return int((Float64bits(x)>>shift)&mask) - bias + exp