Failed asserting that “double:0.999″ matches expected value “double:0.999″.

If you execute the following piece of code in a test using phpUnit….

$dRes = 400-399.001;
$this->assertEquals( 0.999, $dRes);

you will get…

“Failed asserting that <double:0.999> matches expected value <double:0.999>.”

That’s PHP…

The correct way would be “$this->assertEquals(0.999, round($dRes, 3));” as float handling in PHP is a bit strange. But I really love this Message - I start to think about what is reality and what not…

Comments

5 Responses to “Failed asserting that “double:0.999″ matches expected value “double:0.999″.”

  1. Marco Kaiser on August 15th, 2007 5:40 pm

    http://bugs.php.net/bug.php?id=2835
    this bug has same specific behavior and rasmus replied correct to this type of “php magic”. :)

  2. dodger on August 15th, 2007 6:08 pm

    Well you are right but this is not the point.

    If we start to think about that 1 !== 1 … something weird is going on …. ;)

  3. Sebastian Bergmann on August 16th, 2007 8:31 am

    Have a look at the signature of assertEquals():
    void assertEquals(float $expected, float $actual, ”, float $delta = 0)

    Just pass the $delta that defines the tolerance interval (equality within +/- $delta) of your choice and you’re done.

  4. dodger on August 16th, 2007 9:06 am

    Hoi Sebastian,

    you are right as always ;)

    But what delta should we have here ? :) I know it is not a phpUnit problem - simply strange.

  5. Frank Kleine on August 16th, 2007 9:18 pm

    Ever did a php -r “var_dump(serialize(3.13));”? :)

Leave a Reply