]> Cypherpunks.ru repositories - gostls13.git/blob - test/cmplxdivide.c
delete float, complex - code changes
[gostls13.git] / test / cmplxdivide.c
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 iscnan(double complex d)
42 {
43         return !isinf(creal(d)) && !isinf(cimag(d)) && (isnan(creal(d)) || isnan(cimag(d)));
44 }
45
46 double complex zero;    // attempt to hide zero division from gcc
47
48 int
49 main(void)
50 {
51         int i, j, k, l;
52         double complex n, d, q;
53         
54         printf("// # generated by cmplxdivide.c\n");
55         printf("\n");
56         printf("package main\n");
57         printf("var tests = []Test{\n");
58         for(i=0; i<nelem(f); i++)
59         for(j=0; j<nelem(f); j++)
60         for(k=0; k<nelem(f); k++)
61         for(l=0; l<nelem(f); l++) {
62                 n = f[i] + f[j]*I;
63                 d = f[k] + f[l]*I;
64                 q = n/d;
65                 
66                 // BUG FIX.
67                 // Gcc gets the wrong answer for NaN/0 unless both sides are NaN.
68                 // That is, it treats (NaN+NaN*I)/0 = NaN+NaN*I (a complex NaN)
69                 // but it then computes (1+NaN*I)/0 = Inf+NaN*I (a complex infinity).
70                 // Since both numerators are complex NaNs, it seems that the
71                 // results should agree in kind.  Override the gcc computation in this case.
72                 if(iscnan(n) && d == 0)
73                         q = (NAN+NAN*I) / zero;
74
75                 printf("\tTest{complex(%s, %s), complex(%s, %s), complex(%s, %s)},\n",
76                         fmt(creal(n)), fmt(cimag(n)),
77                         fmt(creal(d)), fmt(cimag(d)),
78                         fmt(creal(q)), fmt(cimag(q)));
79         }
80         printf("}\n");
81         return 0;
82 }