Development

#4187 ([PATCH] lime_test::is() fails on comparing float values)

You must first sign up to be able to contribute.

Ticket #4187 (closed defect: fixed)

Opened 5 years ago

Last modified 5 years ago

[PATCH] lime_test::is() fails on comparing float values

Reported by: bschussek Assigned to: fabien
Priority: minor Milestone: 1.0.18
Component: lime Version: 1.1.0 RC2
Keywords: Cc:
Qualification: Unreviewed

Description

The method is() fails sometimes to compare float values correctly. Today I compared the result of a class method with a hardcoded float number. When vardump'ing, both numbers seemed to be equal in type and content, but the call of is() failed nevertheless.

The problem is described here: http://www.php.net/manual/en/language.operators.comparison.php#53462

The solution is to compare float values the same way as is done in other programming languages. Add a constant for epsilon to lime_test on line 19:

class lime_test
{
  const EPSILON = 0.0000000001;

and change the way of float comparisons in line 93:

  public function is($exp1, $exp2, $message = '')
  {
    if (is_object($exp1) || is_object($exp2))
    {
      $value = $exp1 == $exp2;
    }
    elseif (is_float($exp1) && is_float($exp2))
    {
      $value = abs($exp1 - $exp2) < self::EPSILON;
    }
    else

Change History

08/13/08 15:36:25 changed by fabien

  • status changed from new to closed.
  • resolution set to fixed.

(In [10846]) [lime] fixed is() method for floats (closes #4187 - patch from bschussek)

08/13/08 15:36:44 changed by fabien

  • milestone set to 1.0.18.