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.


In reply to Re: Infinite loop but no reason why by Ovid
in thread Infinite loop but no reason why by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.