Source file
src/math/example_test.go
Documentation: math
1
2
3
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
15 }
16
17 func ExampleAcosh() {
18 fmt.Printf("%.2f", math.Acosh(1))
19
20 }
21
22 func ExampleAsin() {
23 fmt.Printf("%.2f", math.Asin(0))
24
25 }
26
27 func ExampleAsinh() {
28 fmt.Printf("%.2f", math.Asinh(0))
29
30 }
31
32 func ExampleAtan() {
33 fmt.Printf("%.2f", math.Atan(0))
34
35 }
36
37 func ExampleAtan2() {
38 fmt.Printf("%.2f", math.Atan2(0, 0))
39
40 }
41
42 func ExampleAtanh() {
43 fmt.Printf("%.2f", math.Atanh(0))
44
45 }
46
47 func ExampleCopysign() {
48 fmt.Printf("%.2f", math.Copysign(3.2, -1))
49
50 }
51
52 func ExampleCos() {
53 fmt.Printf("%.2f", math.Cos(math.Pi/2))
54
55 }
56
57 func ExampleCosh() {
58 fmt.Printf("%.2f", math.Cosh(0))
59
60 }
61
62 func ExampleSin() {
63 fmt.Printf("%.2f", math.Sin(math.Pi))
64
65 }
66
67 func ExampleSincos() {
68 sin, cos := math.Sincos(0)
69 fmt.Printf("%.2f, %.2f", sin, cos)
70
71 }
72
73 func ExampleSinh() {
74 fmt.Printf("%.2f", math.Sinh(0))
75
76 }
77
78 func ExampleTan() {
79 fmt.Printf("%.2f", math.Tan(0))
80
81 }
82
83 func ExampleTanh() {
84 fmt.Printf("%.2f", math.Tanh(0))
85
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
96 }
97
98 func ExampleCeil() {
99 c := math.Ceil(1.49)
100 fmt.Printf("%.1f", c)
101
102 }
103
104 func ExampleFloor() {
105 c := math.Floor(1.51)
106 fmt.Printf("%.1f", c)
107
108 }
109
110 func ExamplePow() {
111 c := math.Pow(2, 3)
112 fmt.Printf("%.1f", c)
113
114 }
115
116 func ExamplePow10() {
117 c := math.Pow10(2)
118 fmt.Printf("%.1f", c)
119
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
129
130
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
140
141
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
151
152
153 }
154
155 func ExampleLog2() {
156 fmt.Printf("%.1f", math.Log2(256))
157
158 }
159
160 func ExampleLog10() {
161 fmt.Printf("%.1f", math.Log10(100))
162
163 }
164
165 func ExampleMod() {
166 c := math.Mod(7, 4)
167 fmt.Printf("%.1f", c)
168
169 }
170
171 func ExampleAbs() {
172 x := math.Abs(-2)
173 fmt.Printf("%.1f\n", x)
174
175 y := math.Abs(2)
176 fmt.Printf("%.1f\n", y)
177
178
179
180 }
181 func ExampleDim() {
182 fmt.Printf("%.2f\n", math.Dim(4, -2))
183 fmt.Printf("%.2f\n", math.Dim(-4, 2))
184
185
186
187 }
188
189 func ExampleExp() {
190 fmt.Printf("%.2f\n", math.Exp(1))
191 fmt.Printf("%.2f\n", math.Exp(2))
192 fmt.Printf("%.2f\n", math.Exp(-1))
193
194
195
196
197 }
198
199 func ExampleExp2() {
200 fmt.Printf("%.2f\n", math.Exp2(1))
201 fmt.Printf("%.2f\n", math.Exp2(-3))
202
203
204
205 }
206
207 func ExampleExpm1() {
208 fmt.Printf("%.6f\n", math.Expm1(0.01))
209 fmt.Printf("%.6f\n", math.Expm1(-1))
210
211
212
213 }
214
215 func ExampleTrunc() {
216 fmt.Printf("%.2f\n", math.Trunc(math.Pi))
217 fmt.Printf("%.2f\n", math.Trunc(-1.2345))
218
219
220
221 }
222
223 func ExampleCbrt() {
224 fmt.Printf("%.2f\n", math.Cbrt(8))
225 fmt.Printf("%.2f\n", math.Cbrt(27))
226
227
228
229 }
230
231 func ExampleModf() {
232 int, frac := math.Modf(3.14)
233 fmt.Printf("%.2f, %.2f\n", int, frac)
234
235 int, frac = math.Modf(-2.71)
236 fmt.Printf("%.2f, %.2f\n", int, frac)
237
238
239
240 }
241
View as plain text