Skip to content

Commit dbe6edd

Browse files
authored
Partially revert #240 (#241)
1 parent b0f70d9 commit dbe6edd

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/sass/value/color.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,21 +318,21 @@ def ==(other)
318318

319319
if legacy?
320320
return false unless other.legacy?
321-
return false unless FuzzyMath.equals(other.alpha_or_nil, alpha_or_nil)
321+
return false unless FuzzyMath.equals_nilable(other.alpha_or_nil, alpha_or_nil)
322322

323323
if _space == other._space
324-
FuzzyMath.equals(other.channel0_or_nil, channel0_or_nil) &&
325-
FuzzyMath.equals(other.channel1_or_nil, channel1_or_nil) &&
326-
FuzzyMath.equals(other.channel2_or_nil, channel2_or_nil)
324+
FuzzyMath.equals_nilable(other.channel0_or_nil, channel0_or_nil) &&
325+
FuzzyMath.equals_nilable(other.channel1_or_nil, channel1_or_nil) &&
326+
FuzzyMath.equals_nilable(other.channel2_or_nil, channel2_or_nil)
327327
else
328328
_to_space(Space::RGB) == other._to_space(Space::RGB)
329329
end
330330
else
331331
other._space == _space &&
332-
FuzzyMath.equals(other.channel0_or_nil, channel0_or_nil) &&
333-
FuzzyMath.equals(other.channel1_or_nil, channel1_or_nil) &&
334-
FuzzyMath.equals(other.channel2_or_nil, channel2_or_nil) &&
335-
FuzzyMath.equals(other.alpha_or_nil, alpha_or_nil)
332+
FuzzyMath.equals_nilable(other.channel0_or_nil, channel0_or_nil) &&
333+
FuzzyMath.equals_nilable(other.channel1_or_nil, channel1_or_nil) &&
334+
FuzzyMath.equals_nilable(other.channel2_or_nil, channel2_or_nil) &&
335+
FuzzyMath.equals_nilable(other.alpha_or_nil, alpha_or_nil)
336336
end
337337
end
338338

lib/sass/value/fuzzy_math.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ module Value
66
module FuzzyMath
77
PRECISION = 11
88

9+
EPSILON = 10**-PRECISION
10+
11+
INVERSE_EPSILON = 10**PRECISION
12+
913
module_function
1014

1115
def equals(number1, number2)
12-
number1 == number2 || number1.round(PRECISION) == number2.round(PRECISION)
16+
return true if number1 == number2
17+
18+
(number1 - number2).abs <= EPSILON &&
19+
(number1 * INVERSE_EPSILON).round ==
20+
(number2 * INVERSE_EPSILON).round
21+
end
22+
23+
def equals_nilable(number1, number2)
24+
return true if number1 == number2
25+
return false if number1.nil? || number2.nil?
26+
27+
(number1 - number2).abs <= EPSILON &&
28+
(number1 * INVERSE_EPSILON).round ==
29+
(number2 * INVERSE_EPSILON).round
1330
end
1431

1532
def less_than(number1, number2)
@@ -32,7 +49,7 @@ def integer?(number)
3249
return false unless number.finite?
3350
return true if number.integer?
3451

35-
number.round == number.round(PRECISION)
52+
(number.round - number.round(PRECISION)).abs < EPSILON
3653
end
3754

3855
def to_i(number)
@@ -59,7 +76,7 @@ def _clamp_like_css(number, lower_bound, upper_bound)
5976
end
6077

6178
def _round(number)
62-
number&.finite? ? number.round(PRECISION).to_f : number
79+
number&.finite? ? (number * INVERSE_EPSILON).round : number
6380
end
6481
end
6582

0 commit comments

Comments
 (0)