David Landgren wrote:
Spencer Ogden wrote:
OK, Math::RootFind it is. One more question, on interface:
All of the algorithms have two parameters, e (as in epsilon) and
max_iter, which set precision and max run-time respectively. They have
reasonable defaults, and probably won't be changed often, so I am
loathe to make them function arguments. So I have made them package
globals with accessors to alter them. The undesirable effect of course
is that altering them in one part of your program effects all uses of
the algorithm, which may not be intended. It looks like this.
bisection( \&func, -10,10 ); #Normal call with defaults
epsilon(.01);
max_iter(10);
bisection( \&func, -10, 10);
# From now on all calls will use the above e and max_iter
Oops, missed that the first time around, still, easily handled:
BEGIN {
my $max_iter = 5;
sub max_iter {
$max_iter = shift if @_;
$max_iter;
}
}
I still think putting them as function arguments would be redundant in
most cases, although maybe optional arguments would be best (just
thinking out loud). Any other suggestions as to how to handle sort of
'config' settings?
Use named parameters.
bisection( \&func );
bisection( \&func, min => -100 );
bisection( \&func, min => 20, max => 40 );
# ...
sub bisection {
my $codedref = shift;
my %args = ( min => -10, max => 10, iter => max_iter() );
^^^^^^^^^^
if( @_ ) {
my %param = @_;
$args{$_} = $param{$_} for keys %param;
}
...
}
David