blob: ab8392a01b507a50020971f099295cdcb9b161aa [file] [log] [blame]
Charles L. Dorianc3fa32c2010-02-18 23:33:15 -08001// 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
5package math
6
7// Ldexp is the inverse of Frexp.
Charles L. Dorian3c3e68b2010-04-09 14:37:33 -07008// It returns frac × 2**exp.
Charles L. Dorianc3fa32c2010-02-18 23:33:15 -08009func Ldexp(frac float64, exp int) float64 {
10 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
11 // when compiler does it for us
12 // special cases
13 if frac != frac { // IsNaN(frac)
14 return NaN()
15 }
16 x := Float64bits(frac)
17 exp += int(x>>shift) & mask
18 if exp <= 0 {
19 return 0 // underflow
20 }
21 if exp >= mask { // overflow
22 if frac < 0 {
23 return Inf(-1)
24 }
25 return Inf(1)
26 }
27 x &^= mask << shift
28 x |= uint64(exp) << shift
29 return Float64frombits(x)
30}