github.com/primecitizens/pcz/std@v0.2.1/math/ldexp.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 // Ldexp is the inverse of Frexp. 8 // It returns frac × 2**exp. 9 // 10 // Special cases are: 11 // 12 // Ldexp(±0, exp) = ±0 13 // Ldexp(±Inf, exp) = ±Inf 14 // Ldexp(NaN, exp) = NaN 15 func Ldexp(frac float64, exp int) float64 { 16 return ldexp(frac, exp) 17 } 18 19 func ldexp(frac float64, exp int) float64 { 20 // special cases 21 switch { 22 case frac == 0: 23 return frac // correctly return -0 24 case IsInf(frac, 0) || IsNaN(frac): 25 return frac 26 } 27 frac, e := normalize(frac) 28 exp += e 29 x := Float64bits(frac) 30 exp += int(x>>shift)&mask - bias 31 if exp < -1075 { 32 return Copysign(0, frac) // underflow 33 } 34 if exp > 1023 { // overflow 35 if frac < 0 { 36 return Inf(-1) 37 } 38 return Inf(1) 39 } 40 var m float64 = 1 41 if exp < -1022 { // denormal 42 exp += 53 43 m = 1.0 / (1 << 53) // 2**-53 44 } 45 x &^= mask << shift 46 x |= uint64(exp+bias) << shift 47 return m * Float64frombits(x) 48 }