values with IOK and pNOK (but not NOK) encoded as floating point, losing precision
haarg opened this issue · comments
See the following script:
use strict;
use warnings;
use Cpanel::JSON::XS;
use Devel::Peek;
my $l = 412345678901234567;
Dump($l);
print encode_json([$l]) . "\n";
my $g = $l + 1.2;
Dump($l);
print encode_json([$l]) . "\n";
__END__
SV = IV(0x128023738) at 0x128023748
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 412345678901234567
[412345678901234567]
SV = PVNV(0x1280098f0) at 0x128023748
REFCNT = 1
FLAGS = (IOK,pIOK,pNOK)
IV = 412345678901234567
NV = 4.12345678901235e+17
PV = 0
[4.12345678901235e+17]
Using $l
as a float upgraded the value to PVNV and set the pNOK flag. But the NOK flag is not set, because the floating point value is not accurate. But Cpanel::JSON::XS switches to encoding the floating point value. JSON::XS also suffers from this issue.
I have to check why JSON::XS choose that logic