Salve, ho un problema con un ciclo while.
Questo è il file php:
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<table border="1" cellpadding="2" cellspacing="0">
<tbody>
<tr>
<th>#</th>
<th>Number (-10 > +10)</th>
<th>Convert</th>
<th>Memory usage</th>
</tr>
<?php
function convert($string, $precision = 1, $comma = '.'){
$a = explode($comma,$string);
$int = $a[0];
$dec = $a[1];
$lengthofnum=strlen($dec);
$divider="1";
$d=0;
while($d < ($lengthofnum)) {
$divider.="0";
$d++;
}
$divider=(int)$divider;
if ($string < 0) {
$result=$int-($dec/$divider);
}else{
$result=$int+($dec/$divider);
}
if (is_float($result)) {$result = number_format($result,$precision);}
return $result;
}
$i = -10;
$m = 10;
$c = 0.15;
$n = 1;
while ($i <= $m) {
$mem = memory_get_usage();
echo "\t\t<tr>\n\t\t\t<td>". $n . "</td>\n\t\t\t<td>". $i . "</td>\n\t\t\t<td>" . convert($i) . "</td>\n\t\t\t<td>" . $mem . "</td>\n\t\t</tr>\n";
$i = $i + $c;
$n++;
}
?>
</tbody>
</table>
</body>
</html>
Il problema è questo: dopo un certo numero di cicli la somma di $i + $c non è più corretta.
Ecco la parte sbagliata dell'output:
# Number (-10 > +10) Convert Memory usage
59 -1.3 -1.3 55416
60 -1.15 -1.2 55416
61 -0.99999999999999 -46,566.1 55416
62 -0.84999999999999 -39,581.2 55416
63 -0.69999999999999 -32,596.3 55416
64 -0.54999999999999 -25,611.4 55416
65 -0.39999999999999 -18,626.5 55416
66 -0.24999999999999 -11,641.5 55416
67 -0.099999999999988 -46,566.1 55416
68 0.050000000000012 23,283.1 55416
69 0.20000000000001 9,313.2 55416
70 0.35000000000001 16,298.1 55416
71 0.50000000000001 23,283.1 55416
72 0.65000000000001 30,268.0 55416
73 0.80000000000001 37,252.9 55416
74 0.95000000000001 44,237.8 55416
75 1.1 1.1 55416
76 1.25 1.3 55416
Qualcuno sa spiegarmi il perchè faccia così?
P.S.: il server dove gira è su Aruba.it
Ciao