Hi,

On Sat, Jul 25, 2015 at 11:01 PM, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:

> Hi all,
>
> I had to work with Google Map API and need to handle values precisely.
> Fortunately, it seems values are IEEE double, but I get rounded float
> values by default.
>
> For example,
> <?php
> $json = '
> {
>    "results" : [
>       {
>          "elevation" : 1608.637939453125,
>          "location" : {
>             "lat" : 39.73915360,
>             "lng" : -104.98470340
>          },
>          "resolution" : 4.771975994110107
>       },
>       {
>          "elevation" : -50.78903579711914,
>          "location" : {
>             "lat" : 36.4555560,
>             "lng" : -116.8666670
>          },
>          "resolution" : 19.08790397644043
>       }
>    ],
>    "status" : "OK"
> }
> ';
>
> var_dump(json_decode($json));
> ?>
>
> object(stdClass)#5 (2) {
>   ["results"]=>
>   array(2) {
>     [0]=>
>     object(stdClass)#1 (3) {
>       ["elevation"]=>
>       float(1608.6379394531)
>       ["location"]=>
>       object(stdClass)#2 (2) {
>         ["lat"]=>
>         float(39.7391536)
>         ["lng"]=>
>         float(-104.9847034)
>       }
>       ["resolution"]=>
>       float(4.7719759941101)
>     }
>     [1]=>
>     object(stdClass)#3 (3) {
>       ["elevation"]=>
>       float(-50.789035797119)
>       ["location"]=>
>       object(stdClass)#4 (2) {
>         ["lat"]=>
>         float(36.455556)
>         ["lng"]=>
>         float(-116.866667)
>       }
>       ["resolution"]=>
>       float(19.08790397644)
>     }
>   }
>   ["status"]=>
>   string(2) "OK"
> }
>
>
> json_decode()/json_encode() must be able to handle precise IEEE double
> value by _default_.
>
> serialize() is changed to use max precision. json_decode/encode should do
> the same at least.
>
> I think this change should be provided as bug fix.
> Any comments?
>
>
I don't think that this is a bug. Your example is also completely unrelated
to json because the place when the value is rounded is var_dump where it's
based on ini precision. You would get the same values with json_encode but
it's only because it uses the same ini ( precision ). Basically it has
nothing to do with json_decode.

I see that that the bug report you created (
https://bugs.php.net/bug.php?id=70137 ) is actually about using
serialize.precision instead of precision in json_encode . I agree that
using 'precision' ini is not the best solution. However I don't think that
start suddenly using serialize.precision is a good idea. First of all it's
a big BC break that will be quite difficult to find (There is no way this
should go to the point release because this is changing the generated
json). Also the json encode usage is much wider than serialization. There
might be use cases when you don't care about maximal precision and you
prefer to save some space. Imagine that you are transferring lots of float
numbers. Then it will result in increasing of the transfer size. It would
be great if the new ini was used when it was introduced but I'm afraid that
changing that now is not a good idea

I think that due to BC break, this shouldn't be changed. If you really want
a bigger precision, you can do ini_set('precision', 30) before json_encode
and then set it back. It means that this not a bug because you can still
change it if you want. That means that we can't change before PHP 8 or
whatever comes after 7... :)

Cheers

Jakub

Reply via email to