-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed bug #50224 where float without decimals were converted to integer when encode to JSON #642
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4c263b6
Fixed bug #50224 where float without decimals were converted to integer
jrbasso 56b4ba5
Replacing use of strstr to strchr for performance reasons
jrbasso 9bb2248
Performance updates
jrbasso cbe1360
Performance optimizations
jrbasso 52a8fb0
Changed how float numbers are converted to JSON strings
jrbasso bb6697b
Fixing pointer address to avoid compiler warnings
jrbasso 408b7f7
Reducing the size of string for float to string conversion
jrbasso a64736e
Fixed typo
jrbasso 2468194
Using cast instead of getting the reference of first element
jrbasso dd52b7b
Using values from float.h and fallbacking to hardcoded value
jrbasso 8c41b03
Added JSON_PRESERVE_FRACTIONAL_PART option
jrbasso 4a53df1
Optimized the original code when the option JSON_PRESERVE_FRACTIONAL_…
jrbasso 8ff879d
Added a condition to avoid overflow
jrbasso 6c8d3dd
Renamed constant name after discussion on internals list
jrbasso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--TEST-- | ||
bug #50224 (json_encode() does not always encode a float as a float) | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("json")) print "skip"; ?> | ||
--FILE-- | ||
<?php | ||
echo "* Testing JSON output\n\n"; | ||
var_dump(json_encode(12.3, JSON_PRESERVE_ZERO_FRACTION)); | ||
var_dump(json_encode(12, JSON_PRESERVE_ZERO_FRACTION)); | ||
var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION)); | ||
var_dump(json_encode(0.0, JSON_PRESERVE_ZERO_FRACTION)); | ||
var_dump(json_encode(array(12, 12.0, 12.3), JSON_PRESERVE_ZERO_FRACTION)); | ||
var_dump(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION)); | ||
|
||
echo "\n* Testing encode/decode symmetry\n\n"; | ||
|
||
var_dump(json_decode(json_encode(12.3, JSON_PRESERVE_ZERO_FRACTION))); | ||
var_dump(json_decode(json_encode(12, JSON_PRESERVE_ZERO_FRACTION))); | ||
var_dump(json_decode(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION))); | ||
var_dump(json_decode(json_encode(0.0, JSON_PRESERVE_ZERO_FRACTION))); | ||
var_dump(json_decode(json_encode(array(12, 12.0, 12.3), JSON_PRESERVE_ZERO_FRACTION))); | ||
var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION))); | ||
var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION), true)); | ||
?> | ||
--EXPECTF-- | ||
* Testing JSON output | ||
|
||
string(4) "12.3" | ||
string(2) "12" | ||
string(4) "12.0" | ||
string(3) "0.0" | ||
string(14) "[12,12.0,12.3]" | ||
string(27) "{"float":12.0,"integer":12}" | ||
|
||
* Testing encode/decode symmetry | ||
|
||
float(12.3) | ||
int(12) | ||
float(12) | ||
float(0) | ||
array(3) { | ||
[0]=> | ||
int(12) | ||
[1]=> | ||
float(12) | ||
[2]=> | ||
float(12.3) | ||
} | ||
object(stdClass)#%d (2) { | ||
["float"]=> | ||
float(12) | ||
["integer"]=> | ||
int(12) | ||
} | ||
array(2) { | ||
["float"]=> | ||
float(12) | ||
["integer"]=> | ||
int(12) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that not a tad, er, excessive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TazeTSchnitzel according with http://tigcc.ticalc.org/doc/float.html the value if the constants are defined is
3 + 16 - -999
= 1018.There is research from @bukka and he explains on this comment:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually allocate less space on the stack than it was before this patch - The spprintf (xbuf_format_converter) allocates 2048 + additional space for other variables and function stack . See http://lxr.php.net/xref/PHP_TRUNK/main/spprintf.c#xbuf_format_converter for more details. As I said 1080 won't be probably used in any case as DBL_MANT_DIG, DBL_MIN_EXP are almost always defined so it will be mostly 1079 :). You might think that it's not necessary as the EG(precision) will be always smaller. Unfortunately we don't know its value at the C compile time (the dynamic allocation leads to the worse perf so we need to allocate on the stack). The only better way might be using alloca. I plan to experiment with that later to see if there no perf penalty but we need to have fallback anyway for non-alloca platforms. That requires the max space for double value otherwise there would be chance of the stack overflow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this was C99 we could do dynamic stack allocations. Alas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, the constants are defined on windows, linux and mac. So just in rare cases it will fallback to the hardcoded value.