php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75192 Spaceship operator returns wrong value for GMP numbers
Submitted: 2017-09-12 01:34 UTC Modified: 2017-09-12 09:48 UTC
From: marcospassos dot com at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: GNU MP related
PHP Version: 7.1.9 OS: Mac
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: marcospassos dot com at gmail dot com
New email:
PHP Version: OS:

 

 [2017-09-12 01:34 UTC] marcospassos dot com at gmail dot com
Description:
------------
Operator overloading does not work for the spaceship operator.

Test script:
---------------
$result = gmp_init('1') <=> gmp_init('-1');

Expected result:
----------------
1

Actual result:
--------------
2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-09-12 01:38 UTC] marcospassos dot com at gmail dot com
In fact, the operator overloading works, but the result is not compatible with the operator specification.
 [2017-09-12 05:19 UTC] requinix@php.net
-Status: Open +Status: Analyzed
 [2017-09-12 05:19 UTC] requinix@php.net
gmp's <=> overload wraps gmp_cmp() so it should be a pretty simple fix to gmp.c:gmp_compare to bound the return to -1/+1.
Though personally I wish <=> wasn't so rigid to begin with.
 [2017-09-12 09:48 UTC] cmb@php.net
-Status: Analyzed +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2017-09-12 09:48 UTC] cmb@php.net
> Though personally I wish <=> wasn't so rigid to begin with.

It is not. Actually, the documentation states[1]:

> An integer less than, equal to, or greater than zero when $a is
> respectively less than, equal to, or greater than $b. Available
> as of PHP 7.

So this is not a bug.

One might desire a sign() function, so that the result of <=>,
strcmp() etc. could easily be used in a switch, but that would be
a separate feature request, and can be implemented in user-land,
of course.

[1] <http://php.net/manual/en/language.operators.comparison.php>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC