Debian Codegraph @

libgo/go/math/example_test.go

raw
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package math_test
6
7 import (
8 "fmt"
9 "math"
10 )
11
12 func ExampleAcos() {
13 fmt.Printf("%.2f", math.Acos(1))
14 // Output: 0.00
15 }
16
17 func ExampleAcosh() {
18 fmt.Printf("%.2f", math.Acosh(1))
19 // Output: 0.00
20 }
21
22 func ExampleAsin() {
23 fmt.Printf("%.2f", math.Asin(0))
24 // Output: 0.00
25 }
26
27 func ExampleAsinh() {
28 fmt.Printf("%.2f", math.Asinh(0))
29 // Output: 0.00
30 }
31
32 func ExampleAtan() {
33 fmt.Printf("%.2f", math.Atan(0))
34 // Output: 0.00
35 }
36
37 func ExampleAtan2() {
38 fmt.Printf("%.2f", math.Atan2(0, 0))
39 // Output: 0.00
40 }
41
42 func ExampleAtanh() {
43 fmt.Printf("%.2f", math.Atanh(0))
44 // Output: 0.00
45 }
46
47 func ExampleCopysign() {
48 fmt.Printf("%.2f", math.Copysign(3.2, -1))
49 // Output: -3.20
50 }
51
52 func ExampleCos() {
53 fmt.Printf("%.2f", math.Cos(math.Pi/2))
54 // Output: 0.00
55 }
56
57 func ExampleCosh() {
58 fmt.Printf("%.2f", math.Cosh(0))
59 // Output: 1.00
60 }
61
62 func ExampleSin() {
63 fmt.Printf("%.2f", math.Sin(math.Pi))
64 // Output: 0.00
65 }
66
67 func ExampleSincos() {
68 sin, cos := math.Sincos(0)
69 fmt.Printf("%.2f, %.2f", sin, cos)
70 // Output: 0.00, 1.00
71 }
72
73 func ExampleSinh() {
74 fmt.Printf("%.2f", math.Sinh(0))
75 // Output: 0.00
76 }
77
78 func ExampleTan() {
79 fmt.Printf("%.2f", math.Tan(0))
80 // Output: 0.00
81 }
82
83 func ExampleTanh() {
84 fmt.Printf("%.2f", math.Tanh(0))
85 // Output: 0.00
86 }
87
88 func ExampleSqrt() {
89 const (
90 a = 3
91 b = 4
92 )
93 c := math.Sqrt(a*a + b*b)
94 fmt.Printf("%.1f", c)
95 // Output: 5.0
96 }
97
98 func ExampleCeil() {
99 c := math.Ceil(1.49)
100 fmt.Printf("%.1f", c)
101 // Output: 2.0
102 }
103
104 func ExampleFloor() {
105 c := math.Floor(1.51)
106 fmt.Printf("%.1f", c)
107 // Output: 1.0
108 }
109
110 func ExamplePow() {
111 c := math.Pow(2, 3)
112 fmt.Printf("%.1f", c)
113 // Output: 8.0
114 }
115
116 func ExamplePow10() {
117 c := math.Pow10(2)
118 fmt.Printf("%.1f", c)
119 // Output: 100.0
120 }
121
122 func ExampleRound() {
123 p := math.Round(10.5)
124 fmt.Printf("%.1f\n", p)
125
126 n := math.Round(-10.5)
127 fmt.Printf("%.1f\n", n)
128 // Output:
129 // 11.0
130 // -11.0
131 }
132
133 func ExampleRoundToEven() {
134 u := math.RoundToEven(11.5)
135 fmt.Printf("%.1f\n", u)
136
137 d := math.RoundToEven(12.5)
138 fmt.Printf("%.1f\n", d)
139 // Output:
140 // 12.0
141 // 12.0
142 }
143
144 func ExampleLog() {
145 x := math.Log(1)
146 fmt.Printf("%.1f\n", x)
147
148 y := math.Log(2.7183)
149 fmt.Printf("%.1f\n", y)
150 // Output:
151 // 0.0
152 // 1.0
153 }
154
155 func ExampleLog2() {
156 fmt.Printf("%.1f", math.Log2(256))
157 // Output: 8.0
158 }
159
160 func ExampleLog10() {
161 fmt.Printf("%.1f", math.Log10(100))
162 // Output: 2.0
163 }
164
165 func ExampleRemainder() {
166 fmt.Printf("%.1f", math.Remainder(100, 30))
167 // Output: 10.0
168 }
169
170 func ExampleMod() {
171 c := math.Mod(7, 4)
172 fmt.Printf("%.1f", c)
173 // Output: 3.0
174 }
175
176 func ExampleAbs() {
177 x := math.Abs(-2)
178 fmt.Printf("%.1f\n", x)
179
180 y := math.Abs(2)
181 fmt.Printf("%.1f\n", y)
182 // Output:
183 // 2.0
184 // 2.0
185 }
186 func ExampleDim() {
187 fmt.Printf("%.2f\n", math.Dim(4, -2))
188 fmt.Printf("%.2f\n", math.Dim(-4, 2))
189 // Output:
190 // 6.00
191 // 0.00
192 }
193
194 func ExampleExp() {
195 fmt.Printf("%.2f\n", math.Exp(1))
196 fmt.Printf("%.2f\n", math.Exp(2))
197 fmt.Printf("%.2f\n", math.Exp(-1))
198 // Output:
199 // 2.72
200 // 7.39
201 // 0.37
202 }
203
204 func ExampleExp2() {
205 fmt.Printf("%.2f\n", math.Exp2(1))
206 fmt.Printf("%.2f\n", math.Exp2(-3))
207 // Output:
208 // 2.00
209 // 0.12
210 }
211
212 func ExampleExpm1() {
213 fmt.Printf("%.6f\n", math.Expm1(0.01))
214 fmt.Printf("%.6f\n", math.Expm1(-1))
215 // Output:
216 // 0.010050
217 // -0.632121
218 }
219
220 func ExampleTrunc() {
221 fmt.Printf("%.2f\n", math.Trunc(math.Pi))
222 fmt.Printf("%.2f\n", math.Trunc(-1.2345))
223 // Output:
224 // 3.00
225 // -1.00
226 }
227
228 func ExampleCbrt() {
229 fmt.Printf("%.2f\n", math.Cbrt(8))
230 fmt.Printf("%.2f\n", math.Cbrt(27))
231 // Output:
232 // 2.00
233 // 3.00
234 }
235
236 func ExampleModf() {
237 int, frac := math.Modf(3.14)
238 fmt.Printf("%.2f, %.2f\n", int, frac)
239
240 int, frac = math.Modf(-2.71)
241 fmt.Printf("%.2f, %.2f\n", int, frac)
242 // Output:
243 // 3.00, 0.14
244 // -2.00, -0.71
245 }