Bug #67839 [Com]: mysqli does not handle 4-byte floats correctly
| From: | nikita at paymentwall dot com | Date: | Sun, 19 Oct 2014 08:09:47 +0000 |
| Subject: | Bug #67839 [Com]: mysqli does not handle 4-byte floats correctly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188186@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67839&edit=1
ID: 67839
Comment by: nikita at paymentwall dot com
Reported by: keyur@php.net
Summary: mysqli does not handle 4-byte floats correctly
Status: Closed
Type: Bug
Package: MySQLi related
Operating System: Linux
PHP Version: 5.4.31
Assigned To: keyur
Block user comment: N
Private report: N
New Comment:
There is an issue which occurs in the "else" condition in mysqlnd_ps_codec.c on line 224
https://github.com/php/php-src/commit/c044164a96553668cc6c4ae1eb32f18219308851#diff-244e2eb2c3973758baa5d722dc42e7c0
------------
PHP version: 5.4.34
OS: FreeBSD 10.0, FreeBSD 9.1
Compiled with clang, not GCC
------------
CREATE TABLE test_php_bug(id INT PRIMARY KEY, fp4 FLOAT(8,2), fp8 DOUBLE) ENGINE = InnoDB;
INSERT INTO test_php_bug(id, fp4, fp8) VALUES (1, 38.50, 38.50);
if (!($stmt = mysqli_prepare($link, "SELECT id, fp4, fp8 FROM test_php_bug WHERE id=1")))
{
die();
}
if (!mysqli_stmt_execute($stmt)) {
die();
}
if (!($result = mysqli_stmt_get_result($stmt))) {
die();
}
$data = mysqli_fetch_assoc($result);
print $data['id'] . ": " . $data['fp4'] . ": " .
$data['fp8'] . "\n";
-------------
Actual result:
1: 38: 38.5
Previous Comments:
------------------------------------------------------------------------
[2014-08-15 23:52:47] keyur@php.net
The fix for this bug has been committed.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
------------------------------------------------------------------------
[2014-08-14 05:40:28] keyur_govande at yahoo dot com
The patch for this bug is here: https://gist.github.com/keyurdg/9c99da11848ac7589e66
------------------------------------------------------------------------
[2014-08-14 05:36:00] keyur@php.net
Description:
------------
mysqli does not handle 4-byte floats correctly. It casts it into a double which essentially
increases the "error" in the IEEE-754 FP number.
Consider the following C code:
float a = 99.99f;
double b = (double) a;
The output will be:
a = 99.9899979
b = 99.989997863769531
b will never be printed as 99.99
Test script:
---------------
On a MySQL DB, run the following queries:
CREATE TABLE test(id INT PRIMARY KEY, fp4 FLOAT, fp8 DOUBLE) ENGINE = InnoDB;
INSERT INTO test(id, fp4, fp8) VALUES (1, 9.9999, 9.9999)
Now using mysqli, SELECT the one row out:
if (!($stmt = mysqli_prepare($link, "SELECT id, fp4, fp8 FROM test"))) {
die();
}
if (!mysqli_stmt_execute($stmt)) {
die();
}
if (!($result = mysqli_stmt_get_result($stmt))) {
die();
}
$data = mysqli_fetch_assoc($result);
print $data['id'] . ": " . $data['fp4'] . ": " .
$data['fp8'] . "\n";
Expected result:
----------------
It should be:
1: 9.9999: 9.9999
Actual result:
--------------
The output will be:
1: 9.9998998641968: 9.9999
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67839&edit=1