-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
166 lines (131 loc) · 2.94 KB
/
example_test.go
File metadata and controls
166 lines (131 loc) · 2.94 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sqrt_test
import (
"fmt"
"github.com/keep94/sqrt"
)
func ExampleSqrt() {
// Print the square root of 13 with 100 significant digits.
fmt.Printf("%.100g\n", sqrt.Sqrt(13))
// Output:
// 3.605551275463989293119221267470495946251296573845246212710453056227166948293010445204619082018490717
}
func ExampleCubeRoot() {
// Print the cube root of 3 with 100 significant digits.
fmt.Printf("%.100g\n", sqrt.CubeRoot(3))
// Output:
// 1.442249570307408382321638310780109588391869253499350577546416194541687596829997339854755479705645256
}
func ExampleNewNumberForTesting() {
// n = 10.2003400340034...
n, _ := sqrt.NewNumberForTesting([]int{1, 0, 2}, []int{0, 0, 3, 4}, 2)
fmt.Println(n)
// Output:
// 10.20034003400340
}
func ExampleNewFiniteNumber() {
// n = 563.5
n, _ := sqrt.NewFiniteNumber([]int{5, 6, 3, 5}, 3)
fmt.Println(n.Exact())
// Output:
// 563.5
}
func ExampleAsString() {
// sqrt(3) = 0.1732050807... * 10^1
n := sqrt.Sqrt(3)
fmt.Println(sqrt.AsString(n.WithStart(2).WithEnd(10)))
// Output:
// 32050807
}
func ExampleFiniteNumber_Exponent() {
// sqrt(50176) = 0.224 * 10^3
n := sqrt.Sqrt(50176)
fmt.Println(n.Exponent())
// Output:
// 3
}
func ExampleFiniteNumber_All() {
// sqrt(7) = 0.26457513110... * 10^1
n := sqrt.Sqrt(7)
for index, value := range n.All() {
fmt.Println(index, value)
if index == 5 {
break
}
}
// Output:
// 0 2
// 1 6
// 2 4
// 3 5
// 4 7
// 5 5
}
func ExampleFiniteNumber_Values() {
// sqrt(7) = 0.26457513110... * 10^1
n := sqrt.Sqrt(7)
for value := range n.WithEnd(6).Values() {
fmt.Println(value)
}
// Output:
// 2
// 6
// 4
// 5
// 7
// 5
}
func ExampleFiniteNumber_Backward() {
// sqrt(7) = 0.26457513110... * 10^1
n := sqrt.Sqrt(7).WithSignificant(6)
for index, value := range n.Backward() {
fmt.Println(index, value)
}
// Output:
// 5 5
// 4 7
// 3 5
// 2 4
// 1 6
// 0 2
}
func ExampleFiniteNumber_Exact() {
n := sqrt.Sqrt(2).WithSignificant(60)
fmt.Println(n.Exact())
// Output:
// 1.41421356237309504880168872420969807856967187537694807317667
}
func ExampleFiniteNumber_At() {
// sqrt(7) = 0.264575131106459...*10^1
n := sqrt.Sqrt(7)
fmt.Println(n.At(0))
fmt.Println(n.At(1))
fmt.Println(n.At(2))
// Output:
// 2
// 6
// 4
}
func ExampleFiniteNumber_NumComputed() {
n := sqrt.Sqrt(5)
// Initially Number computes zero digits.
fmt.Println(n.NumComputed())
// Number can compute extra digits beyond what is needed.
fmt.Printf("%.70g\n", n)
firstCount := n.NumComputed()
fmt.Println(firstCount >= 70)
// Since Number already computed the digit at position 50, no extra
// computation is needed.
fmt.Println(n.At(50)) // 51st digit
fmt.Println(n.NumComputed() == firstCount)
// Extra computation may be needed here.
fmt.Println(n.At(199)) // 200th digit
fmt.Println(n.NumComputed() >= 200)
// Output:
// 0
// 2.236067977499789696409173668731276235440618359611525724270897245410520
// true
// 2
// true
// 7
// true
}