-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuncertainty_test.go
89 lines (73 loc) · 2.35 KB
/
uncertainty_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package uncertainty
import (
"fmt"
"math"
"testing"
)
func TestUFloat_Add(t *testing.T) {
uf1 := UFloat{Value: 10, Uncertainty: 1}
uf2 := UFloat{Value: 5, Uncertainty: 0.5}
expected := UFloat{Value: 15, Uncertainty: math.Sqrt(math.Pow(1, 2) + math.Pow(0.5, 2))}
result := uf1.Add(uf2)
if result != expected {
t.Errorf("Addition test failed: expected %v, got %v", expected, result)
}
}
func TestUFloat_Subtract(t *testing.T) {
uf1 := UFloat{Value: 10, Uncertainty: 1}
uf2 := UFloat{Value: 5, Uncertainty: 0.5}
expected := UFloat{Value: 5, Uncertainty: math.Sqrt(math.Pow(1, 2) + math.Pow(0.5, 2))}
result := uf1.Subtract(uf2)
if result != expected {
t.Errorf("Subtraction test failed: expected %v, got %v", expected, result)
}
}
func TestMultiplyByFloat(t *testing.T) {
f1 := 2.00
uf2 := UFloat{Value: 3, Uncertainty: 0.2}
expected := UFloat{Value: 6, Uncertainty: 2 * 0.2}
result := uf2.MultiplyByFloat(f1)
if result != expected {
t.Errorf("Scalar multiplication test failed: expected %v, got %v", expected, result)
}
}
func TestUFloat_Multiply(t *testing.T) {
uf1 := UFloat{Value: 2, Uncertainty: 0.1}
uf2 := UFloat{Value: 3, Uncertainty: 0.2}
expected := UFloat{Value: 6, Uncertainty: math.Sqrt(math.Pow(2*0.2, 2) + math.Pow(3*0.1, 2))}
result := uf1.Multiply(uf2)
if result != expected {
t.Errorf("Multiplication test failed: expected %v, got %v", expected, result)
}
}
func TestUFloat_Divide(t *testing.T) {
uf1 := UFloat{Value: 10, Uncertainty: 0.5}
uf2 := UFloat{Value: 2, Uncertainty: 0.1}
expected := UFloat{Value: 5, Uncertainty: math.Sqrt(math.Pow(0.5/2, 2) + math.Pow(0.1*10/(math.Pow(2, 2)), 2))}
result := uf1.Divide(uf2)
if result != expected {
t.Errorf("Division test failed: expected %v, got %v", expected, result)
}
}
func foo(f1 UFloat, f2 UFloat) UFloat {
return f1.Multiply(f2) // f1 * f2
}
func bar(f1 UFloat, f2 UFloat) UFloat {
uf := f1.Divide(f2)
return uf.MultiplyByFloat(4) // 4 * (f1 / f2)
}
func ExampleUncertainty() {
// func foo(f1 UFloat, f2 UFloat) UFloat {
// return f1.Multiply(f2) // f1 * f2
// }
//
// func bar(f1 UFloat, f2 UFloat) UFloat {
// uf := f1.Divide(f2)
// return uf.MultiplyByFloat(4) // 4 * (f1 / f2)
// }
// UFloat type is {value, standard deviation}
a := UFloat{Value: 1.2, Uncertainty: 0.3}
b := UFloat{Value: 4.5, Uncertainty: 0.6}
fmt.Println(foo(a, b))
fmt.Println(bar(a, b))
}