13

I have a very basic MySQL query that reads rows from a database table and adds or subtracts the row value to a PHP string defined as $total_balance.

For example;

$statement_details_query = mysqli_query($con,"SELECT transaction_nominal_code, SUM(transaction_debit) as TotalDebit, SUM(transaction_credit) as TotalCredit FROM accounts_transaction GROUP BY transaction_nominal_code") or die(mysql_error());

while($statement_details = mysqli_fetch_array( $statement_details_query )) {

$balance = $statement_details['TotalCredit'] - $statement_details['TotalDebit'];

$total_balance = $total_balance + $balance;

}

echo number_format($total_balance, 2, '.', ',');

My question is, what is the difference between -0 and 0?

7
  • 1
    Zero can't be negative!!
    – kamal pal
    Jul 6, 2015 at 15:52
  • 10
    Yes it can. It is called signed zero, and it is an important part of floating point numbers.
    – Rob Foley
    Jul 6, 2015 at 15:54
  • The balance is always Debit minus Credit. Jul 6, 2015 at 15:57
  • how is related the code shown to the question asked? do you have an -0 echoed? Jul 6, 2015 at 16:45
  • Yes, I have a -0 echoed
    – Michael LB
    Jul 6, 2015 at 20:28

5 Answers 5

5

In PHP, there is no real difference:

Float:

php > $negZ = -0.0;
php > $posZ = +0.0;
php > var_dump($negZ == $posZ, $negZ === $posZ);
bool(true)
bool(true)

Int:

php > $negZ = -0;
php > $posZ = +0;
php > var_dump($negZ == $posZ, $negZ === $posZ);
bool(true)
bool(true)
4
  • 1
    Why 'no real'? Is there a difference or not? To me, it seems as if there is no difference at all. Jul 6, 2015 at 20:42
  • because yes, under IEEE 754, there are positive and negative zeroes. php glosses that over and just treats all zeroes as zero.
    – Marc B
    Jul 6, 2015 at 21:05
  • 1
    I know IEEE 754. My question was how PHP does make sure that -0 === +0 is true. Jul 7, 2015 at 5:55
  • 2
    Here's a real difference: as negative zero float was not known in PHP 5, you can get unexpected issues when migrating from 5 to 7. I just fixed a bug in some old jpgraph version (php graph library) where the precision in a format string was calculated to be "-0" which was subsequently used in a format string, which became "%01.-0f" which is invalid, and will result in the string "0f" always.
    – mvds
    Jun 6, 2018 at 10:49
2

I just asked myself the same question and thought 0 === -0 is true:

-1

true

How to reproduce:

<?php

$sum = 1.0000001;
$val = 1.00;

$a = round($val - $sum,2);
var_dump($a, $a === 0);

float(-0)
bool(false)

1
  • it's because round returns float, not int
    – hejdav
    Sep 6, 2022 at 19:18
2
php > echo bin2hex(pack("f", 0.0));
00000000
php > echo bin2hex(pack("f", +0.0));
00000000
php > echo bin2hex(pack("f", -0.0));
00000080

php > var_dump(unpack("f", "\x00\x00\x00\x00")[1]);
float(0)
php > var_dump(unpack("f", "\x00\x00\x00\x80")[1]);
float(-0)
1
  • 3
    I think you should summarize your answer, instead of forcing people to read the whole thing. Jul 14, 2022 at 6:54
1

Old question but stumbled upon an issue with this today.

if (is_numeric($denominator) && $denominator) {
    return 10 / $denominator;
}

If $denominator is negative zero this will result in a division by zero. So it looks like -0 == true which might be perfectly fine. Just not what I expected...

0

Yes, 0 == -0.

See my test below: http://viper-7.com/pa0xBU

1
  • 2
    -0 is treated as 0 in PHP. Problem actually relates floats
    – hejdav
    Sep 15, 2016 at 14:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.