summaryrefslogtreecommitdiff
path: root/eqpct.go
diff options
context:
space:
mode:
Diffstat (limited to 'eqpct.go')
-rw-r--r--eqpct.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/eqpct.go b/eqpct.go
new file mode 100644
index 0000000..d830049
--- /dev/null
+++ b/eqpct.go
@@ -0,0 +1,41 @@
+package badtudexo
+
+import (
+ "errors"
+ "fmt"
+)
+
+// Given a list of base, percent and the result and a tolerance return a list
+// of indices where outside this tolerance pct. For instance, given a base of
+// [100, 50, 200], percents of [ 10, 10, 10 ], and a tolerance either way of
+// 1%, return all in result which are not between 9% and 11% of the
+// corresponding index of base: if res were [10, 2, 19], return [1], as the
+// 2nd (1st w/ zero-indexing) elem is the only one outside the allowed pcts.
+func EqPct(base, pct, res []float64, tol float64) ([]int, error) {
+ var ret []int
+ var allgood bool = true
+ if len(base) != len(pct) {
+ return nil, errors.New("len(base) != len(pct)")
+ }
+
+ if len(pct) != len(res) {
+ return nil, errors.New("len(pct) != len(res)")
+ }
+
+ for i := 0; i < len(pct); i++ {
+ min := base[i] * ((pct[i] - tol) / 100)
+ max := base[i] * ((pct[i] + tol) / 100)
+
+ if res[i] < min || res[i] > max {
+ // fmt.Printf("Min [%v] < Res [%v] > Max [%v]\n", min, res[i], max)
+ allgood = false
+ ret = append(ret, i)
+ }
+ }
+
+ if allgood {
+ // fmt.Println("EqPct: all good!")
+ }
+
+ return ret, nil
+}