Em Fri, 27 May 2011 04:03:01 +0100, Philip Olson <phi...@roshambo.org>
escreveu:
Hello geeks,
A geek is needed to clarify PHP bug #45712. This is an edge case but the
test (bug45712.phpt) contains code similar to the following:
<?php
$inf = pow(0, -2);
var_dump($inf); // float(INF)
var_dump($inf == $inf); // bool(false)
var_dump($inf === $inf); // bool(true)
?>
That's how it's behaved since ~forever (AFAICT) and remains in
5.3.7-dev, but PHP 5.4.0-dev changes behavior so both now return true.
Is this is how we want it? And how should this be documented/explained?
What IEEE Std 754-2008 says is this:
«5.11 Details of comparison predicates
5.11.0
For every supported arithmetic format, it shall be possible to compare one
floating-point datum to another
in that format (see 5.6.1). Additionally, floating-point data
represented in different formats shall be
comparable as long as the operands’ formats have the same radix.
Four mutually exclusive relations are possible: less than, equal, greater
than, and unordered. The last case
arises when at least one operand is NaN. Every NaN shall compare unordered
with everything, including
itself. Comparisons shall ignore the sign of zero (so +0 = −0). Infinite
operands of the same sign shall
compare equal.
Languages define how the result of a comparison shall be delivered, in one
of two ways: either as a relation
identifying one of the four relations listed above, or as a true-false
response to a predicate that names the
specific comparison desired.
Table 5.1, Table 5.2, and Table 5.3 exhibit twenty-two functionally
distinct useful predicates and negations
with various ad-hoc and traditional names and symbols. Each predicate is
true if any of its indicated
relations is true. The relation “?” indicates an unordered relation. (...)
(...)
Table 5.1 - Required unordered-quiet predicate and negation
Unordered quiet predicate | Unordered-quiet Negation
-----------------------------------|---------------------------------------
True Relations | Names | True Relations | Names
---------------|-------------------|---------------------------------------
EQ | compareQuietEqual | LT GT UN | compareQuietNotEqual
| = | | ?<>, NOT(=), ≠
Given this, I think the behavior of 5.4 is correct because "[i]nfinite
operands of the same sign shall compare equal" and compareQuietEqual maps
EQ to true. 5.4 still gives false for NAN==NAN, which is also correct.
This is also what this C99 program gives:
#include<stdio.h>
#include<math.h>
void main() {
printf("%d %d\n", INFINITY == INFINITY, NAN == NAN);
}
./a.out
1 0
--
Gustavo Lopes
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php