Skip to content

Commit 21ff75b

Browse files
committed
complex divide: match C99 implementation
R=iant, ken2, r, r2, ken3 CC=golang-dev https://golang.org/cl/1686044
1 parent 99b23a1 commit 21ff75b

File tree

7 files changed

+2569
-41
lines changed

7 files changed

+2569
-41
lines changed

src/pkg/runtime/complex.c

+47-23
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,57 @@
44

55
#include "runtime.h"
66

7-
// complex128div(num, den complex128) (quo complex128)
7+
typedef struct Complex128 Complex128;
8+
89
void
9-
·complex128div(float64 numreal, float64 numimag,
10-
float64 denreal, float64 denimag,
11-
float64 quoreal, float64 quoimag)
10+
·complex128div(Complex128 n, Complex128 d, Complex128 q)
1211
{
12+
int32 ninf, dinf, nnan, dnan;
1313
float64 a, b, ratio, denom;
1414

15-
a = denreal;
16-
if(a < 0)
17-
a = -a;
18-
b = denimag;
19-
if(b < 0)
20-
b = -b;
21-
if(a <= b) {
22-
if(b == 0)
23-
panicstring("complex divide by zero");
24-
ratio = denreal/denimag;
25-
denom = denreal*ratio + denimag;
26-
quoreal = (numreal*ratio + numimag) / denom;
27-
quoimag = (numimag*ratio - numreal) / denom;
15+
// Special cases as in C99.
16+
ninf = isInf(n.real, 0) || isInf(n.imag, 0);
17+
dinf = isInf(d.real, 0) || isInf(d.imag, 0);
18+
19+
nnan = !ninf && (isNaN(n.real) || isNaN(n.imag));
20+
dnan = !dinf && (isNaN(d.real) || isNaN(d.imag));
21+
22+
if(nnan || dnan) {
23+
q.real = NaN();
24+
q.imag = NaN();
25+
} else if(ninf && !dinf && !dnan) {
26+
q.real = Inf(0);
27+
q.imag = Inf(0);
28+
} else if(!ninf && !nnan && dinf) {
29+
q.real = 0;
30+
q.imag = 0;
31+
} else if(d.real == 0 && d.imag == 0) {
32+
if(n.real == 0 && n.imag == 0) {
33+
q.real = NaN();
34+
q.imag = NaN();
35+
} else {
36+
q.real = Inf(0);
37+
q.imag = Inf(0);
38+
}
2839
} else {
29-
ratio = denimag/denreal;
30-
denom = denimag*ratio + denreal;
31-
quoreal = (numimag*ratio + numreal) / denom;
32-
quoimag = (numimag - numreal*ratio) / denom;
40+
// Standard complex arithmetic, factored to avoid unnecessary overflow.
41+
a = d.real;
42+
if(a < 0)
43+
a = -a;
44+
b = d.imag;
45+
if(b < 0)
46+
b = -b;
47+
if(a <= b) {
48+
ratio = d.real/d.imag;
49+
denom = d.real*ratio + d.imag;
50+
q.real = (n.real*ratio + n.imag) / denom;
51+
q.imag = (n.imag*ratio - n.real) / denom;
52+
} else {
53+
ratio = d.imag/d.real;
54+
denom = d.imag*ratio + d.real;
55+
q.real = (n.imag*ratio + n.real) / denom;
56+
q.imag = (n.imag - n.real*ratio) / denom;
57+
}
3358
}
34-
FLUSH(&quoreal);
35-
FLUSH(&quoimag);
59+
FLUSH(&q);
3660
}

test/cmplxdivide.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// gcc '-std=c99' cmplxdivide.c && a.out >cmplxdivide1.go
6+
7+
#include <complex.h>
8+
#include <math.h>
9+
#include <stdio.h>
10+
#include <string.h>
11+
12+
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
13+
14+
double f[] = {
15+
0,
16+
1,
17+
-1,
18+
2,
19+
NAN,
20+
INFINITY,
21+
-INFINITY,
22+
};
23+
24+
char*
25+
fmt(double g)
26+
{
27+
static char buf[10][30];
28+
static int n;
29+
char *p;
30+
31+
p = buf[n++];
32+
if(n == 10)
33+
n = 0;
34+
sprintf(p, "%g", g);
35+
if(strcmp(p, "-0") == 0)
36+
strcpy(p, "negzero");
37+
return p;
38+
}
39+
40+
int
41+
main(void)
42+
{
43+
int i, j, k, l;
44+
double complex n, d, q;
45+
46+
printf("// # generated by cmplxdivide.c\n");
47+
printf("\n");
48+
printf("package main\n");
49+
printf("var tests = []Test{\n");
50+
for(i=0; i<nelem(f); i++)
51+
for(j=0; j<nelem(f); j++)
52+
for(k=0; k<nelem(f); k++)
53+
for(l=0; l<nelem(f); l++) {
54+
n = f[i] + f[j]*I;
55+
d = f[k] + f[l]*I;
56+
q = n/d;
57+
printf("\tTest{cmplx(%s, %s), cmplx(%s, %s), cmplx(%s, %s)},\n", fmt(creal(n)), fmt(cimag(n)), fmt(creal(d)), fmt(cimag(d)), fmt(creal(q)), fmt(cimag(q)));
58+
}
59+
printf("}\n");
60+
return 0;
61+
}

test/cmplxdivide.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// $G $D/$F.go $D/cmplxdivide1.go && $L $D/$F.$A && ./$A.out
2+
3+
// Copyright 2010 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Driver for complex division table defined in cmplxdivide1.go
8+
9+
package main
10+
11+
import (
12+
"cmath"
13+
"fmt"
14+
"math"
15+
)
16+
17+
type Test struct{
18+
f, g complex128
19+
out complex128
20+
}
21+
22+
var nan = math.NaN()
23+
var inf = math.Inf(1)
24+
var negzero = math.Copysign(0, -1)
25+
26+
func calike(a, b complex128) bool {
27+
switch {
28+
case cmath.IsInf(a) && cmath.IsInf(b):
29+
return true
30+
case cmath.IsNaN(a) && cmath.IsNaN(b):
31+
return true
32+
}
33+
return a == b
34+
}
35+
36+
func main() {
37+
for _, t := range tests {
38+
x := t.f/t.g
39+
if !calike(x, t.out) {
40+
fmt.Printf("%v/%v: expected %v error; got %v\n", t.f, t.g, t.out, x)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)