forked from rurema/doctree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger
More file actions
1060 lines (778 loc) · 26.7 KB
/
Integer
File metadata and controls
1060 lines (778 loc) · 26.7 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
= class Integer < Numeric
#@until 3.2
alias Fixnum
alias Bignum
#@end
整数クラスです。
整数オブジェクトに特異メソッドを追加する事はできません。追加した場合、
[[c:TypeError]] が発生します。
#@since 3.2
かつて Integer クラスのエイリアスであった Fixnum と Bignum は 3.2 で削除されました。
#@else
2.4.0 から [[c:Fixnum]], [[c:Bignum]] は Integerに統合されました。
2.4.0 からはどちらも Integer クラスのエイリアスとなっています。
#@end
== Class Methods
#@since 3.1
--- try_convert(obj) -> Integer | nil
obj を Integer に変換しようと試みます。変換には [[m:Object#to_int]]
メソッドが使われます。
Integer ならそのままobjを返します。
そうでなければ obj.to_int の結果を返すか、nil が返されます。
@param obj 変換する任意のオブジェクト
@return Integer または nil
@raise TypeError to_int が Integer を返さなかった場合に発生します。
#@samplecode 例
Integer.try_convert(1) # => 1
Integer.try_convert(1.25) # => 1
Integer.try_convert([]) # => nil
#@end
#@end
#@since 2.5.0
--- sqrt(n) -> Integer
非負整数 n の整数の平方根を返します。すなわち n の平方根以下の
最大の非負整数を返します。
@param n 非負整数。Integer ではない場合は、最初に Integer に変換されます。
@raise Math::DomainError n が負の整数の時に発生します。
#@samplecode
Integer.sqrt(0) # => 0
Integer.sqrt(1) # => 1
Integer.sqrt(24) # => 4
Integer.sqrt(25) # => 5
Integer.sqrt(10**400) == 10**200 # => true
#@end
Math.sqrt(n).floor と同等ですが、後者は浮動小数点数の精度の限界によって
真の値とは違う結果になることがあります。
#@samplecode
Integer.sqrt(10**46) #=> 100000000000000000000000
Math.sqrt(10**46).floor #=> 99999999999999991611392 (!)
#@end
@see [[m:Math.#sqrt]]
#@end
== Instance Methods
--- chr -> String
--- chr(encoding) -> String
self を文字コードとして見た時に、引数で与えたエンコーディング encoding に対応する文字を返します。
#@samplecode
p 65.chr
# => "A"
p 12354.chr
# => `chr': 12354 out of char range (RangeError)
p 12354.chr(Encoding::UTF_8)
# => "あ"
p 12354.chr(Encoding::EUC_JP)
# => RangeError: invalid codepoint 0x3042 in EUC-JP
#@end
引数無しで呼ばれた場合は self を US-ASCII、ASCII-8BIT、デフォルト内部エンコーディングの順で優先的に解釈します。
#@samplecode
p 0x79.chr.encoding # => #<Encoding:US_ASCII>
p 0x80.chr.encoding # => #<Encoding:ASCII_8BIT>
#@end
@param encoding エンコーディングを表すオブジェクト。Encoding::UTF_8、'shift_jis' など。
@return 一文字からなる文字列
@raise RangeError self を与えられたエンコーディングで正しく解釈できない場合に発生します。
@see [[m:String#ord]] [[m:Encoding.default_internal]]
--- digits -> [Integer]
--- digits(base) -> [Integer]
base を基数として self を位取り記数法で表記した数値を配列で返します。
base を指定しない場合の基数は 10 です。
#@samplecode
16.digits # => [6, 1]
16.digits(16) # => [0, 1]
#@end
self は非負整数でなければいけません。非負整数でない場合は、Math::DomainErrorが発生します。
#@samplecode
-10.digits # Math::DomainError: out of domain が発生
#@end
@return 位取り記数法で表した時の数値の配列
@param base 基数となる数値。
@raise ArgumentError base に正の整数以外を指定した場合に発生します。
@raise Math::DomainError 非負整数以外に対して呼び出した場合に発生します。
--- downto(min) {|n| ... } -> self
--- downto(min) -> Enumerator
self から min まで 1 ずつ減らしながらブロックを繰り返し実行します。
self < min であれば何もしません。
@param min 数値
@return self を返します。
#@samplecode
5.downto(1) {|i| print i, " " } # => 5 4 3 2 1
#@end
@see [[m:Integer#upto]], [[m:Numeric#step]], [[m:Integer#times]]
--- next -> Integer
--- succ -> Integer
self の次の整数を返します。
#@samplecode
1.next #=> 2
(-1).next #=> 0
1.succ #=> 2
(-1).succ #=> 0
#@end
@see [[m:Integer#pred]]
--- times {|n| ... } -> self
--- times -> Enumerator
self 回だけ繰り返します。
self が正の整数でない場合は何もしません。
またブロックパラメータには 0 から self - 1 までの数値が渡されます。
#@samplecode
3.times { puts "Hello, World!" } # Hello, World! と3行続いて表示される。
0.times { puts "Hello, World!" } # 何も表示されない。
5.times {|n| print n } # 01234 と表示される。
#@end
@see [[m:Integer#upto]], [[m:Integer#downto]], [[m:Numeric#step]]
--- to_i -> self
--- to_int -> self
self を返します。
#@samplecode
10.to_i # => 10
#@end
#@since 2.5.0
--- floor(ndigits = 0) -> Integer
#@else
--- floor(ndigits = 0) -> Integer | Float
#@end
self と等しいかより小さな整数のうち最大のものを返します。
@param ndigits 10進数での小数点以下の有効桁数を整数で指定します。
#@since 2.5.0
負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
#@else
正の整数を指定した場合、[[c:Float]] を返します。
小数点以下を、最大 n 桁にします。
負の整数を指定した場合、[[c:Integer]] を返します。
小数点位置から左に少なくとも n 個の 0 が並びます。
#@end
#@samplecode
1.floor # => 1
#@since 2.5.0
1.floor(2) # => 1
#@else
1.floor(2) # => 1.0
#@end
18.floor(-1) # => 10
(-18).floor(-1) # => -20
#@end
@see [[m:Numeric#floor]]
#@since 2.5.0
--- ceil(ndigits = 0) -> Integer
#@else
--- ceil(ndigits = 0) -> Integer | Float
#@end
self と等しいかより大きな整数のうち最小のものを返します。
@param ndigits 10進数での小数点以下の有効桁数を整数で指定します。
#@since 2.5.0
負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
#@else
正の整数を指定した場合、[[c:Float]] を返します。
小数点以下を、最大 n 桁にします。
負の整数を指定した場合、[[c:Integer]] を返します。
小数点位置から左に少なくとも n 個の 0 が並びます。
#@end
#@samplecode
1.ceil # => 1
#@since 2.5.0
1.ceil(2) # => 1
#@else
1.ceil(2) # => 1.0
#@end
18.ceil(-1) # => 20
(-18).ceil(-1) # => -10
#@end
@see [[m:Numeric#ceil]]
#@since 2.5.0
--- round(ndigits = 0, half: :up) -> Integer
#@else
--- round(ndigits = 0, half: :up) -> Integer | Float
#@end
self ともっとも近い整数を返します。
@param ndigits 10進数での小数点以下の有効桁数を整数で指定します。
#@since 2.5.0
負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
#@else
正の整数を指定した場合、[[c:Float]] を返します。
小数点以下を、最大 n 桁にします。
負の整数を指定した場合、[[c:Integer]] を返します。
小数点位置から左に少なくとも n 個の 0 が並びます。
#@end
@param half ちょうど半分の値の丸め方を指定します。
サポートされている値は以下の通りです。
* :up or nil: 0から遠い方に丸められます。
* :even: もっとも近い偶数に丸められます。
* :down: 0に近い方に丸められます。
#@samplecode
1.round # => 1
#@since 2.5.0
1.round(2) # => 1
#@else
1.round(2) # => 1.0
#@end
15.round(-1) # => 20
(-15).round(-1) # => -20
25.round(-1, half: :up) # => 30
25.round(-1, half: :down) # => 20
25.round(-1, half: :even) # => 20
35.round(-1, half: :up) # => 40
35.round(-1, half: :down) # => 30
35.round(-1, half: :even) # => 40
(-25).round(-1, half: :up) # => -30
(-25).round(-1, half: :down) # => -20
(-25).round(-1, half: :even) # => -20
#@end
@see [[m:Numeric#round]]
#@since 2.5.0
--- truncate(ndigits = 0) -> Integer
#@else
--- truncate(ndigits = 0) -> Integer | Float
#@end
0 から self までの整数で、自身にもっとも近い整数を返します。
@param ndigits 10進数での小数点以下の有効桁数を整数で指定します。
#@since 2.5.0
負の整数を指定した場合、小数点位置から左に少なくとも n 個の 0 が並びます。
#@else
正の整数を指定した場合、[[c:Float]] を返します。
小数点以下を、最大 n 桁にします。
負の整数を指定した場合、[[c:Integer]] を返します。
小数点位置から左に少なくとも n 個の 0 が並びます。
#@end
#@samplecode
1.truncate # => 1
#@since 2.5.0
1.truncate(2) # => 1
#@else
1.truncate(2) # => 1.0
#@end
18.truncate(-1) # => 10
(-18).truncate(-1) # => -10
#@end
@see [[m:Numeric#truncate]]
--- to_s(base=10) -> String
--- inspect(base=10) -> String
整数を 10 進文字列表現に変換します。
引数を指定すれば、それを基数とした文字列表
現に変換します。
#@samplecode
p 10.to_s(2) # => "1010"
p 10.to_s(8) # => "12"
p 10.to_s(16) # => "a"
p 35.to_s(36) # => "z"
#@end
@return 数値の文字列表現
@param base 基数となる 2 - 36 の数値。
@raise ArgumentError base に 2 - 36 以外の数値を指定した場合に発生します。
--- upto(max) {|n| ... } -> Integer
--- upto(max) -> Enumerator
self から max まで 1 ずつ増やしながら繰り返します。
self > max であれば何もしません。
@param max 数値
@return self を返します。
#@samplecode
5.upto(10) {|i| print i, " " } # => 5 6 7 8 9 10
#@end
@see [[m:Integer#downto]], [[m:Numeric#step]], [[m:Integer#times]]
--- integer? -> true
常に真を返します。
#@samplecode
1.integer? # => true
1.0.integer? # => false
#@end
--- even? -> bool
自身が偶数であれば真を返します。
そうでない場合は偽を返します。
#@samplecode
10.even? # => true
5.even? # => false
#@end
--- odd? -> bool
自身が奇数であれば真を返します。
そうでない場合は偽を返します。
#@samplecode
5.odd? # => true
10.odd? # => false
#@end
--- ord -> Integer
自身を返します。
#@samplecode
10.ord #=> 10
# String#ord
?a.ord #=> 97
#@end
@see [[m:String#ord]]
--- pred -> Integer
self から -1 した値を返します。
#@samplecode
1.pred #=> 0
(-1).pred #=> -2
#@end
@see [[m:Integer#next]]
--- denominator -> Integer
分母(常に1)を返します。
@return 分母を返します。
#@samplecode
10.denominator # => 1
-10.denominator # => 1
#@end
@see [[m:Integer#numerator]]
--- gcd(n) -> Integer
自身と整数 n の最大公約数を返します。
@raise ArgumentError n に整数以外のものを指定すると発生します。
#@samplecode
2.gcd(2) # => 2
3.gcd(7) # => 1
3.gcd(-7) # => 1
((1<<31)-1).gcd((1<<61)-1) # => 1
#@end
また、self や n が 0 だった場合は、0 ではない方の整数の絶対値を返します。
#@samplecode
3.gcd(0) # => 3
0.gcd(-7) # => 7
#@end
@see [[m:Integer#lcm]], [[m:Integer#gcdlcm]]
--- gcdlcm(n) -> [Integer]
自身と整数 n の最大公約数と最小公倍数の配列 [self.gcd(n), self.lcm(n)]
を返します。
@raise ArgumentError n に整数以外のものを指定すると発生します。
#@samplecode
2.gcdlcm(2) # => [2, 2]
3.gcdlcm(-7) # => [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1) # => [1, 4951760154835678088235319297]
#@end
@see [[m:Integer#gcd]], [[m:Integer#lcm]]
--- lcm(n) -> Integer
自身と整数 n の最小公倍数を返します。
@raise ArgumentError n に整数以外のものを指定すると発生します。
#@samplecode
2.lcm(2) # => 2
3.lcm(-7) # => 21
((1<<31)-1).lcm((1<<61)-1) # => 4951760154835678088235319297
#@end
また、self や n が 0 だった場合は、0 を返します。
#@samplecode
3.lcm(0) # => 0
0.lcm(-7) # => 0
#@end
@see [[m:Integer#gcd]], [[m:Integer#gcdlcm]]
--- numerator -> Integer
分子(常に自身)を返します。
@return 分子を返します。
#@samplecode
10.numerator # => 10
-10.numerator # => -10
#@end
@see [[m:Integer#denominator]]
--- to_r -> Rational
自身を [[c:Rational]] に変換します。
#@samplecode
1.to_r # => (1/1)
(1<<64).to_r # => (18446744073709551616/1)
#@end
--- rationalize -> Rational
--- rationalize(eps) -> Rational
自身を [[c:Rational]] に変換します。
@param eps 許容する誤差
引数 eps は常に無視されます。
#@samplecode
2.rationalize # => (2/1)
2.rationalize(100) # => (2/1)
2.rationalize(0.1) # => (2/1)
#@end
--- to_f -> Float
self を浮動小数点数([[c:Float]])に変換します。
self が [[c:Float]] の範囲に収まらない場合、[[m:Float::INFINITY]] を返します。
#@samplecode
1.to_f # => 1.0
(Float::MAX.to_i * 2).to_f # => Infinity
(-Float::MAX.to_i * 2).to_f # => -Infinity
#@end
--- <=>(other) -> -1 | 0 | 1 | nil
self と other を比較して、self が大きい時に1、等しい時に 0、小さい時
に-1、比較できない時に nil を返します。
@param other 比較対象の数値
@return -1 か 0 か 1 か nil のいずれか
#@samplecode
1 <=> 2 # => -1
1 <=> 1 # => 0
2 <=> 1 # => 1
2 <=> '' # => nil
#@end
--- -@ -> Integer
単項演算子の - です。
self の符号を反転させたものを返します。
#@samplecode
- 10 # => -10
- -10 # => 10
#@end
--- +(other) -> Numeric
算術演算子。和を計算します。
@param other 二項演算の右側の引数(対象)
@return 計算結果
#@samplecode
3 + 4 # => 7
#@end
--- -(other) -> Numeric
算術演算子。差を計算します。
@param other 二項演算の右側の引数(対象)
@return 計算結果
#@samplecode
4 - 1 #=> 3
#@end
--- *(other) -> Numeric
算術演算子。積を計算します。
@param other 二項演算の右側の引数(対象)
@return 計算結果
#@samplecode
2 * 3 # => 6
#@end
--- /(other) -> Numeric
除算の算術演算子。
other が Integer の場合、整商(整数の商)を Integer で返します。
普通の商(剰余を考えない商)を越えない最大の整数をもって整商とします。
other が Float、Rational、Complex の場合、普通の商を other と
同じクラスのインスタンスで返します。
@param other 二項演算の右側の引数(対象)
@return 計算結果
#@samplecode 例
7 / 2 # => 3
7 / -2 # => -4
7 / 2.0 # => 3.5
7 / Rational(2, 1) # => (7/2)
7 / Complex(2, 0) # => ((7/2)+0i)
begin
2 / 0
rescue => e
e # => #<ZeroDivisionError: divided by 0>
end
#@end
@see [[m:Integer#div]], [[m:Integer#fdiv]], [[m:Numeric#quo]]
--- div(other) -> Integer
整商(整数の商)を返します。
普通の商(剰余を考えない商)を越えない最大の整数をもって整商とします。
other が Integer オブジェクトの場合、[[m:Integer#/]] の結果と一致します。
div に対応する剰余メソッドは modulo です。
@param other 二項演算の右側の引数(対象)
@return 計算結果
#@samplecode 例
7.div(2) # => 3
7.div(-2) # => -4
7.div(2.0) # => 3
7.div(Rational(2, 1)) # => 3
begin
2.div(0)
rescue => e
e # => #<ZeroDivisionError: divided by 0>
end
begin
2.div(0.0)
rescue => e
e # => #<ZeroDivisionError: divided by 0>
# Integer#/ と違い、引数が Float でもゼロで割ることはできない
end
#@end
@see [[m:Integer#fdiv]], [[m:Integer#/]], [[m:Integer#modulo]]
--- %(other) -> Numeric
--- modulo(other) -> Numeric
算術演算子。剰余を計算します。
#@samplecode
13 % 4 # => 1
13 % -4 # => -3
-13 % 4 # => 3
-13 % -4 # => -1
#@end
@param other 二項演算の右側の引数(対象)
@return 計算結果
--- remainder(other) -> Numeric
self を other で割った余り r を返します。
r の符号は self と同じになります。
@param other self を割る数。
#@samplecode
5.remainder(3) # => 2
-5.remainder(3) # => -2
5.remainder(-3) # => 2
-5.remainder(-3) # => -2
-1234567890987654321.remainder(13731) # => -6966
-1234567890987654321.remainder(13731.24) # => -9906.22531493148
#@end
@see [[m:Integer#divmod]], [[m:Integer#modulo]], [[m:Numeric#modulo]]
--- divmod(other) -> [Integer, Numeric]
self を other で割った商 q と余り r を、 [q, r] という 2 要素の配列にし
て返します。 商 q は常に整数ですが、余り r は整数であるとは限りません。
@param other self を割る数。
@see [[m:Numeric#divmod]]
--- fdiv(other) -> Numeric
self を other で割った商を [[c:Float]] で返します。
ただし [[c:Complex]] が関わる場合は例外です。
その場合も成分は Float になります。
@param other self を割る数を指定します。
例:
654321.fdiv(13731) # => 47.652829364212366
654321.fdiv(13731.24) # => 47.65199646936475
-1234567890987654321.fdiv(13731) # => -89910996357705.52
-1234567890987654321.fdiv(13731.24) # => -89909424858035.72
@see [[m:Numeric#quo]], [[m:Numeric#div]], [[m:Integer#div]]
#@since 3.2
--- ceildiv(other) -> Integer
self を other で割り、その(剰余を考えない)商を整数に切り上げたものを返します。
すなわち、self を other で割った商を q とすると、q 以上で最小の整数を返します。
@param other self を割る数を指定します。
#@samplecode
3.ceildiv(3) # => 1
4.ceildiv(3) # => 2
5.ceildiv(3) # => 2
3.ceildiv(1.2) # => 3
-5.ceildiv(3) # => -1
-5.ceildiv(-3) # => 2
#@end
#@end
--- **(other) -> Numeric
--- pow(other) -> Numeric
--- pow(other, modulo) -> Integer
算術演算子。冪(べき乗)を計算します。
@param other 二項演算の右側の引数(対象)
@param modulo 指定すると、計算途中に巨大な値を生成せずに (self**other) % modulo と同じ結果を返します。
@return 計算結果
@raise TypeError 2引数 pow で Integer 以外を指定した場合に発生します。
@raise RangeError 2引数 pow で other に負の数を指定した場合に発生します。
#@since 3.4
@raise 計算結果が巨大になりすぎる場合に発生します。
#@end
#@samplecode
2 ** 3 # => 8
2 ** 0 # => 1
0 ** 0 # => 1
3.pow(3, 8) # => 3
3.pow(3, -8) # => -5
3.pow(2, -2) # => -1
-3.pow(3, 8) # => 5
-3.pow(3, -8) # => -3
5.pow(2, -8) # => -7
#@end
#@until 3.4
結果が巨大すぎる整数になりそうなとき、警告を出したうえで Float::INFINITY を返します。
#@samplecode 計算を放棄して Float::INFINITY を返す例
p 100**9999999
# => warning: in a**b, b may be too big
# Infinity
#@end
判定の閾値は変わりえます。
#@end
#@since 3.4
計算結果が巨大すぎるときは ArgumentError が発生します。
#@samplecode 計算結果が巨大すぎる例
p 100**9999999999999999999
# => exponent is too large (ArgumentError)
#@end
判定の閾値は変わりえます。
#@end
@see [[m:BigDecimal#power]]
--- abs -> Integer
--- magnitude -> Integer
self の絶対値を返します。
#@samplecode
-12345.abs # => 12345
12345.abs # => 12345
-1234567890987654321.abs # => 1234567890987654321
#@end
--- ==(other) -> bool
--- ===(other) -> bool
比較演算子。数値として等しいか判定します。
@param other 比較対象の数値
@return self と other が等しい場合 true を返します。
そうでなければ false を返します。
#@samplecode
1 == 2 # => false
1 == 1.0 # => true
#@end
--- <(other) -> bool
比較演算子。数値として小さいか判定します。
@param other 比較対象の数値
@return self よりも other が大きい場合 true を返します。
そうでなければ false を返します。
#@samplecode
1 < 1 # => false
1 < 2 # => true
#@end
--- <=(other) -> bool
比較演算子。数値として等しいまたは小さいか判定します。
@param other 比較対象の数値
@return self よりも other の方が大きい場合か、
両者が等しい場合 true を返します。
そうでなければ false を返します。
#@samplecode
1 <= 0 # => false
1 <= 1 # => true
1 <= 2 # => true
#@end
--- >(other) -> bool
比較演算子。数値として大きいか判定します。
@param other 比較対象の数値
@return self よりも other の方が小さい場合 true を返します。
そうでなければ false を返します。
#@samplecode
1 > 0 # => true
1 > 1 # => false
#@end
--- >=(other) -> bool
比較演算子。数値として等しいまたは大きいか判定します。
@param other 比較対象の数値
@return self よりも other の方が小さい場合か、
両者が等しい場合 true を返します。
そうでなければ false を返します。
#@samplecode
1 >= 0 # => true
1 >= 1 # => true
1 >= 2 # => false
#@end
--- ~ -> Integer
ビット演算子。否定を計算します。
#@samplecode
~1 # => -2
~3 # => -4
~-4 # => 3
#@end
--- |(other) -> Integer
ビット二項演算子。論理和を計算します。
@param other 数値
#@samplecode
1 | 1 # => 1
2 | 3 # => 3
#@end
--- &(other) -> Integer
ビット二項演算子。論理積を計算します。
@param other 数値
#@samplecode
1 & 1 # => 1
2 & 3 # => 2
#@end
--- ^(other) -> Integer
ビット二項演算子。排他的論理和を計算します。
@param other 数値
#@samplecode
1 ^ 1 # => 0
2 ^ 3 # => 1
#@end
--- [](nth) -> Integer
#@since 2.7.0
--- [](nth, len) -> Integer
--- [](range) -> Integer
#@end
nth 番目のビット(最下位ビット(LSB)が 0 番目)が立っている時 1
を、そうでなければ 0 を返します。
@param nth 何ビット目を指すかの数値
#@since 2.7.0
@param len 何ビット分を返すか
@param range 返すビットの範囲
@return self[nth] は 1 か 0
@return self[i, len] は (n >> i) & ((1 << len) - 1) と同じ
@return self[i..j] は (n >> i) & ((1 << (j - i + 1)) - 1) と同じ
@return self[i...j] は (n >> i) & ((1 << (j - i)) - 1) と同じ
@return self[i..] は (n >> i) と同じ
@return self[..j] は n & ((1 << (j + 1)) - 1) が 0 なら 0
@return self[...j] は n & ((1 << j) - 1) が 0 なら 0
@raise ArgumentError self[..j] で n & ((1 << (j + 1)) - 1) が 0 以外のとき
@raise ArgumentError self[...j] で n & ((1 << j) - 1) が 0 以外のとき
#@else
@return 1 か 0
#@end
#@samplecode
a = 0b11001100101010
30.downto(0) {|n| print a[n] }
# => 0000000000000000011001100101010
a = 9**15
50.downto(0) {|n| print a[n] }
# => 000101110110100000111000011110010100111100010111001
#@end
n[i] は (n >> i) & 1 と等価なので、負のインデックスは常に 0 を返します。
#@samplecode
p 255[-1] # => 0
#@end
#@since 2.7.0
#@samplecode 複数ビットの例
0b01001101[2, 4] #=> 0b0011
0b01001100[2..5] #=> 0b0011
0b01001100[2...6] #=> 0b0011
# ^^^^
#@end
#@end
self[nth]=bit (つまりビットの修正) がないのは、Numeric 関連クラスが
immutable であるためです。
--- <<(bits) -> Integer
シフト演算子。bits だけビットを左にシフトします。
@param bits シフトさせるビット数
#@samplecode
printf("%#b\n", 0b0101 << 1) # => 0b1010
p -1 << 1 # => -2
#@end
--- >>(bits) -> Integer
シフト演算子。bits だけビットを右にシフトします。
右シフトは、符号ビット(最上位ビット(MSB))が保持されます。
bitsが実数の場合、小数点以下を切り捨てた値でシフトします。
@param bits シフトさせるビット数
#@samplecode
printf("%#b\n", 0b0101 >> 1) # => 0b10
p -1 >> 1 # => -1
#@end
--- size -> Integer
整数の実装上のサイズをバイト数で返します。
#@samplecode
p 1.size # => 8
p 0x1_0000_0000.size # => 8
#@end
@see [[m:Integer#bit_length]]
--- bit_length -> Integer
self を表すのに必要なビット数を返します。
「必要なビット数」とは符号ビットを除く最上位ビットの位置の事を意味しま
す。2**n の場合は n+1 になります。self にそのようなビットがない(0 や
-1 である)場合は 0 を返します。
#@samplecode 例: ceil(log2(int < 0 ? -int : int+1)) と同じ結果
(-2**12-1).bit_length # => 13
(-2**12).bit_length # => 12
(-2**12+1).bit_length # => 12
-0x101.bit_length # => 9
-0x100.bit_length # => 8
-0xff.bit_length # => 8
-2.bit_length # => 1
-1.bit_length # => 0