DoubleComparator.php (2103B)
1 <?php 2 /* 3 * This file is part of the Comparator package. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 namespace SebastianBergmann\Comparator; 12 13 /** 14 * Compares doubles for equality. 15 */ 16 class DoubleComparator extends NumericComparator 17 { 18 /** 19 * Smallest value available in PHP. 20 * 21 * @var float 22 */ 23 const EPSILON = 0.0000000001; 24 25 /** 26 * Returns whether the comparator can compare two values. 27 * 28 * @param mixed $expected The first value to compare 29 * @param mixed $actual The second value to compare 30 * @return bool 31 */ 32 public function accepts($expected, $actual) 33 { 34 return (is_double($expected) || is_double($actual)) && is_numeric($expected) && is_numeric($actual); 35 } 36 37 /** 38 * Asserts that two values are equal. 39 * 40 * @param mixed $expected The first value to compare 41 * @param mixed $actual The second value to compare 42 * @param float $delta The allowed numerical distance between two values to 43 * consider them equal 44 * @param bool $canonicalize If set to TRUE, arrays are sorted before 45 * comparison 46 * @param bool $ignoreCase If set to TRUE, upper- and lowercasing is 47 * ignored when comparing string values 48 * @throws ComparisonFailure Thrown when the comparison 49 * fails. Contains information about the 50 * specific errors that lead to the failure. 51 */ 52 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false) 53 { 54 if ($delta == 0) { 55 $delta = self::EPSILON; 56 } 57 58 parent::assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase); 59 } 60 }