#include <fstream.hfa>
#include <stdlib.hfa>
#include <array.hfa> // learned has to come afer stdlib, which uses the word tag

// Usage:
// ./a.out 5
// example does an unchecked reference to argv[1]


forall( [N] ) // array bound
array(bool, N) & f( array(float, N) & a, array(float, N) & b ) {
    array(bool, N) & ret = *alloc(); // sizeof used by alloc
    for( i; N ) {
        ret[i] = 0.005 > 2 * (abs(a[i] - b[i])) / (abs(a[i]) + abs(b[i]));
    }
    return ret;
}










// TODO: standardize argv

int main( int argc, char * argv[] ) {
    int n = ato( argv[1] );
    array(float, n) a, b; // VLA
    for ( i; n ) {
        a[i] = 3.14 / (i + 1);
        b[i] = a[i] + 0.005 ;
    }
    array(bool, n) & result = f( a, b ); // call
    sout | "result: " | nonl;
    for ( i; n )
        sout | result[i] | nonl;
    sout | nl;
    free( &result ); // free returned storage
}
/*
$\$$ ./a.out 5
result: true true true false false
$\$$ ./a.out 7
result: true true true false false false false
*/

void fred() {
	array(float, 10) a;
	array(float, 20) b;
    f( a, a );
    f( b, b );
    f( a, b );
}

#ifdef SHOWERR1
forall( [M], [N] )
void bad( array(float, M) &a, array(float, N) &b ) {
    f( a, a ); // ok
    f( b, b ); // ok
    f( a, b ); // error
}
#endif



forall( [M], [N] )
void bad_fixed( array(float, M) & a, array(float, N) & b ) {
    if ( M == N ) {
        f( a, (array(float, M) &)b ); // cast b to matching type
    }
}
