in reply to Infinite loop but no reason why
Looking at your code reveals several errors which could be caught with some error checking. For the specific section of code you point out, I note that you have this line:
my $mse = function( $i, $j );But the function only uses one of those arguments:
sub function($_) #arbitary function...will be given in the partually w +ritten program { return ( $_[0] - 50.02 )**2 + 2; }
There a several problems here. First, the ($_) isn't doing anything. You think you're using a prototype but you're not. I'd remove it. How you actually want to handle your errors is a different story. Here's one way:
use Carp qw/croak/; # put near the top of your program sub function { if ( @_ != 2) { croak("function() requires two arguments"); } my ( $x, $y ) = @_; # ... body of function }
I also note that grid() is passed one more argument than you are using. You either need to test the number of arguments you are passing or test the value of each argument explicitly.
Also, as noted above, you're trying to compare floating point numbers with ==. That's bad and is likely to fail. Instead, you want to determine the what precision is acceptable and round the numbers to that and do a string compare (with eq). Or you could try the Math::Precision module (I haven't used it) or something similar.
To round the numbers and do a string compare, here's what perldoc perlop suggests (though it notes that this is expensive):
sub fp_equal { my ($X, $Y, $POINTS) = @_; my ($tX, $tY); $tX = sprintf("%.${POINTS}g", $X); $tY = sprintf("%.${POINTS}g", $Y); return $tX eq $tY; }
Cheers,
Ovid
New address of my CGI Course.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Infinite loop but no reason why
by ikegami (Patriarch) on Jul 19, 2006 at 15:10 UTC | |
by jhourcle (Prior) on Jul 19, 2006 at 16:12 UTC | |
by ikegami (Patriarch) on Jul 19, 2006 at 17:18 UTC |