1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // sum.c -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Wed May 27 17:56:53 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sat Nov 21 18:08:18 2015 |
---|
13 | // Update Count : 119 |
---|
14 | // |
---|
15 | |
---|
16 | extern "C" { |
---|
17 | int printf( char *, ... ); |
---|
18 | } |
---|
19 | #include "fstream.h" |
---|
20 | |
---|
21 | context sumable( type T ) { |
---|
22 | const T 0; |
---|
23 | T ?+?( T, T ); |
---|
24 | T ?+=?( T *, T ); |
---|
25 | T ++?( T * ); |
---|
26 | T ?++( T * ); |
---|
27 | }; |
---|
28 | |
---|
29 | forall( type T | sumable( T ) ) |
---|
30 | T sum( unsigned int n, T a[] ) { |
---|
31 | T total = 0; // instantiate T, select 0 |
---|
32 | for ( unsigned int i = 0; i < n; i += 1 ) |
---|
33 | total += a[i]; // select + |
---|
34 | return total; |
---|
35 | } |
---|
36 | |
---|
37 | // Required to satisfy sumable as char does not have addition. |
---|
38 | const char 0; |
---|
39 | char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion |
---|
40 | char ++?( char *op ) { *op += 1; return *op; } |
---|
41 | char ?++( char *op ) { char temp = *op; *op += 1; return temp; } |
---|
42 | |
---|
43 | int main() { |
---|
44 | const int low = 5, High = 15, size = High - low; |
---|
45 | |
---|
46 | ofstream *sout = ofstream_stdout(); |
---|
47 | |
---|
48 | char s = 0, a[size]; |
---|
49 | char v = low; |
---|
50 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
51 | s += v; |
---|
52 | a[i] = v; |
---|
53 | } |
---|
54 | sout << "sum from " << low << " to " << High << " is " |
---|
55 | << (int)sum( size, a ) << ", check " << (int)s << endl; |
---|
56 | |
---|
57 | int s = 0, a[size]; |
---|
58 | int v = low; |
---|
59 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
60 | s += (int)v; |
---|
61 | a[i] = (int)v; |
---|
62 | } |
---|
63 | sout << "sum from " << low << " to " << High << " is " |
---|
64 | << sum( size, (int *)a ) << ", check " << (int)s << endl; |
---|
65 | |
---|
66 | double s = 0.0, a[size]; |
---|
67 | double v = low / 10.0; |
---|
68 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
69 | s += (double)v; |
---|
70 | a[i] = (double)v; |
---|
71 | } |
---|
72 | printf( "%g\n", sum( size, (double *)a ) ); |
---|
73 | // sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is " |
---|
74 | // << sum( size, (double *)a ) << ", check " << (double)s << endl; |
---|
75 | |
---|
76 | float s = 0.0, a[size]; |
---|
77 | float v = low / 10.0; |
---|
78 | for ( int i = 0; i < size; i += 1, v += 0.1f ) { |
---|
79 | s += (float)v; |
---|
80 | a[i] = (float)v; |
---|
81 | } |
---|
82 | printf( "%g\n", sum( size, (float *)a ) ); |
---|
83 | // sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is " |
---|
84 | // << sum( size, (float *)a ) << ", check " << (float)s << endl; |
---|
85 | } |
---|
86 | |
---|
87 | // Local Variables: // |
---|
88 | // tab-width: 4 // |
---|
89 | // compile-command: "cfa sum.c fstream.o iostream.o" // |
---|
90 | // End: // |
---|