zaro Posted January 15, 2008 Share Posted January 15, 2008 I've been working on trying to calculate the interval between two known numbers with a dynamic amount of locations between those two numbers, I know this explanation isn't the best but its a little bit difficult for me to example, a sample of the datafile will help. This is my results from 0 to 157 with 516 numbers between it: 0.000,0.304,0.608,0.912,1.216,1.52,1.824,2.128,2.432,2.736,3.04,3.344,3.648,3.952,4.256,4.56,4.864,5.168,5.472,5.776,6.08,6.384,6.688,6.992,7.296,7.6,7.904,8.208, 8.512,8.816,9.12,9.424,9.728,10.032,10.336,10.64,10.944,11.248,11.552....156.092 This is the correct results from 0 to 157 with 516 numbers between it: 0,0.304,0.611,0.915,1.223,1.527,1.834,2.138,2.442,2.749,3.053,3.361,3.665,3.972,4.276,4.583,4.887,5.191,5.499,5.803,6.11,6.414,6.721,7.025,7.329,7.637,7.94,8.248, 8.552,8.859,9.163,9.471,9.775,10.078,10.386,10.69,10.997,11.301...157 This is the code I'm currently using to determine the Interval: if (!substr_compare($data[$i], "L", 0, 1)){ //0 if true for ($x=0;$x<$count;$x++){ if (!substr_compare($data[$i+1+$x], "L", 0, 1))break;//start with row after linestart }//Number of readings before new start and end $curredit[1]=number_format($currreplace[1],3);//Known Start $endedit=preg_split('/\s+/', $data[$i+$x-5]); $endedit[1]=$currreplace[2];//Known End $interval=(($endedit[1]-$curredit[1])/($x-2));//Calc 1st interval $string=$curredit[0]."\t".$curredit[1]."\t".$curredit[2]."\t".$curredit[3]."\t".$curredit[4]."\t".$curredit[5]."\t".$curredit[6]."\t".$curredit[7]."\n";//Format String $y=$x-2; fwrite($fw, $string); for ($z=6;$z<$x+1;$z++){ $currwrite=preg_split('/\s+/', $data[$i+$z]); $interval=(($endedit[1]-$curredit[1])/($y--)); $curredit[1]=$curredit[1]+$interval;//Recalc the interval $curredit[1]+=0.0005;//Round up $curredit[1]=number_format($curredit[1],3); $string=$currwrite[0]."\t".$curredit[1]."\t".$currwrite[2]."\t".$currwrite[3]."\t".$currwrite[4]."\t".$currwrite[5]."\t".$currwrite[6]."\t".$currwrite[7]."\n"; fwrite($fw, $string); } Hopefully this explains what I'm trying to accomplish, if not ask and I can provide more information. Thanks, Zaro Quote Link to comment Share on other sites More sharing options...
btherl Posted January 16, 2008 Share Posted January 16, 2008 Is the specification something like "Produce n equally spaced numbers starting at x and ending at y" ? My first impression is that you may be having rounding problems. I notice that you set $curredit[1] using number_format(), and then you use that value in a computation later. It's probably better if you use the original value (not formatted) for calculation, and only format for display. You may find that the problems disappear after that. Quote Link to comment Share on other sites More sharing options...
zaro Posted January 16, 2008 Author Share Posted January 16, 2008 Is the specification something like "Produce n equally spaced numbers starting at x and ending at y" ? Thats exactly what I'm trying to obtain, I couldn't quite put it into words. $curredit[1]=number_format($currreplace[1],3);//Known Start $curredit[1] is always a known whole number to start with, I don't even know why I rounded it, but that didn't fix the problem. Thanks, Zaro Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 16, 2008 Share Posted January 16, 2008 Well, im a little confused with the code you've attempted, and the problem. Im not sure why there are so many string functions, when we're dealing with numbers. However, if the problem is as btherl stated, then this might give a solution: <?php function numbers($start,$finish,$n,$dps=3){ if($n <= 1 || $start >= $finish){ return false; } $increment = ($finish - $start)/($n-1); //echo $increment; uncomment this line $array = array(); while($start <= $finish){ $array[] = number_format($start,$dps); $start += $increment; } if(count($array) < $n){ $array[] = $finish;//comment out this line } return $array; } echo '<pre>'.print_r(numbers(0,157,516),1).'</pre>'; ?> Note: There's always going to be a slight issue here. With such large numbers, and the rounding that occurs, the penultimate number generated + $increment is actually a touch larger than the value for $final. That was why i added that last if statement. As an example, using 0-157 in 516 steps, the last number generated is 156.695145631069 (to 12 d.p.), and the number we are incrementing by is 0.304854368932. Add them together, and you get 157.000000000001. So the last number (157) is not added to the array. If you follow the comments and uncomment and comment where required, you'll see what i mean. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.