jtlc/tests/Interpolator.jtl
josh bb982b62b8 modified example programs, added strings to lexer
git-svn-id: svn://anubis/jtlc/trunk@6 f5bc74b8-7b62-4e90-9214-7121d538519f
2010-01-12 20:23:08 +00:00

38 lines
787 B
Plaintext

module Interpolator;
import std.io.*;
typedef function double (double) interpolator_t;
public main(args : String[]) : int
{
interpolator_t i1 = getInterpolator(1, 5, 4);
interpolator_t i2 = getInterpolator(10, 2, 5);
assert( i1(0) =~ 1 );
assert( i1(4) =~ 5 );
assert( i1(3) =~ 4.0 );
assert( i2(0) =~ 10 );
assert( i2(5) =~ 2 );
assert( i2(1.0) =~ 8.4 );
assert( i2(2.0) =~ 6.8 );
return 0;
}
/*
* Create and return a linear interpolator functor which interpolates
* between (0.0, x1) and (sep, x2), returning the output y-value
*/
private getInterpolator(x1 : double, x2 : double, sep : double) : interpolator_t
{
assert (sep > 0.0);
return function double (x : double)
{
return x * (x2 - x1) / sep + x1;
}
}