Skip to content

Commit 606595b

Browse files
committed
Merge branch 'MAGETWO-90968' into MPI-PR
2 parents 1c3a7f2 + 1bd038e commit 606595b

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Math;
9+
10+
/**
11+
* Contains methods to compare float digits.
12+
*
13+
* @api
14+
*/
15+
class FloatComparator
16+
{
17+
/**
18+
* Precision for floats comparing.
19+
*
20+
* @var float
21+
*/
22+
private static $epsilon = 0.00001;
23+
24+
/**
25+
* Compares two float digits.
26+
*
27+
* @param float $a
28+
* @param float $b
29+
* @return bool
30+
*/
31+
public function equal(float $a, float $b): bool
32+
{
33+
return abs($a - $b) <= self::$epsilon;
34+
}
35+
36+
/**
37+
* Compares if the first argument greater than the second argument.
38+
*
39+
* @param float $a
40+
* @param float $b
41+
* @return bool
42+
*/
43+
public function greaterThan(float $a, float $b): bool
44+
{
45+
return ($a - $b) > self::$epsilon;
46+
}
47+
48+
/**
49+
* Compares if the first argument greater or equal to the second.
50+
*
51+
* @param float $a
52+
* @param float $b
53+
* @return bool
54+
*/
55+
public function greaterThanOrEqual(float $a, float $b): bool
56+
{
57+
return $this->equal($a, $b) || $this->greaterThan($a, $b);
58+
}
59+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Math\Test\Unit;
9+
10+
use Magento\Framework\Math\FloatComparator;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class FloatComparatorTest extends TestCase
14+
{
15+
/**
16+
* @var FloatComparator
17+
*/
18+
private $comparator;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function setUp()
24+
{
25+
$this->comparator = new FloatComparator();
26+
}
27+
28+
/**
29+
* Checks a case when `a` and `b` are equal.
30+
*
31+
* @param float $a
32+
* @param float $b
33+
* @param bool $expected
34+
* @dataProvider eqDataProvider
35+
*/
36+
public function testEq(float $a, float $b, bool $expected)
37+
{
38+
self::assertEquals($expected, $this->comparator->equal($a, $b));
39+
}
40+
41+
/**
42+
* Gets list of variations to compare equal float.
43+
*
44+
* @return array
45+
*/
46+
public function eqDataProvider(): array
47+
{
48+
return [
49+
[10, 10.00001, true],
50+
[10, 10.000001, true],
51+
[10.0000099, 10.00001, true],
52+
[1, 1.0001, false],
53+
[1, -1.00001, false],
54+
];
55+
}
56+
57+
/**
58+
* Checks a case when `a` > `b`.
59+
*
60+
* @param float $a
61+
* @param float $b
62+
* @param bool $expected
63+
* @dataProvider gtDataProvider
64+
*/
65+
public function testGt(float $a, float $b, bool $expected)
66+
{
67+
self::assertEquals($expected, $this->comparator->greaterThan($a, $b));
68+
}
69+
70+
/**
71+
* Gets list of variations to compare if `a` > `b`.
72+
*
73+
* @return array
74+
*/
75+
public function gtDataProvider(): array
76+
{
77+
return [
78+
[10, 10.00001, false],
79+
[10, 10.000001, false],
80+
[10.0000099, 10.00001, false],
81+
[1.0001, 1, true],
82+
[1, -1.00001, true],
83+
];
84+
}
85+
86+
/**
87+
* Checks a case when `a` >= `b`.
88+
*
89+
* @param float $a
90+
* @param float $b
91+
* @param bool $expected
92+
* @dataProvider gteDataProvider
93+
*/
94+
public function testGte(float $a, float $b, bool $expected)
95+
{
96+
self::assertEquals($expected, $this->comparator->greaterThanOrEqual($a, $b));
97+
}
98+
99+
/**
100+
* Gets list of variations to compare if `a` >= `b`.
101+
*
102+
* @return array
103+
*/
104+
public function gteDataProvider(): array
105+
{
106+
return [
107+
[10, 10.00001, true],
108+
[10, 10.000001, true],
109+
[10.0000099, 10.00001, true],
110+
[1.0001, 1, true],
111+
[1, -1.00001, true],
112+
[1.0001, 1.001, false],
113+
];
114+
}
115+
}

0 commit comments

Comments
 (0)