php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78627 acos(1) is giving NAN instead of 0
Submitted: 2019-10-03 10:10 UTC Modified: 2019-10-05 19:08 UTC
From: me at gmorel dot io Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 7.2.23 OS: Linux Debian
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: me at gmorel dot io
New email:
PHP Version: OS:

 

 [2019-10-03 10:10 UTC] 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)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-03 10:35 UTC] cmb@php.net
Cannot reproduce: <https://3v4l.org/LTLAg>.
 [2019-10-04 09:46 UTC] 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-04 10:02 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2019-10-04 10:02 UTC] nikic@php.net
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.
 [2019-10-05 19:08 UTC] me at gmorel dot io
Thank you for your reply
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 01:01:28 2024 UTC