tags:

views:

117

answers:

2

I am having trouble with a complex script which sometimes (About 2 or 3 times while calulating about 90'000 values), generates a '-0' and writes it into the database. I suspect it's a string (The values which are calulated can result in integers, floats or strings.)*

Is there any PHP calculation which might result in a '-0'?

* = Oh, how I miss strong typing sometimes...

+5  A: 

As Gurdas says, you can have your strong typing in the database. That aside, I don't know the answer to your question but I know how would I approach the problem.

The problem, as I understand it, is that you don't know in which cases you get the '-0', which is a valid floating point representation of 0, by the way. So you have to find in which cases you are getting that. I'd take one of two routes:

  1. Use Xdebug, raise an error in the database insertion code when the value is '-0' to get a stack_trace with arguments (use xdebug.collect_params=1)
  2. Create an empty string at the beginning of the script, populating it with all the operations and operands being done as they are, with the result and line. Afterwards, in the insertion clause add an if ($value == '-0') { print $string; }
Vinko Vrsalovic
+4  A: 

Rounding a negative number toward positive infinity, as ceil() does, can produce -0.

echo ceil(-.7);

// -0

The same result comes with, e.g., round(-.2).

Both of these will resolve to true:

(-0 == 0)
(ceil(-.7) == 0)

While these will resolve to true and false, respectively:

(-0 === 0)
(ceil(-.7) === 0)

Edit: An interesting (and implemented) rfc can be read here.

GZipp
Thanks, I think the culprit was the round()-method which is being applied to some of my values.
christian studer