-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofloat_example_test.go
66 lines (52 loc) · 1.59 KB
/
gofloat_example_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
package gofloat_test
import (
"fmt"
"math"
"github.com/senpathi/gofloat"
)
func ExampleToFloat() {
// convert Pi and Sqrt(2) into Float with three precisions
pi := gofloat.ToFloat(math.Pi, 3)
sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
fmt.Println(pi.Float64(), sqrt2.Float64())
//Output: 3.142 1.414
}
func ExampleFloat_Add() {
// add Sqrt(2) to Pi and get results with 3 precisions
pi := gofloat.ToFloat(math.Pi, 3)
sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
f := pi.Add(sqrt2)
fmt.Println(`PI + Sqrt(2) =`, f.Float64())
//Output: PI + Sqrt(2) = 4.556
}
func ExampleFloat_Sub() {
// subtract Sqrt(2) from Pi and get result with 3 precision
pi := gofloat.ToFloat(math.Pi, 3)
sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
f := pi.Sub(sqrt2)
fmt.Println(`PI - Sqrt(2) =`, f.Float64())
//Output: PI - Sqrt(2) = 1.728
}
func ExampleFloat_Multiply() {
// multiply Pi by Sqrt(2) and get result with 3 precision
pi := gofloat.ToFloat(math.Pi, 3)
sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
f := pi.Multiply(sqrt2)
fmt.Println(`PI * Sqrt(2) =`, f.Float64())
//Output: PI * Sqrt(2) = 4.443
}
func ExampleFloat_Divide() {
// divide Pi by Sqrt(2) and get result with 3 precision
pi := gofloat.ToFloat(math.Pi, 3)
sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
f := pi.Divide(sqrt2)
fmt.Println(`PI / Sqrt(2) =`, f.Float64())
//Output: PI / Sqrt(2) = 2.222
}
func ExampleFloat_Float64() {
// convert Pi and Sqrt(2) into Float with three precisions and convert back to float64
pi := gofloat.ToFloat(math.Pi, 3)
sqrt2 := gofloat.ToFloat(math.Sqrt2, 3)
fmt.Println(pi.Float64(), sqrt2.Float64())
//Output: 3.142 1.414
}