rfc:precise_float_value
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:precise_float_value [2015/09/01 02:23] – yohgaki | rfc:precise_float_value [2016/06/05 19:02] – Update voting choice bukka | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: More precise float value handling ====== | ====== PHP RFC: More precise float value handling ====== | ||
- | * Version: | + | * Version: |
* Date: 2015-07-30 | * Date: 2015-07-30 | ||
- | * Author: Yasuo Ohgaki < | + | * Author: Yasuo Ohgaki < |
* Status: Under Discussion | * Status: Under Discussion | ||
* First Published at: http:// | * First Published at: http:// | ||
Line 8: | Line 8: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC is based on float like JSON numeric | + | This RFC is based on the discussion |
- | JSON is used to exchange data between systems. Although [[https:// | + | JSON is used to exchange data between systems. Although [[https:// |
- | Currently, json_encode() uses EG(precision) which is 14. IEEE 754 double supports | + | Currently, json_encode() uses EG(precision) which is set to 14. That means that 14 digits max are used for displaying (printing) the number. IEEE 754 double supports |
<code php> | <code php> | ||
Line 40: | Line 40: | ||
</ | </ | ||
- | PHP's float type stores " | + | PHP's float type stores " |
Current PHP outputs meaningless values for oversized EG(precision)/ | Current PHP outputs meaningless values for oversized EG(precision)/ | ||
Line 57: | Line 57: | ||
float(0.12345678901234567736988623209981597028672695159912109375) | float(0.12345678901234567736988623209981597028672695159912109375) | ||
</ | </ | ||
+ | |||
+ | That is caused by used mode for double to string conversion. | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | * PHP 7.0: Introduce | + | This RFC proposes to introduce |
- | + | ||
- | Since JSON is used extensively for web apps, we may have JSON specific INI setting for better compatibility and ease of use. | + | |
- | + | ||
- | * PHP 7.0: Add JSON_G(json_precision) - Add JSON module specific precision INI setting. | + | |
- | + | ||
- | PHP 5.6's json_encode() uses EG(precision) and could lose floating point number information. | + | |
- | + | ||
- | * PHP 5.6: Make json_encode() uses PG(serialize_precision). | + | |
+ | The RFC also proposes changing ini for JSON precision to PG(serialize_precision). | ||
Followings are sample codes and outputs of the proposed patch. | Followings are sample codes and outputs of the proposed patch. | ||
Line 80: | Line 75: | ||
ini_set(' | ini_set(' | ||
ini_set(' | ini_set(' | ||
- | ini_set(' | ||
var_dump($v); | var_dump($v); | ||
Line 103: | Line 97: | ||
ini_set(' | ini_set(' | ||
ini_set(' | ini_set(' | ||
- | ini_set(' | ||
var_dump($v); | var_dump($v); | ||
echo var_export($v, | echo var_export($v, | ||
+ | ini_set(' | ||
echo json_encode($v), | echo json_encode($v), | ||
+ | ini_set(' | ||
echo $v, PHP_EOL; | echo $v, PHP_EOL; | ||
?> | ?> | ||
Line 123: | Line 118: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | PHP 7.0 will have 0 mode as the default. var_export()/ | + | Setting |
- | FYI: Existing | + | The BC break could happen only if someone would rely on exact output but that shouldn' |
None when old INI value is used. | None when old INI value is used. | ||
Line 131: | Line 126: | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | * PHP 7.0.0 - EG(precision)/ | + | * PHP 7.1 |
- | * PHP 5.6.x - JSON uses PG(serialize_precision) for float precision. | + | |
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Line 142: | Line 136: | ||
* 0 mode (EG(precision)= -1) float outputs values rounded to nearest. | * 0 mode (EG(precision)= -1) float outputs values rounded to nearest. | ||
- | Standard module | + | Standard module |
- | * serialize(), | + | * serialize(), |
- | + | ||
- | JSON | + | |
- | * json_encode() - PHP7.0: | + | |
==== To Opcache ==== | ==== To Opcache ==== | ||
Line 164: | Line 154: | ||
* php.ini-production values : 14 Unmodified | * php.ini-production values : 14 Unmodified | ||
- | serialize_precision(PHP5.6) | + | serialize_precision |
- | * hardcoded default values : 17 Unmodified | + | |
- | * php.ini-development values : 17 Unmodified | + | |
- | * php.ini-production values : 17 Unmodified | + | |
- | + | ||
- | serialize_precision(PHP7) | + | |
- | * hardcoded default values : -1 | + | |
- | * php.ini-development values : -1 | + | |
- | * php.ini-production values : -1 | + | |
- | + | ||
- | json_precision(NEW - PHP7 only) | + | |
* hardcoded default values : -1 | * hardcoded default values : -1 | ||
* php.ini-development values : -1 | * php.ini-development values : -1 | ||
Line 190: | Line 170: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | |||
- | * Remove PG(serialize_precision) setting. If we are going to remove PG(serialize_precision) in the future, we may be better to have JSON_G(json_precision) as one may need specific precision for JSON data. | ||
WDDX | WDDX | ||
Line 202: | Line 180: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Requires a 50%+1 majority | + | Requires a 2/3 majority |
- | Please choose Yes/No for 0 mode EG(precision)/ | + | There will be two votings |
- | <doodle title=" | + | * whether to introduce mode 0 and use it as default for serialize_precision |
- | * Yes | + | * should PG(serialize_precision) be used instead of EG(precision) |
- | * No | + | |
- | </ | + | |
- | . | + | ===== Patches and Tests ===== |
- | Please choose your preference whether JSON module should use PG(serialize_precision) or new JSON_G(json_precision). i.e. JSON module has its own precision setting or not. | + | The initial PR can be found here: |
- | <doodle title=" | + | * https:// |
- | * Yes | + | |
- | * No | + | |
- | </doodle> | + | |
- | . | + | Note that the PR is currently outdate but it will be updated |
- | + | ||
- | Please choose | + | |
- | + | ||
- | <doodle title=" | + | |
- | * Yes | + | |
- | * No | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | ===== Patches and Tests ===== | + | |
- | + | ||
- | * https:// | + | |
===== Implementation ===== | ===== Implementation ===== | ||
Line 242: | Line 203: | ||
===== References ===== | ===== References ===== | ||
- | https:// | + | * https:// |
+ | * https:// | ||
+ | * http:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Keep this updated with features that were discussed on the mail lists. | Keep this updated with features that were discussed on the mail lists. | ||
rfc/precise_float_value.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1