ZF-8946: Invalid Zend_Validate_Float string validations
Description
Zend_Validate_Float is returning some invalid validations. For example, strings "74.8960196583719" and "169.76664148522" are evaluated as invalid. See example below (first tests are for baseline and were taken from ZF-5879):
Tests:
<?php
require_once 'Zend/Validate.php';
$floats = array(// floats
0.000000234,
1.0000000234,
0.000050004,
0.005000004,
// strings
'0.000000234',
'1.0000000234',
'0.000050004',
'0.005000004');
foreach ($floats as $f) {
echo "$f: ";
var_dump(Zend_Validate::is($f,'Float'));
}
echo "----------------------------------------------------------\n";
$floats = array(// floats
74.8960196583719,
169.76664148522,
// strings
'74.8960196583719',
'169.76664148522');
foreach ($floats as $f) {
echo "$f: ";
var_dump(Zend_Validate::is($f,'Float'));
}
Result/output:
2.34E-7: bool(true)
1.0000000234: bool(true)
5.0004E-5: bool(true)
0.005000004: bool(true)
0.000000234: bool(true)
1.0000000234: bool(true)
0.000050004: bool(true)
0.005000004: bool(true)
----------------------------------------------------------
74.8960196584: bool(true)
169.766641485: bool(true)
74.8960196583719: bool(false)
169.76664148522: bool(false)
Comments
Posted by Thomas Weidner (thomas) on 2010-01-26T23:08:17.000+0000
Please test with release 1.10. 1.9.7 is outdated and several bug fixes have been integrated.
Additionally we need some informations from your system for reproduction. * Systems locale * OS * PHP version * PHP's settings for number calculation
Posted by Shawn Iwinski (siwinski) on 2010-01-27T08:19:23.000+0000
Downloaded and tested with release 1.10.0rc1. Created new test script with additional environment output. Also logic change because "Zend_Validate::is($f,'Float')" threw the exception "Validate class not found from basename 'float'".
OS is RHEL 4.
String "169.7666414852" validates correctly with release 1.10.0rc1, but string "74.8960196583719" still does not.
Test:
Result/output:
Posted by Thomas Weidner (thomas) on 2010-01-27T23:05:19.000+0000
Main reason is that long floats are automatically converted by PHP but it takes into account the precision setting in your PHP ini configuration.
This means that long precision values are stripped and therefor are no longer identical with the original value...
For example: 10.1234567890123456789 will automatically be rounded to 10.1234567890123 (when you use the default setting of a precision to 13 digits within PHP.ini)
As both values are NOT identical a false is returned.
Posted by John Kelly (postalservice14) on 2011-05-27T22:44:12.000+0000
Seems to be fixed in the latest version.
Posted by Thomas Weidner (thomas) on 2011-08-08T21:15:04.000+0000
Marked as resolved according to latest reply. Not reproduceable with latest release.