Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87332 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51722 invoked from network); 27 Jul 2015 22:18:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jul 2015 22:18:29 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.180 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.160.180 mail-yk0-f180.google.com Received: from [209.85.160.180] ([209.85.160.180:35171] helo=mail-yk0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/51-33169-43EA6B55 for ; Mon, 27 Jul 2015 18:18:28 -0400 Received: by ykdu72 with SMTP id u72so81212652ykd.2 for ; Mon, 27 Jul 2015 15:18:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=WfgF9NUaWD8HmQNHWPo2jhbL5aJnsrQOFOP0dQyIp6g=; b=Nk8HhmglgMgCDrINFIuvZBxXeBavwo7rg1hGCMpR2uoH/sJYbjaSzq8Wi2eHRyk+Yv DFTrg4o5rcgHB9oNt1/00qJ5utk9yHVWOhnmXf+XeEyXyQd8grkcmiSvbqF/4BIB+6qO GsCc0uYvTNjgucgFe3OnQ5ShDI+ykyBEOZehIfKyvLnKd7Haw18H7bxblIGzCop71Spu mDgwbW/NtNEGJSaO8eFmCOHsKBXaoZB2e0xRIPKghWPX0GKiEaspYdwLGnwwHWDUlIKh pNGNcIvtybXCEWyI74641sg2K16OfibWgAdLDky32KwvGrbib6NLKMXHSyPPekhIwefG HEAw== X-Received: by 10.129.98.3 with SMTP id w3mr31599786ywb.32.1438035506058; Mon, 27 Jul 2015 15:18:26 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.129.40.77 with HTTP; Mon, 27 Jul 2015 15:17:46 -0700 (PDT) In-Reply-To: References: Date: Tue, 28 Jul 2015 07:17:46 +0900 X-Google-Sender-Auth: AHNl16ojBVtIxQwRBrfZzw8IrA0 Message-ID: To: Anthony Ferrara Cc: Jakub Zelenka , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a1146c814835dd4051be2bb3f Subject: Re: [PHP-DEV] json_decode/encode should return full precision values by default From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a1146c814835dd4051be2bb3f Content-Type: text/plain; charset=UTF-8 On Tue, Jul 28, 2015 at 7:10 AM, Yasuo Ohgaki wrote: > On Tue, Jul 28, 2015 at 6:54 AM, Anthony Ferrara > wrote: > >> On Sun, Jul 26, 2015 at 4:20 PM, Yasuo Ohgaki wrote: >> > Hi Jakub, >> > >> > On Mon, Jul 27, 2015 at 3:32 AM, Jakub Zelenka 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 >> > > > $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. > > > > $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 yohgaki@ohgaki.net --001a1146c814835dd4051be2bb3f--