Hi Anthony, 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? Regards, -- Yasuo Ohgaki yohg...@ohgaki.net