Weird calculations
| From: | Marek Bialoglowy | Date: | Tue, 27 May 2003 11:50:49 +0000 |
| Subject: | Weird calculations | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-149085@lists.php.net to get a copy of this message | ||
I've found some problem which suprised me a little bit.
My PHP script is executing this SQL query:
mysql> SELECT indexes_values.value FROM indexes_values, indexes_names WHERE
locked=0 AND indexes_values.nameid=indexes_names.ID AND
indexes_names.name='MISC-IND' AND indexes_names.market='JSX' ORDER BY date
DESC LIMIT 2;
+----------+
| value |
+----------+
| 103.7916 |
| 102.8137 |
+----------+
2 rows in set (0.00 sec)
where VALUE is decimal(8,4), and this part of the script is processing
result:
[...]
if (mysql_numrows($result)>0) {
$rrow=mysql_fetch_row($result);
$v1 = $rrow[0];
$rrow=mysql_fetch_row($result);
$v2 = $rrow[0];
}
$curr_value = $v1;
$change_value = ($v1-$v2);
[...]
where non of the values such as $v1 or $change_value has been initialised
before, and finally executing
echo "$changed_value";
what shows result "0.97790000000001". Well from what I know result of
103.7916-102.8137 should always be 0.9779, right ? So what is the problem ?
Certailny if I will change
echo "$changed_value"; to
printf("%8.4f",$change_value); everything will be fine, but I am just
wondering why I get this wrong result in first place ?
I've found that the problem exists only for this particulal calculation, all
other calculations in the database does not create any problem. I will also
notice that echo "($v1,$v2)"; correctly displays
"(103.7916,102.8137)".
Best Regards,
Marek Bialoglowy (ultor@systemintegra.com) - IT Security Researcher
PGPkey: http://www.systemintegra.com/pgp/ultor.asc |
ID: 0x4B36656E
JOB: (CTO) System Integra | JKT, Indonesia | Timezone: JAVT, GMT +7