forked from rurema/doctree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRational
More file actions
486 lines (332 loc) · 12.3 KB
/
Rational
File metadata and controls
486 lines (332 loc) · 12.3 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
= class Rational < Numeric
有理数を扱うクラスです。
「1/3」のような有理数を扱う事ができます。[[c:Integer]] や [[c:Float]]
と同様に Rational.new ではなく、 [[m:Kernel.#Rational]] を使用して
[[c:Rational]] オブジェクトを作成します。
#@samplecode 例
Rational(1, 3) # => (1/3)
Rational('1/3') # => (1/3)
Rational('0.33') # => (33/100)
Rational.new(1, 3) # => NoMethodError
#@end
[[c:Rational]] オブジェクトは常に既約(それ以上約分できない状態)である
事に注意してください。
#@samplecode 例
Rational(2, 6) # => (1/3)
Rational(1, 3) * 3 # => (1/1)
#@end
== Public Instance Methods
--- *(other) -> Rational | Float
積を計算します。
@param other 自身に掛ける数
other に [[c:Float]] を指定した場合は、計算結果を [[c:Float]] で返しま
す。
#@samplecode 例
r = Rational(3, 4)
r * 2 # => (3/2)
r * 4 # => (3/1)
r * 0.5 # => 0.375
r * Rational(1, 2) # => (3/8)
#@end
--- **(other) -> Rational | Float
冪(べき)乗を計算します。
@param other 自身を other 乗する数
#@since 3.4
@raise ArgumentError 計算結果の分母・分子が巨大すぎる場合に発生します。
#@end
other に [[c:Float]] を指定した場合は、計算結果を [[c:Float]] で返しま
す。other が有理数であっても、計算結果が無理数だった場合は [[c:Float]]
を返します。
#@samplecode 例
r = Rational(3, 4)
r ** Rational(2, 1) # => (9/16)
r ** 2 # => (9/16)
r ** 2.0 # => 0.5625
r ** Rational(1, 2) # => 0.866025403784439
#@end
--- +(other) -> Rational | Float
和を計算します。
@param other 自身に足す数
other に [[c:Float]] を指定した場合は、計算結果を [[c:Float]] で返しま
す。
#@samplecode 例
r = Rational(3, 4)
r + Rational(1, 2) # => (5/4)
r + 1 # => (7/4)
r + 0.5 # => 1.25
#@end
--- -(other) -> Rational | Float
差を計算します。
@param other 自身から引く数
other に [[c:Float]] を指定した場合は、計算結果を [[c:Float]] で返しま
す。
#@samplecode 例
r = Rational(3, 4)
r - 1 # => (-1/4)
r - 0.5 # => 0.25
#@end
--- /(other) -> Rational | Float
--- quo(other) -> Rational | Float
商を計算します。
@param other 自身を割る数
other に [[c:Float]] を指定した場合は、計算結果を [[c:Float]] で返します。
#@samplecode 例
r = Rational(3, 4)
r / 2 # => (3/8)
r / 2.0 # => 0.375
r / 0.5 # => 1.5
r / Rational(1, 2) # => (3/2)
r / 0 # => ZeroDivisionError
#@end
@raise ZeroDivisionError other が 0 の時に発生します。
@see [[m:Numeric#quo]]
#@since 2.4.0
--- -@ -> Rational
単項演算子の - です。
self の符号を反転させたものを返します。
#@samplecode 例
r = Rational(3, 4)
- r # => (-3/4)
#@end
#@end
--- <=>(other) -> -1 | 0 | 1 | nil
self と other を比較して、self が大きい時に 1、等しい時に 0、小さい時に
-1 を返します。比較できない場合はnilを返します。
@param other 自身と比較する数値
@return -1 か 0 か 1 か nil を返します。
#@samplecode 例
Rational(2, 3) <=> Rational(2, 3) # => 0
Rational(5) <=> 5 # => 0
Rational(2, 3) <=> Rational(1,3) # => 1
Rational(1, 3) <=> 1 # => -1
Rational(1, 3) <=> 0.3 # => 1
Rational(1, 3) <=> nil # => nil
#@end
--- ==(other) -> bool
数値として等しいか判定します。
@param other 自身と比較する数値
@return self と other が等しい場合 true を返します。
そうでなければ false を返します。
#@samplecode 例
Rational(2, 3) == Rational(2, 3) # => true
Rational(5) == 5 # => true
Rational(0) == 0.0 # => true
Rational('1/3') == 0.33 # => false
Rational('1/2') == '1/2' # => false
#@end
#@since 2.4.0
--- positive? -> bool
self が 0 より大きい場合に true を返します。そうでない場合に false を返します。
#@samplecode 例
Rational(1, 2).positive? # => true
Rational(-1, 2).positive? # => false
#@end
@see [[m:Rational#negative?]]
--- negative? -> bool
self が 0 未満の場合に true を返します。そうでない場合に false を返します。
#@samplecode 例
Rational(1, 2).negative? # => false
Rational(-1, 2).negative? # => true
#@end
@see [[m:Rational#positive?]]
--- abs -> Rational
--- magnitude -> Rational
自身の絶対値を返します。
#@samplecode 例
Rational(1, 2).abs # => (1/2)
Rational(-1, 2).abs # => (1/2)
#@end
#@end
--- ceil(precision = 0) -> Integer | Rational
自身と等しいかより大きな整数のうち最小のものを返します。
@param precision 計算結果の精度
@raise TypeError precision に整数以外のものを指定すると発生します。
#@samplecode 例
Rational(3).ceil # => 3
Rational(2, 3).ceil # => 1
Rational(-3, 2).ceil # => -1
#@end
precision を指定した場合は指定した桁数の数値と、上述の性質に最も近い整
数か [[c:Rational]] を返します。
#@samplecode 例
Rational('-123.456').ceil(+1) # => (-617/5)
Rational('-123.456').ceil(+1).to_f # => -123.4
Rational('-123.456').ceil(0) # => -123
Rational('-123.456').ceil(-1) # => -120
#@end
@see [[m:Rational#floor]], [[m:Rational#round]], [[m:Rational#truncate]]
--- coerce(other) -> Array
自身と other が同じクラスになるよう、自身か other を変換し [other, self] という
配列にして返します。
@param other 比較または変換するオブジェクト
#@samplecode 例
Rational(1).coerce(2) # => [(2/1), (1/1)]
Rational(1).coerce(2.2) # => [2.2, 1.0]
#@end
--- denominator -> Integer
分母を返します。常に正の整数を返します。
@return 分母を返します。
#@samplecode 例
Rational(7).denominator # => 1
Rational(7, 1).denominator # => 1
Rational(9, -4).denominator # => 4
Rational(-2, -10).denominator # => 5
#@end
@see [[m:Rational#numerator]]
--- fdiv(other) -> Float
self を other で割った商を [[c:Float]] で返します。
other に虚数を指定することは出来ません。
@param other 自身を割る数
#@samplecode 例
Rational(2, 3).fdiv(1) # => 0.6666666666666666
Rational(2, 3).fdiv(0.5) # => 1.3333333333333333
Rational(2).fdiv(3) # => 0.6666666666666666
Rational(1).fdiv(Complex(1, 0)) # => 1.0
Rational(1).fdiv(Complex(0, 1)) # => RangeError
#@end
--- floor(precision = 0) -> Integer | Rational
自身と等しいかより小さな整数のうち最大のものを返します。
@param precision 計算結果の精度
@raise TypeError precision に整数以外のものを指定すると発生します。
#@samplecode 例
Rational(3).floor # => 3
Rational(2, 3).floor # => 0
Rational(-3, 2).floor # => -2
#@end
[[m:Rational#to_i]] とは違う結果を返す事に注意してください。
#@samplecode 例
Rational(+7, 4).to_i # => 1
Rational(+7, 4).floor # => 1
Rational(-7, 4).to_i # => -1
Rational(-7, 4).floor # => -2
#@end
precision を指定した場合は指定した桁数の数値と、上述の性質に最も近い整
数か [[c:Rational]] を返します。
#@samplecode 例
Rational('-123.456').floor(+1) # => (-247/2)
Rational('-123.456').floor(+1).to_f # => -123.5
Rational('-123.456').floor(0) # => -124
Rational('-123.456').floor(-1) # => -130
#@end
@see [[m:Rational#ceil]], [[m:Rational#round]], [[m:Rational#truncate]]
--- hash -> Integer
自身のハッシュ値を返します。
@return ハッシュ値を返します。
#@#noexample
@see [[m:Object#hash]]
--- inspect -> String
自身を人間が読みやすい形の文字列表現にして返します。
"(3/5)", "(-17/7)" のように10進数の表記を返します。
@return 有理数の表記にした文字列を返します。
#@samplecode 例
Rational(5, 8).inspect # => "(5/8)"
Rational(2).inspect # => "(2/1)"
Rational(-8, 6).inspect # => "(-4/3)"
Rational(0.5).inspect # => "(1/2)"
#@end
@see [[m:Rational#to_s]]
--- numerator -> Integer
分子を返します。
@return 分子を返します。
#@samplecode 例
Rational(7).numerator # => 7
Rational(7, 1).numerator # => 7
Rational(9, -4).numerator # => -9
Rational(-2, -10).numerator # => 1
#@end
@see [[m:Rational#denominator]]
--- rationalize(eps = 0) -> Rational
自身から eps で指定した許容誤差の範囲に収まるような [[c:Rational]] を返
します。
eps を省略した場合は self を返します。
@param eps 許容する誤差
#@samplecode 例
r = Rational(5033165, 16777216)
r.rationalize # => (5033165/16777216)
r.rationalize(Rational(0.01)) # => (3/10)
r.rationalize(Rational(0.1)) # => (1/3)
#@end
--- round(precision = 0) -> Integer | Rational
自身ともっとも近い整数を返します。
中央値 0.5, -0.5 はそれぞれ 1,-1 に切り上げされます。
@param precision 計算結果の精度
@raise TypeError precision に整数以外のものを指定すると発生します。
#@samplecode 例
Rational(3).round # => 3
Rational(2, 3).round # => 1
Rational(-3, 2).round # => -2
#@end
precision を指定した場合は指定した桁数の数値と、上述の性質に最も近い整
数か [[c:Rational]] を返します。
#@samplecode 例
Rational('-123.456').round(+1) # => (-247/2)
Rational('-123.456').round(+1).to_f # => -123.5
Rational('-123.456').round(0) # => -123
Rational('-123.456').round(-1) # => -120
Rational('-123.456').round(-2) # => -100
#@end
@see [[m:Rational#ceil]], [[m:Rational#floor]], [[m:Rational#truncate]]
--- to_f -> Float
自身の値を最も良く表現する [[c:Float]] に変換します。
絶対値が極端に小さい、または大きい場合にはゼロや無限大が返ることがあります。
@return [[c:Float]] を返します。
#@samplecode 例
Rational(2).to_f # => 2.0
Rational(9, 4).to_f # => 2.25
Rational(-3, 4).to_f # => -0.75
Rational(20, 3).to_f # => 6.666666666666667
Rational(1, 10**1000).to_f # => 0.0
Rational(-1, 10**1000).to_f # => -0.0
Rational(10**1000).to_f # => Infinity
Rational(-10**1000).to_f # => -Infinity
#@end
--- to_i -> Integer
--- truncate(precision = 0) -> Rational | Integer
小数点以下を切り捨てて値を整数に変換します。
@param precision 計算結果の精度
@raise TypeError precision に整数以外のものを指定すると発生します。
#@samplecode 例
Rational(2, 3).to_i # => 0
Rational(3).to_i # => 3
Rational(300.6).to_i # => 300
Rational(98, 71).to_i # => 1
Rational(-31, 2).to_i # => -15
#@end
precision を指定した場合は指定した桁数で切り捨てた整数か
[[c:Rational]] を返します。
#@samplecode 例
Rational('-123.456').truncate(+1) # => (-617/5)
Rational('-123.456').truncate(+1).to_f # => -123.4
Rational('-123.456').truncate(0) # => -123
Rational('-123.456').truncate(-1) # => -120
#@end
@see [[m:Rational#ceil]], [[m:Rational#floor]]
--- to_r -> Rational
自身を返します。
@return 自身を返します。
#@samplecode 例
Rational(3, 4).to_r # => (3/4)
Rational(8).to_r # => (8/1)
#@end
--- to_s -> String
自身を人間が読みやすい形の文字列表現にして返します。
"3/5", "-17/7" のように10進数の表記を返します。
@return 有理数の表記にした文字列を返します。
#@samplecode 例
Rational(3, 4).to_s # => "3/4"
Rational(8).to_s # => "8/1"
Rational(-8, 6).to_s # => "-4/3"
Rational(0.5).to_s # => "1/2"
#@end
@see [[m:Rational#inspect]]
== Private Instance Methods
--- convert(*arg) -> Rational
引数を有理数([[c:Rational]])に変換した結果を返します。
@param arg 変換対象のオブジェクトです。
[[m:Kernel.#Rational]] の本体です。
@see [[m:Kernel.#Rational]]
--- marshal_dump -> Array
[[m:Marshal.#load]] のためのメソッドです。
Rational::compatible#marshal_load で復元可能な配列を返します。
[注意] Rational::compatible は通常の方法では参照する事ができません。
#@# #6625 を参照。