-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complex divide: match C99 implementation
R=iant, ken2, r, r2, ken3 CC=golang-dev https://golang.org/cl/1686044
- Loading branch information
Showing
7 changed files
with
2,569 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2010 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// gcc '-std=c99' cmplxdivide.c && a.out >cmplxdivide1.go | ||
|
||
#include <complex.h> | ||
#include <math.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define nelem(x) (sizeof(x)/sizeof((x)[0])) | ||
|
||
double f[] = { | ||
0, | ||
1, | ||
-1, | ||
2, | ||
NAN, | ||
INFINITY, | ||
-INFINITY, | ||
}; | ||
|
||
char* | ||
fmt(double g) | ||
{ | ||
static char buf[10][30]; | ||
static int n; | ||
char *p; | ||
|
||
p = buf[n++]; | ||
if(n == 10) | ||
n = 0; | ||
sprintf(p, "%g", g); | ||
if(strcmp(p, "-0") == 0) | ||
strcpy(p, "negzero"); | ||
return p; | ||
} | ||
|
||
int | ||
main(void) | ||
{ | ||
int i, j, k, l; | ||
double complex n, d, q; | ||
|
||
printf("// # generated by cmplxdivide.c\n"); | ||
printf("\n"); | ||
printf("package main\n"); | ||
printf("var tests = []Test{\n"); | ||
for(i=0; i<nelem(f); i++) | ||
for(j=0; j<nelem(f); j++) | ||
for(k=0; k<nelem(f); k++) | ||
for(l=0; l<nelem(f); l++) { | ||
n = f[i] + f[j]*I; | ||
d = f[k] + f[l]*I; | ||
q = n/d; | ||
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))); | ||
} | ||
printf("}\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// $G $D/$F.go $D/cmplxdivide1.go && $L $D/$F.$A && ./$A.out | ||
|
||
// Copyright 2010 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Driver for complex division table defined in cmplxdivide1.go | ||
|
||
package main | ||
|
||
import ( | ||
"cmath" | ||
"fmt" | ||
"math" | ||
) | ||
|
||
type Test struct{ | ||
f, g complex128 | ||
out complex128 | ||
} | ||
|
||
var nan = math.NaN() | ||
var inf = math.Inf(1) | ||
var negzero = math.Copysign(0, -1) | ||
|
||
func calike(a, b complex128) bool { | ||
switch { | ||
case cmath.IsInf(a) && cmath.IsInf(b): | ||
return true | ||
case cmath.IsNaN(a) && cmath.IsNaN(b): | ||
return true | ||
} | ||
return a == b | ||
} | ||
|
||
func main() { | ||
for _, t := range tests { | ||
x := t.f/t.g | ||
if !calike(x, t.out) { | ||
fmt.Printf("%v/%v: expected %v error; got %v\n", t.f, t.g, t.out, x) | ||
} | ||
} | ||
} |
Oops, something went wrong.