Changeset dbff8ec for doc/theses/mike_brooks_MMath/programs/hello-array.cfa
- Timestamp:
- Jul 10, 2024, 3:39:37 AM (9 months ago)
- Branches:
- master
- Children:
- 725f777f
- Parents:
- bb336a6 (diff), f3811df (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified doc/theses/mike_brooks_MMath/programs/hello-array.cfa ¶
rbb336a6 rdbff8ec 8 8 9 9 10 forall( [ N] ) // array bound11 array( bool, N) & f( array(float, N) & a, array(float, N) & b) {12 array( bool, N) & ret = *alloc(); // sizeof used by alloc13 for ( i; N) {14 ret[i] = 0.005 > 2 * (abs( a[i] - b[i])) / (abs(a[i]) + abs(b[i]));10 forall( [@N@] ) $\C{// array dimension}$ 11 array( bool, @N@) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) { 12 array( bool, @N@ ) & ret = *@alloc@(); $\C{// sizeof ret used by alloc}$ 13 for ( i; @N@ ) { 14 ret[i] = 0.005 > 2 * (abs( x[i] - y[i] )) / (abs( x[i]) + abs(y[i] )); 15 15 } 16 16 return ret; … … 29 29 30 30 int main( int argc, char * argv[] ) { 31 int n = ato( argv[1] );32 array( float, n) a, b; // VLA33 for ( i; n ) { 34 a[i] = 3.14 / (i + 1);35 b[i] = a[i] + 0.005 ;31 const int @n@ = ato( argv[1] ); $\C{// deduce conversion type}$ 32 array( float, @n@ ) x, y; $\C{// VLAs}$ 33 for ( i; n ) { $\C{// initialize arrays}$ 34 x[i] = 3.14 / (i + 1); 35 y[i] = x[i] + 0.005 ; 36 36 } 37 array( bool, n) & result = f( a, b ); // call38 sout | "result: " | nonl; 37 array( bool, @n@ ) & result = @f( x, y )@; $\C{// call}$ 38 sout | "result: " | nonl; $\C{// print result}$ 39 39 for ( i; n ) 40 40 sout | result[i] | nonl; 41 41 sout | nl; 42 free( &result ); // free returned storage42 free( &result ); $\C{// free result storage}$ 43 43 } 44 44 /* … … 50 50 51 51 void fred() { 52 array( float, 10) a;53 array( float, 20) b;54 f( a, a);55 f( b, b);56 f( a, b);52 array( float, @10@ ) x; 53 array( float, @20@ ) y; 54 f( x, x ); 55 f( y, y ); 56 // f( x, y ); 57 57 } 58 58 59 59 #ifdef SHOWERR1 60 60 forall( [M], [N] ) 61 void bad( array(float, M) & a, array(float, N) &b) {62 f( a, a ); // ok63 f( b, b ); // ok64 f( a, b ); // error61 void bad( array(float, M) &x, array(float, N) &y ) { 62 f( x, x ); $\C[1.5in]{// ok}$ 63 f( y, y ); $\C{// ok}$ 64 f( x, y ); $\C{// error}\CRT$ 65 65 } 66 66 #endif … … 69 69 70 70 forall( [M], [N] ) 71 void bad_fixed( array(float, M) & a, array(float, N) & b ) { 72 if ( M == N ) { 73 f( a, (array(float, M) &)b ); // cast b to matching type 74 } 71 void bad_fixed( array( float, M ) & x, array( float, N ) & y ) { 72 if ( M == N ) 73 f( x, @(array( float, M ) &)@y ); $\C{// cast y to matching type}$ 75 74 } 75 76 // Local Variables: // 77 // compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // 78 // End: //
Note: See TracChangeset
for help on using the changeset viewer.