Solved PHP float precision on FreeBSD 13.1-p0

Do we know how to control precision on OS level? Or maybe there is another reason for the wrong values.

Tested with PHP 8.1.7 and 7.4. Sometimes processes get to 100% usage giving blank pages but dtrace show there is nothing happening. The problem does not exists on Freebsd 13.

Code:
# php a.php
(object) array(
   'a' => 0.01,
   'b' => 0.02,
   'c' => 0.029999999999999999,
   'd' => 0.040000000000000001,
   'e' => 0.050000000000000003,
   'f' => 0.059999999999999998,
   'g' => 0.070000000000000007,
   'h' => 0.080000000000000002,
   'i' => 0.089999999999999997,
   'j' => 0.10000000000000001,
   'k' => 0.11,
   'l' => 0.12,
)

Code:
# php -v
PHP 8.1.7 (cli) (built: Jun 12 2022 00:24:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.7, Copyright (c), by Zend Technologies


PHP:
<?php
ini_set('precision', 0);
var_export(json_decode('{"a": 0.01, "b": 0.02, "c": 0.03, "d": 0.04, "e": 0.05, "f": 0.06, "g": 0.07, "h": 0.08, "i": 0.09, "j": 0.10, "k": 0.11, "l": 0.12}'));

Python 3.9 looks good:

Python:
Python 3.9.13 (main, May 22 2022, 13:55:38)
[Clang 13.0.0 (git@github.com:llvm/llvm-project.git llvmorg-13.0.0-0-gd7b669b3a on freebsd13
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> print(json.dumps({"a": 0.01, "b": 0.02, "c": 0.03, "d": 0.04, "e": 0.05, "f": 0.06, "g": 0.07, "h": 0.08, "i": 0.09, "j": 0.10, "k": 0.11, "l": 0.12}, sort_keys=True, indent=4))
{
    "a": 0.01,
    "b": 0.02,
    "c": 0.03,
    "d": 0.04,
    "e": 0.05,
    "f": 0.06,
    "g": 0.07,
    "h": 0.08,
    "i": 0.09,
    "j": 0.1,
    "k": 0.11,
    "l": 0.12
}
Any ideas? Thanks.
 
Can you try with others PHP versions ?
I can't reproduce the problem with PHP 8.0.20 on FreebSD 13.1

# php a.php
(object) array(
'a' => 0.01,
'b' => 0.02,
'c' => 0.03,
'd' => 0.04,
'e' => 0.05,
'f' => 0.06,
'g' => 0.07,
'h' => 0.08,
'i' => 0.09,
'j' => 0.1,
'k' => 0.11,
'l' => 0.12,
)

# php -v
PHP 8.0.20 (cli) (built: Jun 18 2022 18:31:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.20, Copyright (c) Zend Technologies
with Zend OPcache v8.0.20, Copyright (c), by Zend Technologies

# freebsd-version
13.1-RELEASE
 
PHP:
<?php
ini_set('precision', 0);
ini_set('serialize_precision', 0);

$json = '{"a": 0.01,"b": 0.02,"c": 0.03,"d": 0.04,"e": 0.05}';
var_export(json_decode($json, true));
var_dump(json_decode($json, true));

output:

Code:
array (
  'a' => 0.01,
  'b' => 0.02,
  'c' => 0.03,
  'd' => 0.04,
  'e' => 0.05,
)test.php:8:
array(5) {
  'a' =>
  double(0.01)
  'b' =>
  double(0.02)
  'c' =>
  double(0.03)
  'd' =>
  double(0.04)
  'e' =>
  double(0.05)
}

PHP:
<?php
ini_set('precision', 0);
ini_set('serialize_precision', 17);

$json = '{"a": 0.01,"b": 0.02,"c": 0.03,"d": 0.04,"e": 0.05}';
var_export(json_decode($json, true));
var_dump(json_decode($json, true));

output:

Code:
array (
  'a' => 0.01,
  'b' => 0.02,
  'c' => 0.029999999999999999,
  'd' => 0.040000000000000001,
  'e' => 0.050000000000000003,
)test.php:8:
array(5) {
  'a' =>
  double(0.01)
  'b' =>
  double(0.02)
  'c' =>
  double(0.03)
  'd' =>
  double(0.04)
  'e' =>
  double(0.05)
}

notice the serialize_precision
 
OP
spag
Can you try with others PHP versions ?
I can't reproduce the problem with PHP 8.0.20 on FreebSD 13.1
The same. on Freebsd 13.1 not on 13.0. The build is the same. Also, I have noticed that official packages work fine - but they are dedicated for v13.0
what's the value for serialize_precision?
Code:
#php -i | grep prec 
precision => 14 => 14
serialize_precision => -1 => -1
(binary pkg)

#php -i | grep prec 
precision => 14 => 14
serialize_precision => 17 => 17
(own build)

This is binary package that works fine:
Code:
#php -i
phpinfo()
PHP Version => 8.1.7

System => FreeBSD det006 13.1-RELEASE FreeBSD 13.1-RELEASE releng/13.1-n250148-fc952ac2212 GENERIC amd64
Build Date => Jun 11 2022 01:27:49
Build System => FreeBSD 130amd64-default-job-12 13.0-RELEASE-p11 FreeBSD 13.0-RELEASE-p11 amd64
Configure Command =>  './configure'  '--disable-all' '--program-prefix=' '--with-config-file-scan-dir=/usr/local/etc/php' '--with-layout=GNU' '--with-libxml' '--with-openssl' '--with-password-argon2=/usr/local' '--enable-dtrace' '--enable-embed' '--enable-fpm' '--with-fpm-group=www' '--with-fpm-user=www' '--enable-mysqlnd' '--prefix=/usr/local' '--localstatedir=/var' '--mandir=/usr/local/man' '--infodir=/usr/local/share/info/' '--build=amd64-portbld-freebsd13.0' 'build_alias=amd64-portbld-freebsd13.0' 'PKG_CONFIG=pkgconf' 'CFLAGS=-O2 -pipe -fstack-protector-strong -fno-strict-aliasing ' 'LDFLAGS=-L/usr/lib -lcrypto -lssl -fstack-protector-strong ' 'CPP=cpp' 'CXXFLAGS=-O2 -pipe -fstack-protector-strong -fno-strict-aliasing ' 'OPENSSL_CFLAGS=-I/usr/include' 'OPENSSL_LIBS=-L/usr/lib -lssl -lcrypto'

where my packages are built on 13.1 but the official repo points to 13.0.

Code:
#sysctl -a | grep osrel
kern.osrelease: 13.1-RELEASE
kern.osreldate: 1301000
kern.disallow_high_osrel: 0
security.jail.param.linux.osrelease: 65
security.jail.param.osrelease: 32
security.jail.param.osreldate: 0
compat.linux.osrelease: 2.6.32

Code:
 #pkg info lang/php81
php81-8.1.7
Name           : php81
Version        : 8.1.7
Installed on   : Mon Jun 27 20:29:20 2022 UTC
Origin         : lang/php81
Architecture   : FreeBSD:13:amd64
Prefix         : /usr/local
Categories     : www lang devel
Licenses       : PHP301
Maintainer     : bofh@FreeBSD.org
WWW            : https://www.php.net/
Comment        : PHP Scripting Language (8.1.X branch)
Options        :
        CGI            : on
        CLI            : on
        DEBUG          : off
        DTRACE         : on
        EMBED          : on
        FPM            : on
        IPV6           : on
        LINKTHR        : on
        MYSQLND        : on
        PHPDBG         : off
        ZTS            : off
Shared Libs required:
        libxml2.so.2
        libargon2.so.0
Shared Libs provided:
        libphp.so
Annotations    :
        FreeBSD_version: 1300139
        cpe            : cpe:2.3:a:php:php:8.1.7:::::freebsd13:x64
        repo_type      : binary
        repository     : FreeBSD
Flat size      : 29.9MiB


Setting ini_set('serialize_precision', 15); is almost good. This require to update all php.ini just in case.
 
Top