github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/src/runtime/complex.go (about) 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 complex128div(n complex128, d complex128) complex128 { 8 // Special cases as in C99. 9 ninf := real(n) == posinf || real(n) == neginf || 10 imag(n) == posinf || imag(n) == neginf 11 dinf := real(d) == posinf || real(d) == neginf || 12 imag(d) == posinf || imag(d) == neginf 13 14 nnan := !ninf && (real(n) != real(n) || imag(n) != imag(n)) 15 dnan := !dinf && (real(d) != real(d) || imag(d) != imag(d)) 16 17 switch { 18 case nnan || dnan: 19 return complex(nan, nan) 20 case ninf && !dinf: 21 return complex(posinf, posinf) 22 case !ninf && dinf: 23 return complex(0, 0) 24 case real(d) == 0 && imag(d) == 0: 25 if real(n) == 0 && imag(n) == 0 { 26 return complex(nan, nan) 27 } else { 28 return complex(posinf, posinf) 29 } 30 default: 31 // Standard complex arithmetic, factored to avoid unnecessary overflow. 32 a := real(d) 33 if a < 0 { 34 a = -a 35 } 36 b := imag(d) 37 if b < 0 { 38 b = -b 39 } 40 if a <= b { 41 ratio := real(d) / imag(d) 42 denom := real(d)*ratio + imag(d) 43 return complex((real(n)*ratio+imag(n))/denom, 44 (imag(n)*ratio-real(n))/denom) 45 } else { 46 ratio := imag(d) / real(d) 47 denom := imag(d)*ratio + real(d) 48 return complex((imag(n)*ratio+real(n))/denom, 49 (imag(n)-real(n)*ratio)/denom) 50 } 51 } 52 }