0

Found an very silly error with PHP 7 and checked internet for some solution, but didn't find any good answer. Below code snippet fails in PHP 7.

Code Snippet:

print_r(
0*-1.1
);


print_r(
    floatval(
    0*-1.1)
);

Both Fails

Desired Output: 0 Real Output: "-0"

Any help is appreciated, is it a PHP bug?

Wolverine
  • 455
  • 3
  • 8
  • 26
  • whoever marked this as duplicate is pointing in wrong direction... this is PHP not JavaScript issue... – Wolverine Sep 18 '20 at 21:29
  • I didn't knew subject was fully getting capsed.. thank you! Initially I changed content then realized it was the subject itself... my bad... anyway can anyone help me understand why we get "-0" even when we have floatval checked? – Wolverine Sep 18 '20 at 21:40
  • because that's how the floating point spec works according to the IEEE standard that most programming languages implement. Please see the marked duplicate and, if you want even more detail, read the spec itself. – ChrisGPT was on strike Sep 19 '20 at 11:36
  • @Chris anything multiplied by zero should be zero and I think when we say this is IEEE standards, actually this is a bug! Again that is my thought! Zero is not positive nor negative!! – Wolverine Sep 21 '20 at 12:49
  • This is dealt with _in the standard_. Again, please read the marked duplicate and, if you want more detail, the full spec. Or just [search for some answers](https://duckduckgo.com/?q=ieee+floating+point+why+negative+zero). There are plenty of resources out there about this, and SO isn't about replacing official specifications and standards. – ChrisGPT was on strike Sep 21 '20 at 12:51
  • [For example](https://en.wikipedia.org/wiki/Signed_zero): "Negatively signed zero echoes the mathematical analysis concept of approaching 0 from below as a one-sided limit, which may be denoted by x → 0−, x → 0−, or x → ↑0. The notation "−0" may be used informally to denote a small negative number that has been rounded to zero. The concept of negative zero also has some theoretical applications in statistical mechanics and other disciplines." – ChrisGPT was on strike Sep 21 '20 at 12:52
  • @Chris thanks for the clarification... looks like -0 is still considered as 0 only. – Wolverine Sep 21 '20 at 13:02

0 Answers0