# Testing program

**URL:** <https://forum.golangbridge.org/t/testing-program/23870>\
**Category:** Getting Help\
**Created:** [June 19, 2021, 6:57am UTC](https://forum.golangbridge.org/t/testing-program/23870 "2021-06-19T06:57:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruslan](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/r/2bfe46/32.png) [@ruslan](https://forum.golangbridge.org/u/ruslan)\
**Post date:** [June 19, 2021, 6:57am UTC](https://forum.golangbridge.org/t/testing-program/23870/1 "2021-06-19T06:57:11Z")

</div>

```
func TestAvg2(t *testing.T) {
	type test struct {
		data []float64
		answer float64
	}

	tests := []test{
		test{[]float64{2, 2}, 2.00},
		test{[]float64{4, 4}, 4.00},
		test{[]float64{3, 3, 4}, 3.33},
		test{[]float64{3, 3, 5}, 3.67},
		test{[]float64{5, 5, 5}, 5.00},
		test{[]float64{-10, 10, 10, 11}, 5.25},
	}

	for _, v := range tests {
		x := Avg(v.data...)
		str := fmt.Sprintf("%.2f", x)
		fl, _ := strconv.ParseFloat(str, 64)
		if x != v.answer {
			t.Error("Expected", v.answer, "got", fl)
		}
	}
}

```

Results:  
table\_test.go:29: Expected 3.33 got 3.33  
table\_test.go:29: Expected 3.67 got 3.67

What’s wrong?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [June 19, 2021, 10:19am UTC](https://forum.golangbridge.org/t/testing-program/23870/2 "2021-06-19T10:19:37Z")

</div>

Rounding errors. Thats what floats are known for.

For floats one usually checks if the absolute of the difference between two values is lesser than or equal a certain threshold, and considers them equal if the difference is small enough.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [September 17, 2021, 10:19am UTC](https://forum.golangbridge.org/t/testing-program/23870/3 "2021-09-17T10:19:41Z")

</div>

Closed on September 17, 2021, 10:19am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
