tokuhirom's Blog

Floating point number decoding in JSON::XS

http://d.hatena.ne.jp/hiratara/20121024/1351054828
https://rt.cpan.org/Public/Bug/Display.html?id=80350

In some environment, JSON::XS's floating point number decoder has an issue.

Here is code:

use v5.16.0;
use JSON::XS;
my $dat = decode_json( "[0.6]" )->[0];

say int ( 24 / $dat );
say int ( 24 / 0.6 );

Outputs:

39
40

Why??

The result of Devel::Peek::Dump($dat) is following:

SV = NV(0x26fc7d8) at 0x26ef7c8
  REFCNT = 1
  FLAGS = (PADMY,NOK,pNOK)
  NV = 0.6

Aghhh

Ah, 0.6 is floating point number. "NV = 0.6" is a rounded presentation. Use printf instead...

use v5.16.0;
use JSON::XS;
my $dat = decode_json( "[0.6]" )->[0];
use Devel::Peek;
Dump($dat);
printf("%.20f\n", $dat);
printf("%.20f\n", 0.6);

Oh! Output is different!!

SV = NV(0x21c8820) at 0x21bb7c8
  REFCNT = 1
  FLAGS = (PADMY,NOK,pNOK)
  NV = 0.6
0.60000000000000008882
0.59999999999999997780

JSON::XS uses it's own json_atof() function for decoding floating point number. It's behavior is different between Perl5's floating point number decoder.

I seem the JSON dcoder's floating point number decoder should make same result with language processor's floating point number decoder.

I think JSON::XS needs to fix this issue.

One more more reason to fix this issue

I think JSON::PP and JSON::XS should return same value from same JSON data. Since these modules provides a wrapper interface named JSON.pm.

Current version of JSON::XS failed following test case in some environment:

use strict;
use warnings;
use 5.010000;

use JSON::XS ();
use JSON::PP ();
use Test::More;

my $xs = JSON::XS::decode_json("[0.6]")->[0];
my $pp = JSON::PP::decode_json("[0.6]")->[0];

ok($xs == $pp);
diag sprintf("%.20f", $xs);
diag sprintf("%.20f", $pp);

done_testing;

my environment

Summary of my perl5 (revision 5 version 16 subversion 0) configuration:
   
  Platform:
    osname=linux, osvers=3.2.0-23-generic, archname=x86_64-linux
    uname='linux www4071uf 3.2.0-23-generic #36-ubuntu smp tue apr 10 20:39:51 utc 2012 x86_64 x86_64 x86_64 gnulinux '
    config_args='-d -Dprefix=/usr/local/perl/5.16.0/'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.6.3', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.15'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector'