Bug #78627 [Opn->Nab]: acos(1) is giving NAN instead of 0
| From: | nikic@php.net | Date: | Fri, 04 Oct 2019 10:02:08 +0000 |
| Subject: | Bug #78627 [Opn->Nab]: acos(1) is giving NAN instead of 0 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223054@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78627&edit=1
ID: 78627
Updated by: nikic@php.net
Reported by: me at gmorel dot io
Summary: acos(1) is giving NAN instead of 0
-Status: Open
+Status: Not a bug
Type: Bug
Package: *Math Functions
Operating System: Linux Debian
PHP Version: 7.2.23
Block user comment: N
Private report: N
New Comment:
On the 3v4l shell the actual result is 1.0000000000000002, so acos() correctly returns NAN. This is
a typical floating point precision issue.
The difference between the results is (most likely) due to the used libc. Most people use glibc,
while the Docker images typically use musl, which will have different implementations of
trigonometric functions.
In any case, this is not a bug in PHP. Whatever behaviour you see here is determined by the
libc/libm implementation.
Previous Comments:
------------------------------------------------------------------------
[2019-10-04 09:46:35] benjamin at betti dot me
Hi,
It is not reproduced by 3v4l's "eval()" functionality, but it does actually happen
with their live shell, as with 7.2.20+ and 7.3.7+ docker images (but not with 7.2.19- nor 7.3.6-).
Not sure if it comes from php or from some libraries/dependancies bundled in the images.
From 3v4l's live shell:
-----------------------
PHP 7.3.0 (cli) (built: Dec 7 2018 10:59:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
Interactive shell
php > $dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
php > var_dump($dist, acos($dist));
float(1)
float(NAN)
https://i.imgur.com/hefGoU8.png
From docker images:
-------------------
benjamin@ontracks-benjamin î° docker pull php:7.2.19
7.2.19: Pulling from library/php
[...]
Digest: sha256:977a074d8fe3e7c7647223cadca37d48d166abd82218fea270a96dfbebb74bb4
Status: Downloaded newer image for php:7.2.19
benjamin@ontracks-benjamin î° docker run -it php:7.2.19 bash
root@89f076974e21:/# uname -a
Linux 89f076974e21 5.0.0-29-generic #31-Ubuntu SMP Thu Sep 12 13:05:32 UTC 2019 x86_64 GNU/Linux
root@89f076974e21:/# php -v
PHP 7.2.19 (cli) (built: Jul 3 2019 21:16:39) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
root@89f076974e21:/# php -a
Interactive shell
php > $dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
php > var_dump($dist, acos($dist));
float(1)
float(0)
benjamin@ontracks-benjamin î° docker pull php:7.2.20
7.2.20: Pulling from library/php
[...]
Digest: sha256:fddc1d9129c3fd84c0d95d1008d26c4c5284b9c8bc8e4a2983b1c0e421489423
Status: Downloaded newer image for php:7.2.20
benjamin@ontracks-benjamin î° docker run -it php:7.2.20 bash
root@ce8a5edd8426:/# uname -a
Linux ce8a5edd8426 5.0.0-29-generic #31-Ubuntu SMP Thu Sep 12 13:05:32 UTC 2019 x86_64 GNU/Linux
root@ce8a5edd8426:/# php -v
PHP 7.2.20 (cli) (built: Jul 12 2019 23:20:54) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
root@ce8a5edd8426:/# php -a
Interactive shell
php > $dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
php > var_dump($dist, acos($dist));
float(1)
float(NAN)
benjamin@ontracks-benjamin î° docker pull php:7.3.6
7.3.6: Pulling from library/php
fc7181108d40: Already exists
[...]
Digest: sha256:85b571a9138847f1a45ab29bc6f92a6ad3c7460e929e2ccc8b1035870984408d
Status: Downloaded newer image for php:7.3.6
benjamin@ontracks-benjamin î° docker run -it php:7.3.6 bash
root@919a1affaa36:/# uname -a
Linux 919a1affaa36 5.0.0-29-generic #31-Ubuntu SMP Thu Sep 12 13:05:32 UTC 2019 x86_64 GNU/Linux
root@919a1affaa36:/# php -v
PHP 7.3.6 (cli) (built: Jul 3 2019 20:46:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies
root@919a1affaa36:/# php -a
Interactive shell
php > $dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
php > var_dump($dist, acos($dist));
float(1)
float(0)
benjamin@ontracks-benjamin î° docker pull php:7.3.7
7.3.7: Pulling from library/php
[...]
Digest: sha256:9016d75d6aaa61da443f42cdf7e97994ecb4e1ceb94aeb6496dcf51cdaf132a2
Status: Downloaded newer image for php:7.3.7
benjamin@ontracks-benjamin î° docker run -it php:7.3.7 bash
root@2ee685f149d8:/# uname -a
Linux 2ee685f149d8 5.0.0-29-generic #31-Ubuntu SMP Thu Sep 12 13:05:32 UTC 2019 x86_64 GNU/Linux
root@2ee685f149d8:/# php -v
PHP 7.3.7 (cli) (built: Jul 12 2019 22:25:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
root@2ee685f149d8:/# php -a
Interactive shell
php > $dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
php > var_dump($dist, acos($dist));
float(1)
float(NAN)
------------------------------------------------------------------------
[2019-10-03 10:35:16] cmb@php.net
Cannot reproduce: <https://3v4l.org/LTLAg>.
------------------------------------------------------------------------
[2019-10-03 10:10:45] me at gmorel dot io
Description:
------------
---
From manual page: https://php.net/function.acos
---
Hi,
Thanks for all your work.
Since the PHP 7.2.20 the acos function seems to have a regression. We have first isolated the bug in
7.2.23
In the case of
$dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
Before acos($dist) => 0
After acos($dist) => NAN
uname -a
Linux 609379a2965b 5.0.0-29-generic #31-Ubuntu SMP Thu Sep 12 13:05:32 UTC 2019 x86_64 GNU/Linux
php -v
PHP 7.2.23 (cli) (built: Sep 26 2019 23:37:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.23, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules]
âKeep up the good work
â
Test script:
---------------
PHP 7.2.18 (sorry they didn't have 7.2.19)
http://sandbox.onlinephpfunctions.com/code/e59e94fa7aee04d67cbacf2990773c1658e4d1c2
It seems to have no issue in 7.3.5
But it appears also in 7.3.10 (cli)
Expected result:
----------------
$dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
var_dump(acos($dist)); // float(0)
Actual result:
--------------
$dist = sin(deg2rad(45.9007)) * sin(deg2rad(45.9007)) + cos(deg2rad(45.9007)) *
cos(deg2rad(45.9007));
var_dump(acos($dist)); // double(NAN)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78627&edit=1