src/pkg/math/ldexp.go - The Go Programming Language

Golang

Source file src/pkg/math/ldexp.go

     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	//	Ldexp(±0, exp) = ±0
    12	//	Ldexp(±Inf, exp) = ±Inf
    13	//	Ldexp(NaN, exp) = NaN
    14	func Ldexp(frac float64, exp int) float64
    15	
    16	func ldexp(frac float64, exp int) float64 {
    17		// special cases
    18		switch {
    19		case frac == 0:
    20			return frac // correctly return -0
    21		case IsInf(frac, 0) || IsNaN(frac):
    22			return frac
    23		}
    24		frac, e := normalize(frac)
    25		exp += e
    26		x := Float64bits(frac)
    27		exp += int(x>>shift)&mask - bias
    28		if exp < -1074 {
    29			return Copysign(0, frac) // underflow
    30		}
    31		if exp > 1023 { // overflow
    32			if frac < 0 {
    33				return Inf(-1)
    34			}
    35			return Inf(1)
    36		}
    37		var m float64 = 1
    38		if exp < -1022 { // denormal
    39			exp += 52
    40			m = 1.0 / (1 << 52) // 2**-52
    41		}
    42		x &^= mask << shift
    43		x |= uint64(exp+bias) << shift
    44		return m * Float64frombits(x)
    45	}