Skip to content

Commit fa56c9b

Browse files
committed
remove float32 variant, relocate into separate file
This change removes the float32 variant of the AlmostEqual funcs, that we will likely never use. This change also relocates the function into a separate file to avoid modifying a file that's a fork of another vendored package. Signed-off-by: Seth Bunce <[email protected]>
1 parent e672c58 commit fa56c9b

File tree

2 files changed

+60
-92
lines changed

2 files changed

+60
-92
lines changed

prometheus/internal/almost_equal.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2015 Björn Rabenstein
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
//
21+
// The code in this package is copy/paste to avoid a dependency. Hence this file
22+
// carries the copyright of the original repo.
23+
// https://github.com/beorn7/floats
24+
package internal
25+
26+
import (
27+
"math"
28+
)
29+
30+
// minNormalFloat64 is the smallest positive normal value of type float64.
31+
var minNormalFloat64 = math.Float64frombits(0x0010000000000000)
32+
33+
// AlmostEqualFloat64 returns true if a and b are equal within a relative error
34+
// of epsilon. See http://floating-point-gui.de/errors/comparison/ for the
35+
// details of the applied method.
36+
func AlmostEqualFloat64(a, b, epsilon float64) bool {
37+
if a == b {
38+
return true
39+
}
40+
absA := math.Abs(a)
41+
absB := math.Abs(b)
42+
diff := math.Abs(a - b)
43+
if a == 0 || b == 0 || absA+absB < minNormalFloat64 {
44+
return diff < epsilon*minNormalFloat64
45+
}
46+
return diff/math.Min(absA+absB, math.MaxFloat64) < epsilon
47+
}
48+
49+
// AlmostEqualFloat64s is the slice form of AlmostEqualFloat64.
50+
func AlmostEqualFloat64s(a, b []float64, epsilon float64) bool {
51+
if len(a) != len(b) {
52+
return false
53+
}
54+
for i := range a {
55+
if !AlmostEqualFloat64(a[i], b[i], epsilon) {
56+
return false
57+
}
58+
}
59+
return true
60+
}

prometheus/internal/difflib.go

-92
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"bytes"
2323
"fmt"
2424
"io"
25-
"math"
2625
"strings"
2726
)
2827

@@ -47,97 +46,6 @@ func calculateRatio(matches, length int) float64 {
4746
return 1.0
4847
}
4948

50-
var (
51-
// minNormalFloat64 is the smallest positive normal value of type float64.
52-
minNormalFloat64 = math.Float64frombits(0x0010000000000000)
53-
54-
// minNormalFloat32 is the smallest positive normal value of type float32.
55-
minNormalFloat32 = math.Float32frombits(0x00800000)
56-
)
57-
58-
// AlmostEqualFloat64 returns true if a and b are equal within a relative error
59-
// of epsilon. See http://floating-point-gui.de/errors/comparison/ for the
60-
// details of the applied method.
61-
//
62-
// This function is copy/paste to avoid a dependency.
63-
// https://github.com/beorn7/floats
64-
func AlmostEqualFloat64(a, b, epsilon float64) bool {
65-
if a == b {
66-
return true
67-
}
68-
absA := math.Abs(a)
69-
absB := math.Abs(b)
70-
diff := math.Abs(a - b)
71-
if a == 0 || b == 0 || absA+absB < minNormalFloat64 {
72-
return diff < epsilon*minNormalFloat64
73-
}
74-
return diff/math.Min(absA+absB, math.MaxFloat64) < epsilon
75-
}
76-
77-
// AlmostEqualFloat64s is the slice form of AlmostEqualFloat64.
78-
func AlmostEqualFloat64s(a, b []float64, epsilon float64) bool {
79-
if len(a) != len(b) {
80-
return false
81-
}
82-
for i := range a {
83-
if !AlmostEqualFloat64(a[i], b[i], epsilon) {
84-
return false
85-
}
86-
}
87-
return true
88-
}
89-
90-
// AlmostEqualFloat32 returns true if a and b are equal within a relative error
91-
// of epsilon. See http://floating-point-gui.de/errors/comparison/ for the
92-
// details of the applied method.
93-
//
94-
// This function is copy/paste to avoid a dependency.
95-
// https://github.com/beorn7/floats
96-
func AlmostEqualFloat32(a, b, epsilon float32) bool {
97-
if a == b {
98-
return true
99-
}
100-
absA := AbsFloat32(a)
101-
absB := AbsFloat32(b)
102-
diff := AbsFloat32(a - b)
103-
if a == 0 || b == 0 || absA+absB < minNormalFloat32 {
104-
return diff < epsilon*minNormalFloat32
105-
}
106-
return diff/MinFloat32(absA+absB, math.MaxFloat32) < epsilon
107-
}
108-
109-
// AlmostEqualFloat32s is the slice form of AlmostEqualFloat32.
110-
func AlmostEqualFloat32s(a, b []float32, epsilon float32) bool {
111-
if len(a) != len(b) {
112-
return false
113-
}
114-
for i := range a {
115-
if !AlmostEqualFloat32(a[i], b[i], epsilon) {
116-
return false
117-
}
118-
}
119-
return true
120-
}
121-
122-
// AbsFloat32 works like math.Abs, but for float32.
123-
func AbsFloat32(x float32) float32 {
124-
switch {
125-
case x < 0:
126-
return -x
127-
case x == 0:
128-
return 0 // return correctly abs(-0)
129-
}
130-
return x
131-
}
132-
133-
// MinFloat32 works like math.Min, but for float32.
134-
func MinFloat32(x, y float32) float32 {
135-
if x < y {
136-
return x
137-
}
138-
return y
139-
}
140-
14149
type Match struct {
14250
A int
14351
B int

0 commit comments

Comments
 (0)