source: doc/theses/mike_brooks_MMath/programs/hello-array.cfa @ d144c26

Last change on this file since d144c26 was bbf6a180, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

change to uw-ethesis and restructure source

  • Property mode set to 100644
File size: 1.6 KB
Line 
1
2#include <common.hfa>
3#include <bits/align.hfa>
4
5extern "C" {
6    int atoi(const char *str);
7}
8
9
10#include "stdlib.hfa"
11#include "array.hfa" // learned has to come afer stdlib, which uses the word tag
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// Usage:
29// ./a.out 5
30// example does an unchecked reference to argv[1]
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50forall( ztype( N ) )
51array(bool, N) & f( array(float, N) & a, array(float, N) & b ) {
52    array(bool, N) & ret = *alloc();
53    for( i; z(N) ) {
54        float fracdiff = 2 * abs( a[i] - b[i] )
55                       / ( abs( a[i] ) + abs( b[i] ) );
56        ret[i] = fracdiff < 0.005;
57    }
58    return ret;
59}
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98// TODO: standardize argv
99
100int main( int argc, char ** argv ) {
101    int n = atoi(argv[1]);
102    array(float, Z(n)) a, b;
103    for (i; n) {
104        a[i] = 3.14 / (i+1);
105        b[i] = a[i] + 0.005 ;
106    }
107    array(bool, Z(n)) & answer = f( a, b );
108    printf("answer:");
109    for (i; n)
110        printf(" %d", answer[i]);
111    printf("\n");
112    free( & answer );
113}
114/*
115$ ./a.out 5
116answer: 1 1 1 0 0
117$ ./a.out 7
118answer: 1 1 1 0 0 0 0
119*/
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136forall( ztype(M), ztype(N) )
137void not_so_bad(array(float, M) &a, array(float, N) &b ) {
138    f( a, a );
139    f( b, b );
140}
141
142
143
144
145
146
147
148#ifdef SHOWERR1
149
150forall( ztype(M), ztype(N) )
151void bad( array(float, M) &a, array(float, N) &b ) {
152    f( a, a ); // ok
153    f( b, b ); // ok
154    f( a, b ); // error
155}
156
157#endif
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196forall( ztype(M), ztype(N) )
197void bad_fixed( array(float, M) &a, array(float, N) &b ) {
198   
199
200    if ( z(M) == z(N) ) {
201        f( a, ( array(float, M) & ) b ); // fixed
202    }
203
204}
Note: See TracBrowser for help on using the repository browser.