facebook / hhvm

A virtual machine for executing programs written in Hack.
https://hhvm.com
Other
18.2k stars 3k forks source link

-0.0 doesn't produce negative zero #7426

Open jesseschalken opened 8 years ago

jesseschalken commented 8 years ago

HHVM Version

HipHop VM 3.16.0-dev (rel)
Compiler: heads/master-0-ge9eb3fc414fbae9b2c999a4f11390e23050b23b7
Repo schema: 9cc0ebe15bc9d2ba3609c5e4458deeef9ed86dcc

Standalone code, or other way to reproduce the problem

<?php

print 1.0 / -0.0;

Expected result

Warning: Division by zero in /in/AnbFm on line 3
-INF

Actual result

Warning: Division by zero in /in/AnbFm on line 3
INF

See https://3v4l.org/AnbFm. This is a copy of PHP bug https://bugs.php.net/bug.php?id=52355, which was fixed in 7.0.2. See also #7425.

lexidor commented 4 years ago

Updated repro, since devision by zero throws a DivisionByZeroException now.

\var_dump(-0.0); (PHP float(-0)), (HHVM float(0)).

Is this related to 4.9.0?

_The hhvm.forcehh INI setting now defaults to true; as all files are now handled as Hack files, this only controls some smaller behaviors which are not to PHP vs Hack: ... the string of -0.0 is now "0" instead of "-0"

jesseschalken commented 4 years ago

@lexidor There's two different issues:

  1. Whether negative zero produces "-0" when converted to a string. That is issue #7425.
  2. Whether the code -0.0 results in a negative zero, like -1.0 * 0.0 does. That is this issue.

It is unclear which of these (or both) the release notes of 4.9.0 are referring to.

In any case, I only opened this issue for the sake of PHP compatibility, and since that has not been a goal for some time it is up to the Hack team what the correct behavior is here.

lexidor commented 4 years ago

@jesseschalken I don't know if Hack code has any way of observing the difference between -0.0 and 0.0 anymore now the toString behavior has changed. It would be great is this was transparent to Hack code.

jesseschalken commented 4 years ago

@jesseschalken I don't know if Hack code has any way of observing the difference between -0.0 and 0.0 anymore now the toString behavior has changed.

I don't have a HHVM interpreter in front of me but you could try:

print \bin2hex(\pack("d", 0.0)) . "\n";
print \bin2hex(\pack("d", -0.0)) . "\n";
print \bin2hex(\pack("d", -1.0 * 0.0)) . "\n";

https://3v4l.org/lh87d

lexidor commented 4 years ago
<<__EntryPoint>>
function main(): void {
  print \bin2hex(\pack("d", 0.0))."\n"; // 0000000000000000
  print \bin2hex(\pack("d", -0.0))."\n"; // 0000000000000000
  print \bin2hex(\pack("d", -1.0 * 0.0))."\n"; // 0000000000000080
}

HHVM appears to outsmart you when you ask for -0.0 and gives you 0.0 instead. -1.0 * 0.0 gives -0.0.

lexidor commented 4 years ago

Thanks for your assistance, I'll pass this on to someone who works on the parser, because this is unexpected to say the least.