Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am a brand new perl user but I have plenty of C++ experiance in coding. Right now I am writing a optimization program. What I have thuse far is this:
@result=grid(2,-2,2,-2,4,1e-15,1000); print"\n*@result*"; ($user,$system,$cuser,$csystem) = times; print "\n***\n$user\n***"; sub grid($_) { my $max_rsd = $_[0]; my $min_rsd = $_[1]; my $max_shift = $_[2]; my $min_shift = $_[3]; my $resolution = $_[4]; my $precision = $_[5]; my $loopcount = 0; my $delta_rsd = ($max_rsd-$min_rsd)*(1/$resolution); my $delta_shift = ($max_shift-$min_shift)*(1/$resolution); my @memory = (0,0,10000); do { #******Find Min Value******* for($i=$min_rsd;$i<=$max_rsd;$i=$i+$delta_rsd) { #print "(i: $i, Max: $max_rsd, Min: $min_rsd,\n $min_rsd+$delta_rs +d)"; for($j=$min_shift;$j<=$max_shift;$j=$j+$delta_shift) { my $mse = function($i, $j);#********************* $temp=$min_shift+$delta_shift; print "(j: $j, Max: $max_shift, Min: $min_shift,\n d: $delta_shift + +: $temp"; if($mse < $memory[2]) { @memory = ($i, $j, $mse); #print "($memory[0],$memory[1])"; } if($temp==$min_shift){print" true)\n";} else{print" false)\n";} } } #******Focus Parameters******* #$max_rsd = $memory[0]+$delta_rsd; #$min_rsd = $memory[0]-$delta_rsd; #$max_shift = $memory[1]+$delta_shift; #$min_shift = $memory[1]-$delta_shift; #$delta_rsd = ($max_rsd-$min_rsd)*(1/$resolution); #$delta_shift = ($max_shift-$min_shift)*(1/$resolution); print"($memory[0], $max_rsd, $min_rsd,\n $delta_rsd)"; if($memory[0] == $max_rsd or $memory[0] == $min_rsd) { $max_rsd = $memory[0]+$delta_rsd*$resolution*(1/2); $min_rsd = $memory[0]-$delta_rsd*$resolution*(1/2); $max_shift = $memory[1]+$delta_shift*$resolution*(1/2); $min_shift = $memory[1]-$delta_shift*$resolution*(1/2); } else { $max_rsd = $memory[0]+$delta_rsd*(1/2); $min_rsd = $memory[0]-$delta_rsd*(1/2); $max_shift = $memory[1]+$delta_shift*(1/2); $min_shift = $memory[1]-$delta_shift*(1/2); $delta_rsd = ($max_rsd-$min_rsd)*(1/$resolution); $delta_shift = ($max_shift-$min_shift)*(1/$resolution); } $loopcount++; if($max_rsd == $min_rsd){print " e\n";} else{print " n\n";} } while((2*$delta_rsd>$precision));#and ($loopcount<$_[6])); #while(($delta_rsd!=0) and ($delta_shift!=0) and ($loopcount<$_[6] +)); return @memory; } sub function($_) #arbitary function...will be given in the partually +written program { return ($_[0]-50.02)**2+2; }
From what I can tell the logic is good. The issue is that the program gets caught in the infinate loop below:
for($j=$min_shift;$j<=$max_shift;$j=$j+$delta_shift) { my $mse = function($i, $j);#********************* $temp=$min_shift+$delta_shift; print "(j: $j, Max: $max_shift, Min: $min_shift,\n d: $delta_shift + +: $temp"; if($mse < $memory[2]) { @memory = ($i, $j, $mse); #print "($memory[0],$memory[1])"; } if($temp==$min_shift){print" true)\n";} else{print" false)\n";} }
From what I can gather, the $j should be increasing in defined increments of $delta_shift but I don't know if it is or not. The $temp and $min_shift appear as the same number on the screen but when I ask the computer if they are the same it tells me no.
My theory is this: the computer can only store a certain precision of a number. $min_shift is at the limit when I try to add $delta_shift to it so it is never really added to the number which would mean the number never increments as it should getting it stuck in an infinate loop. Could someone please take a look at it and let me know if I am right and then how I might go about fixing it. Thanks.
20060719 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Infinite loop but no reason why
by philcrow (Priest) on Jul 19, 2006 at 13:48 UTC | |
|
Re: Infinite loop but no reason why
by Ovid (Cardinal) on Jul 19, 2006 at 14:43 UTC | |
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 |