...
Run Format

Source file src/runtime/complex.go

     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.
     4	
     5	package runtime
     6	
     7	func isposinf(f float64) bool { return f > maxFloat64 }
     8	func isneginf(f float64) bool { return f < -maxFloat64 }
     9	func isnan(f float64) bool    { return f != f }
    10	
    11	func nan() float64 {
    12		var f float64 = 0
    13		return f / f
    14	}
    15	
    16	func posinf() float64 {
    17		var f float64 = maxFloat64
    18		return f * f
    19	}
    20	
    21	func neginf() float64 {
    22		var f float64 = maxFloat64
    23		return -f * f
    24	}
    25	
    26	func complex128div(n complex128, d complex128) complex128 {
    27		// Special cases as in C99.
    28		ninf := isposinf(real(n)) || isneginf(real(n)) ||
    29			isposinf(imag(n)) || isneginf(imag(n))
    30		dinf := isposinf(real(d)) || isneginf(real(d)) ||
    31			isposinf(imag(d)) || isneginf(imag(d))
    32	
    33		nnan := !ninf && (isnan(real(n)) || isnan(imag(n)))
    34		dnan := !dinf && (isnan(real(d)) || isnan(imag(d)))
    35	
    36		switch {
    37		case nnan || dnan:
    38			return complex(nan(), nan())
    39		case ninf && !dinf:
    40			return complex(posinf(), posinf())
    41		case !ninf && dinf:
    42			return complex(0, 0)
    43		case real(d) == 0 && imag(d) == 0:
    44			if real(n) == 0 && imag(n) == 0 {
    45				return complex(nan(), nan())
    46			} else {
    47				return complex(posinf(), posinf())
    48			}
    49		default:
    50			// Standard complex arithmetic, factored to avoid unnecessary overflow.
    51			a := real(d)
    52			if a < 0 {
    53				a = -a
    54			}
    55			b := imag(d)
    56			if b < 0 {
    57				b = -b
    58			}
    59			if a <= b {
    60				ratio := real(d) / imag(d)
    61				denom := real(d)*ratio + imag(d)
    62				return complex((real(n)*ratio+imag(n))/denom,
    63					(imag(n)*ratio-real(n))/denom)
    64			} else {
    65				ratio := imag(d) / real(d)
    66				denom := imag(d)*ratio + real(d)
    67				return complex((imag(n)*ratio+real(n))/denom,
    68					(imag(n)-real(n)*ratio)/denom)
    69			}
    70		}
    71	}
    72	

View as plain text