Charles L. Dorian | c3fa32c | 2010-02-18 23:33:15 -0800 | [diff] [blame] | 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. |
Charles L. Dorian | 3c3e68b | 2010-04-09 14:37:33 -0700 | [diff] [blame^] | 8 | // It returns frac × 2**exp. |
Charles L. Dorian | c3fa32c | 2010-02-18 23:33:15 -0800 | [diff] [blame] | 9 | func 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 | } |