inexact_equality_warning 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/inexact_equality_warning.gemspec +1 -1
- data/lib/inexact_equality_warning.rb +1 -1
- data/spec/inexact_equality_warning_spec.rb +45 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea8cdeed34206e0912c4358b86f589cb8e7ceb6a
|
4
|
+
data.tar.gz: bc341733ed61489d9306ae8edbd1578be2e1635c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 486390cded551301cfd5ef1ef5aec45169a1dbc3849da13a28c0a0a11d5d4383077847a3e76439f30de15b0d29fae34273106f4c016949c96232a0ee5c901620
|
7
|
+
data.tar.gz: cb39f55449f5b519afa88f6abd43002a8d94a64593055abf7972a913ceae011b7210b2f91100418b1d759f2e4f61ac6f0e6045fb4b6fdf1ea8f080d18ae78bd6
|
data/README.md
CHANGED
@@ -4,3 +4,16 @@ x == 0.0
|
|
4
4
|
# WARNING: Testing for equality between inexact floats is ill-advised, when comparing -2.7755575615628914e-17 and 0 (/path/to/file.rb:123)
|
5
5
|
# => false
|
6
6
|
```
|
7
|
+
|
8
|
+
|
9
|
+
### Installation and usage
|
10
|
+
|
11
|
+
Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem "inexact_equality_warning"
|
15
|
+
```
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require "inexact_equality_warning"
|
19
|
+
```
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# it would be good to use #inexact? here -- https://bugs.ruby-lang.org/issues/5321
|
2
2
|
module EqualityWarning
|
3
3
|
def ==(other)
|
4
|
-
warn "WARNING: Testing for equality between inexact floats is ill-advised, when comparing #{self} and #{other} (#{caller.first})" if self.is_a?(Float) || other.is_a?(Float)
|
4
|
+
warn "WARNING: Testing for equality between inexact floats is ill-advised, when comparing #{self} and #{other} (#{caller.first})" if (self.is_a?(Numeric) && other.is_a?(Numeric)) && self.is_a?(Float) || other.is_a?(Float)
|
5
5
|
super
|
6
6
|
end
|
7
7
|
end
|
@@ -19,23 +19,41 @@ RSpec.describe EqualityWarning do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
[
|
22
|
-
1.0, 1.0,
|
23
|
-
1.0, 1.0,
|
24
22
|
1.0, 1,
|
23
|
+
1.0, 2**1000,
|
24
|
+
1.0, Rational(1, 2),
|
25
|
+
1.0, Complex(1, 2),
|
25
26
|
1, 1.0,
|
26
|
-
1
|
27
|
-
1.0,
|
28
|
-
1
|
27
|
+
Rational(1, 2), 1.0,
|
28
|
+
2**1000, 1.0,
|
29
|
+
Complex(1, 2), 1.0,
|
29
30
|
].each_slice(2) do |x, y|
|
30
31
|
specify { expect_warning(x, y) }
|
31
32
|
end
|
32
33
|
|
33
34
|
[
|
35
|
+
1, 2**1000,
|
36
|
+
1, Rational(1, 2),
|
37
|
+
1, Complex(1, 2),
|
38
|
+
1, "anything",
|
39
|
+
2**1000, 1,
|
40
|
+
2**1000, Rational(1, 2),
|
41
|
+
2**1000, Complex(1, 2),
|
42
|
+
2**1000, "anything",
|
43
|
+
Rational(1, 2), 1,
|
44
|
+
Rational(1, 2), 2**1000,
|
45
|
+
Rational(1, 2), Complex(1, 2),
|
46
|
+
Rational(1, 2), "anything",
|
47
|
+
Complex(1, 2), 1,
|
48
|
+
Complex(1, 2), 2**1000,
|
49
|
+
Complex(1, 2), Rational(1, 2),
|
50
|
+
Complex(1, 2), "anything",
|
51
|
+
"anything", 1,
|
52
|
+
"anything", 2**1000,
|
53
|
+
"anything", Rational(1, 2),
|
54
|
+
"anything", Complex(1, 2),
|
55
|
+
1.0, "anything",
|
34
56
|
"anything", 1.0,
|
35
|
-
1, 1,
|
36
|
-
Complex(1, 0), Complex(0, 2),
|
37
|
-
"anything", "anything",
|
38
|
-
2**1000, 2**1000, # Bignum
|
39
57
|
].each_slice(2) do |x, y|
|
40
58
|
specify { expect_no_warning(x, y) }
|
41
59
|
end
|
@@ -50,3 +68,21 @@ RSpec.describe EqualityWarning do
|
|
50
68
|
expect(numeric_classes.reject { |klass| numeric_classes.any? { |other_class| other_class < klass } }).to eq(prepended_classes)
|
51
69
|
end
|
52
70
|
end
|
71
|
+
|
72
|
+
=begin
|
73
|
+
# test case permutations generation:
|
74
|
+
|
75
|
+
test_values = {
|
76
|
+
Fixnum => "1",
|
77
|
+
Float => "1.0",
|
78
|
+
Bignum => "2**1000",
|
79
|
+
Rational => "Rational(1, 2)",
|
80
|
+
Complex => "Complex(1, 2)",
|
81
|
+
String => %~"anything"~
|
82
|
+
}
|
83
|
+
[Float, Fixnum, Bignum, Rational, Complex, String].permutation(2).to_a.map do |x, y|
|
84
|
+
[test_values[x], test_values[y]]
|
85
|
+
end.each do |x, y|
|
86
|
+
puts "#{x}, #{y},"
|
87
|
+
end
|
88
|
+
=end
|