On Tue, Jul 28, 2015 at 7:10 AM, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:

> On Tue, Jul 28, 2015 at 6:54 AM, Anthony Ferrara <ircmax...@gmail.com>
> wrote:
>
>> On Sun, Jul 26, 2015 at 4:20 PM, Yasuo Ohgaki <yohg...@ohgaki.net> wrote:
>> > Hi Jakub,
>> >
>> > On Mon, Jul 27, 2015 at 3:32 AM, Jakub Zelenka <bu...@php.net> wrote:
>> >
>> >> 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.
>> >>
>> >
>> > OK. Better example.
>> >
>> > [yohgaki@dev PHP-master]$ ./php-bin
>> > <?php
>> > $j = '{ "v": 0.1234567890123456789 }';
>> > var_dump(json_encode(json_decode($j)));
>> > ini_set('precision', 20);
>> > var_dump(json_encode(json_decode($j)));
>> > ?>
>> >
>> >
>> > string(22) "{"v":0.12345678901235}"
>> > string(28) "{"v":0.12345678901234567737}"
>>
>> I think you missed the point. Parsing isn't dependent upon precision
>> setting. Only dumping: http://3v4l.org/48VSt
>>
>> $j = '{ "v": 0.1234567890123456789 }';
>> $d1 = json_decode($j);
>>
>> ini_set('precision', 20);
>>
>> $d2 = json_decode($j);
>>
>> var_dump($d1, $d2);
>>
>> //object(stdClass)#1 (1) { ["v"]=> float(0.12345678901234567737) }
>> //object(stdClass)#2 (1) { ["v"]=> float(0.12345678901234567737) }
>>
>> Meaning that it's parsed correctly.
>>
>> There is no bug here.
>
>
> I understands PHP uses pure IEEE data and arithmetic and
> I disagree that this is not a bug.
>
> What I'm pointing it out is "simple JSON operation truncates
> _valid/precise_ value".
> IEEE double can store 17 digit fraction part precisely, but PHP truncates
> it.
>
> > <?php
> > $j = '{ "v": 0.1234567890123456789 }';
> > var_dump(json_encode(json_decode($j)));
> > ini_set('precision', 20);
> > var_dump(json_encode(json_decode($j)));
> > ?>
> >
> >
> > string(22) "{"v":0.12345678901235}"
> > string(28) "{"v":0.12345678901234567737}"
>
> The same problem in serialize/unserialize was fixed as a bug.
> Encode/decode should be as precise as possible by _default_. IMO.
>

> What's the point of truncation and having broken value?
>


Get JSON data from Google maps and store the data using PHP, then
users lose last 2 digits of fraction part by default. The value is changed
and wrong. This is definitely a bug.

We can write

$old = ini_set('precision', 17);
json_encode($var);
ini_set('precision', $old);

everywhere to workaround this problem.

Question is "Is this the way it should be?".

--
Yasuo Ohgaki
yohg...@ohgaki.net

Reply via email to