OSDN Git Service

...。
[ring-lang-081/ring.git] / docs / ja-jp / build / html / _sources / mathfunc.txt
1 .. index:: 
2         single: 算術関数; はじめに
3
4 ========
5 算術関数
6 ========
7
8 算術関数の用法を学びます。
9
10 .. index:: 
11         pair: 算術関数; 関数のリスト
12
13 関数のリスト
14 ============
15
16 このような算術関数が Ring にあります。
17
18 =============== ============================================================================================
19 関数            説明
20 =============== ============================================================================================
21 sin(x)          ラジアン x のサイン角度を返します。
22 cos(x)          ラジアン x のコサイン角度を返します。
23 tan(x)          ラジアン x のタンジェント角度を返します。
24 asin(x)         ラジアン表記によるアークサインの x の主値を返します。
25 acos(x)         ラジアン表記によるアークコサインの x の主値を返します。
26 atan(x)         ラジアン表記によるアークタンジェントの x の主値を返します。
27 atan2(y,x)      ラジアン [-pi,+pi] の区間にある、ラジアン表記によるアークタンジェントの x の主値を返します。
28 sinh(x)         ラジアン x の双曲線サインを返します。
29 cosh(x)         ラジアン x の双曲線コサインを返します。
30 tanh(x)         ラジアン x の双曲線タンジェントを返します。
31 exp(x)          e の x 乗値を返します。
32 log(x)          x の常用対数を返します。
33 log10(x)        x の常用対数を返します (10 を底とする対数)。
34 ceil(x)         x 以上の最小整数値を返します。
35 floor(x)        x 以下の最大整数値を返します。
36 fabs(x)         x の絶対値を返します。
37 pow(x,y)        x に対する y の累乗を返します。
38 sqrt(x)         x の平方根を返します。
39 random(x)       [0,x] の範囲による乱数を返します。
40 srandom(x)              乱数生成器を初期化します。
41 unsigned(n,n,c) 符号なし数値で演算を実行します。
42 decimals(n)     浮動小数点数、倍精度数の小数点の後にある小数点以下の数値を決定します。
43 =============== ============================================================================================
44
45 .. index:: 
46         pair: 算術関数; 用例
47
48 用例
49 ====
50
51 .. code-block:: ring
52
53         See "Mathematical Functions" + nl
54         See "Sin(0) = " + sin(0) + nl
55         See "Sin(90) radians = " + sin(90) + nl
56         See "Sin(90) degree = " + sin(90*3.14/180) + nl
57
58         See "Cos(0) = " + cos(0) + nl
59         See "Cos(90) radians = " + cos(90) + nl
60         See "Cos(90) degree = " + cos(90*3.14/180) + nl
61
62         See "Tan(0) = " + tan(0) + nl
63         See "Tan(90) radians = " + tan(90) + nl
64         See "Tan(90) degree = " + tan(90*3.14/180) + nl
65
66         See "asin(0) = " + asin(0) + nl
67         See "acos(0) = " + acos(0) + nl
68         See "atan(0) = " + atan(0) + nl
69         See "atan2(1,1) = " + atan2(1,1) + nl
70
71         See "sinh(0) = " + sinh(0) + nl
72         See "sinh(1) = " + sinh(1) + nl
73         See "cosh(0) = " + cosh(0) + nl
74         See "cosh(1) = " + cosh(1) + nl
75         See "tanh(0) = " + tanh(0) + nl
76         See "tanh(1) = " + tanh(1) + nl
77
78         See "exp(0) = " + exp(0) + nl
79         See "exp(1) = " + exp(1) + nl
80         See "log(1) = " + log(1) + nl
81         See "log(2) = " + log(2) + nl
82         See "log10(1) = " + log10(1) + nl
83         See "log10(2) = " + log10(2) + nl
84         See "log10(10) = " + log10(10) + nl
85
86         See "Ceil(1.12) = " + Ceil(1.12) + nl
87         See "Ceil(1.72) = " + Ceil(1.72) + nl
88
89         See "Floor(1.12) = " + floor(1.12) + nl
90         See "Floor(1.72) = " + floor(1.72) + nl
91
92         See "fabs(1.12) = " + fabs(1.12) + nl
93         See "fabs(1.72) = " + fabs(1.72) + nl
94
95         See "pow(2,3) = " + pow(2,3) + nl
96
97         see "sqrt(16) = " + sqrt(16) + nl
98
99
100 プログラムの出力:
101
102 .. code-block:: ring
103
104         Mathematical Functions
105         Sin(0) = 0
106         Sin(90) radians = 0.89
107         Sin(90) degree = 1.00
108         Cos(0) = 1
109         Cos(90) radians = -0.45
110         Cos(90) degree = 0.00
111         Tan(0) = 0
112         Tan(90) radians = -2.00
113         Tan(90) degree = 1255.77
114         asin(0) = 0
115         acos(0) = 1.57
116         atan(0) = 0
117         atan2(1,1) = 0.79
118         sinh(0) = 0
119         sinh(1) = 1.18
120         cosh(0) = 1
121         cosh(1) = 1.54
122         tanh(0) = 0
123         tanh(1) = 0.76
124         exp(0) = 1
125         exp(1) = 2.72
126         log(1) = 0
127         log(2) = 0.69
128         log10(1) = 0
129         log10(2) = 0.30
130         log10(10) = 1
131         Ceil(1.12) = 2
132         Ceil(1.72) = 2
133         Floor(1.12) = 1
134         Floor(1.72) = 1
135         fabs(1.12) = 1.12
136         fabs(1.72) = 1.72
137         pow(2,3) = 8
138         sqrt(16) = 4
139
140 .. index:: 
141         pair: 算術関数; Random()
142
143 Random() 関数
144 =============
145
146 Random() 関数は乱数を生成します。また最大値の指定もできます (オプション扱い)。
147
148 文法:
149
150 .. code-block:: ring
151
152         Random(x) ---> [0,x] の範囲による乱数。
153
154 用例:
155
156 .. code-block:: ring
157
158         for x = 1 to 20
159                 see  "Random number : " + random() + nl +
160                      "Random number Max (100) : " + random(100) + nl
161         next
162
163 プログラムの出力:
164
165 .. code-block:: ring
166
167         Random number : 31881
168         Random number Max (100) : 80
169         Random number : 5573
170         Random number Max (100) : 63
171         Random number : 2231
172         Random number Max (100) : 43
173         Random number : 12946
174         Random number Max (100) : 39
175         Random number : 22934
176         Random number Max (100) : 48
177         Random number : 4690
178         Random number Max (100) : 52
179         Random number : 13196
180         Random number Max (100) : 65
181         Random number : 30390
182         Random number Max (100) : 87
183         Random number : 4327
184         Random number Max (100) : 77
185         Random number : 12456
186         Random number Max (100) : 17
187         Random number : 28438
188         Random number Max (100) : 13
189         Random number : 30503
190         Random number Max (100) : 6
191         Random number : 31769
192         Random number Max (100) : 94
193         Random number : 8274
194         Random number Max (100) : 65
195         Random number : 14390
196         Random number Max (100) : 90
197         Random number : 28866
198         Random number Max (100) : 12
199         Random number : 24558
200         Random number Max (100) : 70
201         Random number : 29981
202         Random number Max (100) : 77
203         Random number : 12847
204         Random number Max (100) : 63
205         Random number : 6632
206         Random number Max (100) : 60
207
208 .. index:: 
209         pair: 算術関数; SRandom()
210
211 SRandom() 関数
212 ==============
213
214 SRandom() 関数は乱数生成器を初期化します。
215
216 文法:
217
218 .. code-block:: ring
219
220         SRandom(x) 
221
222 .. index:: 
223         pair: 算術関数; Unsigned()
224
225 Unsigned() 関数
226 ===============
227
228 Unsigned() 関数により符号なし整数を使えます。
229
230 文法:
231
232 .. code-block:: ring
233
234         Unsigned(nNum1,nNum2,cOperator)
235                 --> nNum1,nNum2 における cOperator の演算結果。
236
237 用例:
238
239 .. code-block:: ring
240
241         see oat_hash("hello") + nl
242
243         # ジェンキンス法によるハッシュ関数 - https://en.wikipedia.org/wiki/Jenkins_hash_function
244         func oat_hash cKey      
245                 h = 0
246                 for x in cKey
247                         h = unsigned(h,ascii(x),"+")
248                         h = unsigned(h,unsigned(h,10,"<<"),"+")
249                         r = unsigned(h,6,">>")
250                         h = unsigned(h, r,"^")
251                 next
252                 h = unsigned(h,unsigned(h,3,"<<"),"+")
253                 h = unsigned(h,unsigned(h,11,">>"),"^")
254                 h = unsigned(h,unsigned(h,15,"<<"),"+")
255                 return h  
256
257 実行結果:
258
259 .. code-block:: ring
260
261         3372029979.00
262
263 .. index:: 
264         pair: 算術関数; Decimals()
265
266 Decimals() 関数
267 ===============
268
269 Decimals() により浮動小数点数、倍精度数の小数点の後にある小数点以下の数値を決定できます。
270
271 文法:
272
273 .. code-block:: ring
274
275         Decimals(nDecimalsCount)
276
277 用例:
278
279 .. code-block:: ring
280
281         x = 1.1234567890123
282         for d = 0 to 14
283                 decimals(d)
284                 see x + nl
285         next
286
287
288 実行結果:
289
290 .. code-block:: ring
291
292
293         1
294         1.1
295         1.12
296         1.123
297         1.1235
298         1.12346
299         1.123457
300         1.1234568
301         1.12345679
302         1.123456789
303         1.1234567890
304         1.12345678901
305         1.123456789012
306         1.1234567890123
307         1.12345678901230
308
309
310 .. index:: 
311         pair: 算術関数; 数値の桁間に _ を使うには
312
313 数値の桁間に _ を使うには
314 =========================
315
316 数値の桁間に‘_’を使うことができます。
317
318 用例:
319
320 .. code-block:: ring
321
322         x = 1_000_000
323         see type(x)+nl
324         see x+1+nl
325
326 実行結果:
327
328 .. code-block:: ring
329
330         NUMBER
331         100000001
332
333 .. index:: 
334         pair: 算術関数; 数値の末尾に f を使うには
335
336 数値の末尾に f を使うには
337 =========================
338
339 数値の末尾に‘f’を使うことができます。
340
341 用例:
342
343 .. code-block:: ring
344
345         x = 19.99f
346         see type(x) + nl
347
348 実行結果:
349
350 .. code-block:: ring
351
352         NUMBER