-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric.rbs
More file actions
818 lines (771 loc) · 23.7 KB
/
numeric.rbs
File metadata and controls
818 lines (771 loc) · 23.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
# <!-- rdoc-file=numeric.c -->
# Numeric is the class from which all higher-level numeric classes should
# inherit.
#
# Numeric allows instantiation of heap-allocated objects. Other core numeric
# classes such as Integer are implemented as immediates, which means that each
# Integer is a single immutable object which is always passed by value.
#
# a = 1
# 1.object_id == a.object_id #=> true
#
# There can only ever be one instance of the integer `1`, for example. Ruby
# ensures this by preventing instantiation. If duplication is attempted, the
# same instance is returned.
#
# Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
# 1.dup #=> 1
# 1.object_id == 1.dup.object_id #=> true
#
# For this reason, Numeric should be used when defining other numeric classes.
#
# Classes which inherit from Numeric must implement `coerce`, which returns a
# two-member Array containing an object that has been coerced into an instance
# of the new class and `self` (see #coerce).
#
# Inheriting classes should also implement arithmetic operator methods (`+`,
# `-`, `*` and `/`) and the `<=>` operator (see Comparable). These methods may
# rely on `coerce` to ensure interoperability with instances of other numeric
# classes.
#
# class Tally < Numeric
# def initialize(string)
# @string = string
# end
#
# def to_s
# @string
# end
#
# def to_i
# @string.size
# end
#
# def coerce(other)
# [self.class.new('|' * other.to_i), self]
# end
#
# def <=>(other)
# to_i <=> other.to_i
# end
#
# def +(other)
# self.class.new('|' * (to_i + other.to_i))
# end
#
# def -(other)
# self.class.new('|' * (to_i - other.to_i))
# end
#
# def *(other)
# self.class.new('|' * (to_i * other.to_i))
# end
#
# def /(other)
# self.class.new('|' * (to_i / other.to_i))
# end
# end
#
# tally = Tally.new('||')
# puts tally * 2 #=> "||||"
# puts tally > 1 #=> true
#
# ## What's Here
#
# First, what's elsewhere. Class Numeric:
#
# * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
# * Includes [module Comparable](rdoc-ref:Comparable@What-27s+Here).
#
# Here, class Numeric provides methods for:
#
# * [Querying](rdoc-ref:Numeric@Querying)
# * [Comparing](rdoc-ref:Numeric@Comparing)
# * [Converting](rdoc-ref:Numeric@Converting)
# * [Other](rdoc-ref:Numeric@Other)
#
# ### Querying
#
# * #finite?: Returns true unless `self` is infinite or not a number.
# * #infinite?: Returns -1, `nil` or +1, depending on whether `self` is
# `-Infinity<tt>, finite, or <tt>+Infinity`.
# * #integer?: Returns whether `self` is an integer.
# * #negative?: Returns whether `self` is negative.
# * #nonzero?: Returns whether `self` is not zero.
# * #positive?: Returns whether `self` is positive.
# * #real?: Returns whether `self` is a real value.
# * #zero?: Returns whether `self` is zero.
#
# ### Comparing
#
# * #<=>: Returns:
#
# * -1 if `self` is less than the given value.
# * 0 if `self` is equal to the given value.
# * 1 if `self` is greater than the given value.
# * `nil` if `self` and the given value are not comparable.
#
# * #eql?: Returns whether `self` and the given value have the same value and
# type.
#
# ### Converting
#
# * #% (aliased as #modulo): Returns the remainder of `self` divided by the
# given value.
# * #-@: Returns the value of `self`, negated.
# * #abs (aliased as #magnitude): Returns the absolute value of `self`.
# * #abs2: Returns the square of `self`.
# * #angle (aliased as #arg and #phase): Returns 0 if `self` is positive,
# Math::PI otherwise.
# * #ceil: Returns the smallest number greater than or equal to `self`, to a
# given precision.
# * #coerce: Returns array `[coerced_self, coerced_other]` for the given other
# value.
# * #conj (aliased as #conjugate): Returns the complex conjugate of `self`.
# * #denominator: Returns the denominator (always positive) of the Rational
# representation of `self`.
# * #div: Returns the value of `self` divided by the given value and converted
# to an integer.
# * #divmod: Returns array `[quotient, modulus]` resulting from dividing
# `self` the given divisor.
# * #fdiv: Returns the Float result of dividing `self` by the given divisor.
# * #floor: Returns the largest number less than or equal to `self`, to a
# given precision.
# * #i: Returns the Complex object `Complex(0, self)`. the given value.
# * #imaginary (aliased as #imag): Returns the imaginary part of the `self`.
# * #numerator: Returns the numerator of the Rational representation of
# `self`; has the same sign as `self`.
# * #polar: Returns the array `[self.abs, self.arg]`.
# * #quo: Returns the value of `self` divided by the given value.
# * #real: Returns the real part of `self`.
# * #rect (aliased as #rectangular): Returns the array `[self, 0]`.
# * #remainder: Returns `self-arg*(self/arg).truncate` for the given `arg`.
# * #round: Returns the value of `self` rounded to the nearest value for the
# given a precision.
# * #to_c: Returns the Complex representation of `self`.
# * #to_int: Returns the Integer representation of `self`, truncating if
# necessary.
# * #truncate: Returns `self` truncated (toward zero) to a given precision.
#
# ### Other
#
# * #clone: Returns `self`; does not allow freezing.
# * #dup (aliased as #+@): Returns `self`.
# * #step: Invokes the given block with the sequence of specified numbers.
#
class Numeric
include Comparable
# <!--
# rdoc-file=numeric.c
# - self % other -> real_numeric
# -->
# Returns `self` modulo `other` as a real number.
#
# Of the Core and Standard Library classes, only Rational uses this
# implementation.
#
# For Rational `r` and real number `n`, these expressions are equivalent:
#
# r % n
# r-n*(r/n).floor
# r.divmod(n)[1]
#
# See Numeric#divmod.
#
# Examples:
#
# r = Rational(1, 2) # => (1/2)
# r2 = Rational(2, 3) # => (2/3)
# r % r2 # => (1/2)
# r % 2 # => (1/2)
# r % 2.0 # => 0.5
#
# r = Rational(301,100) # => (301/100)
# r2 = Rational(7,5) # => (7/5)
# r % r2 # => (21/100)
# r % -r2 # => (-119/100)
# (-r) % r2 # => (119/100)
# (-r) %-r2 # => (-21/100)
#
def %: (Numeric) -> Numeric
# Performs addition: the class of the resulting object depends on the class of
# `numeric`.
#
def +: (Numeric) -> Numeric
# <!--
# rdoc-file=numeric.rb
# - +self -> self
# -->
# Returns `self`.
#
def +@: () -> self
# Performs subtraction: the class of the resulting object depends on the class
# of `numeric`.
#
def -: (Numeric) -> Numeric
# <!--
# rdoc-file=numeric.c
# - -self -> numeric
# -->
# Unary Minus---Returns the receiver, negated.
#
def -@: () -> self
# <!--
# rdoc-file=numeric.c
# - self <=> other -> zero or nil
# -->
# Returns zero if `self` is the same as `other`, `nil` otherwise.
#
# No subclass in the Ruby Core or Standard Library uses this implementation.
#
def <=>: (Numeric other) -> Integer
# <!--
# rdoc-file=numeric.c
# - abs -> numeric
# -->
# Returns the absolute value of `self`.
#
# 12.abs #=> 12
# (-34.56).abs #=> 34.56
# -34.56.abs #=> 34.56
#
def abs: () -> Numeric
# <!--
# rdoc-file=complex.c
# - abs2 -> real
# -->
# Returns the square of `self`.
#
def abs2: () -> self
# <!-- rdoc-file=complex.c -->
# Returns zero if `self` is positive, Math::PI otherwise.
#
def angle: () -> (0 | Float)
# <!--
# rdoc-file=complex.c
# - arg -> 0 or Math::PI
# -->
# Returns zero if `self` is positive, Math::PI otherwise.
#
alias arg angle
# <!--
# rdoc-file=numeric.c
# - ceil(ndigits = 0) -> float or integer
# -->
# Returns the smallest float or integer that is greater than or equal to `self`,
# as specified by the given `ndigits`, which must be an [integer-convertible
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
#
# Equivalent to `self.to_f.ceil(ndigits)`.
#
# Related: #floor, Float#ceil.
#
def ceil: () -> Integer
| (Integer digits) -> (Integer | Numeric)
# <!--
# rdoc-file=numeric.c
# - coerce(other) -> array
# -->
# Returns a 2-element array containing two numeric elements, formed from the two
# operands `self` and `other`, of a common compatible type.
#
# Of the Core and Standard Library classes, Integer, Rational, and Complex use
# this implementation.
#
# Examples:
#
# i = 2 # => 2
# i.coerce(3) # => [3, 2]
# i.coerce(3.0) # => [3.0, 2.0]
# i.coerce(Rational(1, 2)) # => [0.5, 2.0]
# i.coerce(Complex(3, 4)) # Raises RangeError.
#
# r = Rational(5, 2) # => (5/2)
# r.coerce(2) # => [(2/1), (5/2)]
# r.coerce(2.0) # => [2.0, 2.5]
# r.coerce(Rational(2, 3)) # => [(2/3), (5/2)]
# r.coerce(Complex(3, 4)) # => [(3+4i), ((5/2)+0i)]
#
# c = Complex(2, 3) # => (2+3i)
# c.coerce(2) # => [(2+0i), (2+3i)]
# c.coerce(2.0) # => [(2.0+0i), (2+3i)]
# c.coerce(Rational(1, 2)) # => [((1/2)+0i), (2+3i)]
# c.coerce(Complex(3, 4)) # => [(3+4i), (2+3i)]
#
# Raises an exception if any type conversion fails.
#
def coerce: (Numeric) -> [ Numeric, Numeric ]
# <!--
# rdoc-file=numeric.rb
# - conj -> self
# -->
# Returns `self`.
#
def conjugate: () -> self
# <!--
# rdoc-file=numeric.rb
# - conj()
# -->
#
alias conj conjugate
# <!--
# rdoc-file=rational.c
# - num.denominator -> integer
# -->
# Returns the denominator (always positive).
#
def denominator: () -> Integer
# <!--
# rdoc-file=numeric.c
# - div(other) -> integer
# -->
# Returns the quotient `self/other` as an integer (via `floor`), using method
# `/` in the derived class of `self`. (Numeric itself does not define method
# `/`.)
#
# Of the Core and Standard Library classes, Only Float and Rational use this
# implementation.
#
def div: (Numeric) -> Integer
# <!--
# rdoc-file=numeric.c
# - divmod(other) -> array
# -->
# Returns a 2-element array `[q, r]`, where
#
# q = (self/other).floor # Quotient
# r = self % other # Remainder
#
# Of the Core and Standard Library classes, only Rational uses this
# implementation.
#
# Examples:
#
# Rational(11, 1).divmod(4) # => [2, (3/1)]
# Rational(11, 1).divmod(-4) # => [-3, (-1/1)]
# Rational(-11, 1).divmod(4) # => [-3, (1/1)]
# Rational(-11, 1).divmod(-4) # => [2, (-3/1)]
#
# Rational(12, 1).divmod(4) # => [3, (0/1)]
# Rational(12, 1).divmod(-4) # => [-3, (0/1)]
# Rational(-12, 1).divmod(4) # => [-3, (0/1)]
# Rational(-12, 1).divmod(-4) # => [3, (0/1)]
#
# Rational(13, 1).divmod(4.0) # => [3, 1.0]
# Rational(13, 1).divmod(Rational(4, 11)) # => [35, (3/11)]
#
def divmod: (Numeric) -> [ Numeric, Numeric ]
# <!--
# rdoc-file=numeric.c
# - eql?(other) -> true or false
# -->
# Returns `true` if `self` and `other` are the same type and have equal values.
#
# Of the Core and Standard Library classes, only Integer, Rational, and Complex
# use this implementation.
#
# Examples:
#
# 1.eql?(1) # => true
# 1.eql?(1.0) # => false
# 1.eql?(Rational(1, 1)) # => false
# 1.eql?(Complex(1, 0)) # => false
#
# Method `eql?` is different from `==` in that `eql?` requires matching types,
# while `==` does not.
#
def eql?: (untyped) -> bool
# <!--
# rdoc-file=numeric.c
# - fdiv(other) -> float
# -->
# Returns the quotient `self/other` as a float, using method `/` in the derived
# class of `self`. (Numeric itself does not define method `/`.)
#
# Of the Core and Standard Library classes, only BigDecimal uses this
# implementation.
#
def fdiv: (Numeric) -> Numeric
# <!--
# rdoc-file=numeric.rb
# - finite? -> true or false
# -->
# Returns `true` if `self` is a finite number, `false` otherwise.
#
def finite?: () -> bool
# <!--
# rdoc-file=numeric.c
# - floor(ndigits = 0) -> float or integer
# -->
# Returns the largest float or integer that is less than or equal to `self`, as
# specified by the given `ndigits`, which must be an [integer-convertible
# object](rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects).
#
# Equivalent to `self.to_f.floor(ndigits)`.
#
# Related: #ceil, Float#floor.
#
def floor: () -> Integer
| (Integer digits) -> Numeric
# <!--
# rdoc-file=numeric.c
# - i -> complex
# -->
# Returns `Complex(0, self)`:
#
# 2.i # => (0+2i)
# -2.i # => (0-2i)
# 2.0.i # => (0+2.0i)
# Rational(1, 2).i # => (0+(1/2)*i)
# Complex(3, 4).i # Raises NoMethodError.
#
def i: () -> Complex
# <!--
# rdoc-file=numeric.rb
# - imag -> 0
# -->
# Returns zero.
#
def imaginary: () -> 0
# <!--
# rdoc-file=numeric.rb
# - imag()
# -->
#
alias imag imaginary
# <!--
# rdoc-file=numeric.rb
# - infinite? -> -1, 1, or nil
# -->
# Returns `nil`, -1, or 1 depending on whether `self` is finite, `-Infinity`, or
# `+Infinity`.
#
def infinite?: () -> Integer?
# <!--
# rdoc-file=numeric.rb
# - integer? -> true or false
# -->
# Returns `true` if `self` is an Integer.
#
# 1.0.integer? # => false
# 1.integer? # => true
#
def integer?: () -> bool
# <!-- rdoc-file=numeric.c -->
# Returns the absolute value of `self`.
#
# 12.abs #=> 12
# (-34.56).abs #=> 34.56
# -34.56.abs #=> 34.56
#
alias magnitude abs
# <!-- rdoc-file=numeric.c -->
# Returns `self` modulo `other` as a real number.
#
# Of the Core and Standard Library classes, only Rational uses this
# implementation.
#
# For Rational `r` and real number `n`, these expressions are equivalent:
#
# r % n
# r-n*(r/n).floor
# r.divmod(n)[1]
#
# See Numeric#divmod.
#
# Examples:
#
# r = Rational(1, 2) # => (1/2)
# r2 = Rational(2, 3) # => (2/3)
# r % r2 # => (1/2)
# r % 2 # => (1/2)
# r % 2.0 # => 0.5
#
# r = Rational(301,100) # => (301/100)
# r2 = Rational(7,5) # => (7/5)
# r % r2 # => (21/100)
# r % -r2 # => (-119/100)
# (-r) % r2 # => (119/100)
# (-r) %-r2 # => (-21/100)
#
def modulo: (Numeric) -> Numeric
# <!--
# rdoc-file=numeric.c
# - negative? -> true or false
# -->
# Returns `true` if `self` is less than 0, `false` otherwise.
#
def negative?: () -> bool
# <!--
# rdoc-file=numeric.c
# - nonzero? -> self or nil
# -->
# Returns +self+ if +self+ is not a zero value, +nil+ otherwise;
# uses method <tt>zero?</tt> for the evaluation.
#
# The returned +self+ allows the method to be chained:
#
# a = %w[z Bb bB bb BB a aA Aa AA A]
# a.sort {|a, b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
# # => ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
#
# Of the Core and Standard Library classes,
# Integer, Float, Rational, and Complex use this implementation.
#
# Related: #zero?
#
def nonzero?: () -> self?
# <!--
# rdoc-file=rational.c
# - num.numerator -> integer
# -->
# Returns the numerator.
#
def numerator: () -> Numeric
# <!-- rdoc-file=complex.c -->
# Returns zero if `self` is positive, Math::PI otherwise.
#
alias phase angle
# <!--
# rdoc-file=complex.c
# - polar -> array
# -->
# Returns array `[self.abs, self.arg]`.
#
def polar: () -> [ Numeric, Numeric ]
# <!--
# rdoc-file=numeric.c
# - positive? -> true or false
# -->
# Returns `true` if `self` is greater than 0, `false` otherwise.
#
def positive?: () -> bool
# <!--
# rdoc-file=rational.c
# - num.quo(int_or_rat) -> rat
# - num.quo(flo) -> flo
# -->
# Returns the most exact division (rational for integers, float for floats).
#
def quo: (Numeric) -> Numeric
# <!--
# rdoc-file=numeric.rb
# - real -> self
# -->
# Returns `self`.
#
def real: () -> self
# <!--
# rdoc-file=numeric.rb
# - real? -> true or false
# -->
# Returns `true` if `self` is a real number (i.e. not Complex).
#
def real?: () -> true
# <!-- rdoc-file=complex.c -->
# Returns array `[self, 0]`.
#
def rect: () -> [ Numeric, Numeric ]
# <!--
# rdoc-file=complex.c
# - rect -> array
# -->
# Returns array `[self, 0]`.
#
alias rectangular rect
# <!--
# rdoc-file=numeric.c
# - remainder(other) -> real_number
# -->
# Returns the remainder after dividing `self` by `other`.
#
# Of the Core and Standard Library classes, only Float and Rational use this
# implementation.
#
# Examples:
#
# 11.0.remainder(4) # => 3.0
# 11.0.remainder(-4) # => 3.0
# -11.0.remainder(4) # => -3.0
# -11.0.remainder(-4) # => -3.0
#
# 12.0.remainder(4) # => 0.0
# 12.0.remainder(-4) # => 0.0
# -12.0.remainder(4) # => -0.0
# -12.0.remainder(-4) # => -0.0
#
# 13.0.remainder(4.0) # => 1.0
# 13.0.remainder(Rational(4, 1)) # => 1.0
#
# Rational(13, 1).remainder(4) # => (1/1)
# Rational(13, 1).remainder(-4) # => (1/1)
# Rational(-13, 1).remainder(4) # => (-1/1)
# Rational(-13, 1).remainder(-4) # => (-1/1)
#
def remainder: (Numeric) -> Numeric
# <!--
# rdoc-file=numeric.c
# - round(digits = 0) -> integer or float
# -->
# Returns `self` rounded to the nearest value with a precision of `digits`
# decimal digits.
#
# Numeric implements this by converting `self` to a Float and invoking
# Float#round.
#
def round: () -> Integer
| (Integer digits) -> Numeric
# <!--
# rdoc-file=numeric.c
# - step(to = nil, by = 1) {|n| ... } -> self
# - step(to = nil, by = 1) -> enumerator
# - step(to = nil, by: 1) {|n| ... } -> self
# - step(to = nil, by: 1) -> enumerator
# - step(by: 1, to: ) {|n| ... } -> self
# - step(by: 1, to: ) -> enumerator
# - step(by: , to: nil) {|n| ... } -> self
# - step(by: , to: nil) -> enumerator
# -->
# Generates a sequence of numbers; with a block given, traverses the sequence.
#
# Of the Core and Standard Library classes, Integer, Float, and Rational use
# this implementation.
#
# A quick example:
#
# squares = []
# 1.step(by: 2, to: 10) {|i| squares.push(i*i) }
# squares # => [1, 9, 25, 49, 81]
#
# The generated sequence:
#
# * Begins with `self`.
# * Continues at intervals of `by` (which may not be zero).
# * Ends with the last number that is within or equal to `to`; that is, less
# than or equal to `to` if `by` is positive, greater than or equal to `to`
# if `by` is negative. If `to` is `nil`, the sequence is of infinite length.
#
# If a block is given, calls the block with each number in the sequence; returns
# `self`. If no block is given, returns an Enumerator::ArithmeticSequence.
#
# **Keyword Arguments**
#
# With keyword arguments `by` and `to`, their values (or defaults) determine the
# step and limit:
#
# # Both keywords given.
# squares = []
# 4.step(by: 2, to: 10) {|i| squares.push(i*i) } # => 4
# squares # => [16, 36, 64, 100]
# cubes = []
# 3.step(by: -1.5, to: -3) {|i| cubes.push(i*i*i) } # => 3
# cubes # => [27.0, 3.375, 0.0, -3.375, -27.0]
# squares = []
# 1.2.step(by: 0.2, to: 2.0) {|f| squares.push(f*f) }
# squares # => [1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]
#
# squares = []
# Rational(6/5).step(by: 0.2, to: 2.0) {|r| squares.push(r*r) }
# squares # => [1.0, 1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]
#
# # Only keyword to given.
# squares = []
# 4.step(to: 10) {|i| squares.push(i*i) } # => 4
# squares # => [16, 25, 36, 49, 64, 81, 100]
# # Only by given.
#
# # Only keyword by given
# squares = []
# 4.step(by:2) {|i| squares.push(i*i); break if i > 10 }
# squares # => [16, 36, 64, 100, 144]
#
# # No block given.
# e = 3.step(by: -1.5, to: -3) # => (3.step(by: -1.5, to: -3))
# e.class # => Enumerator::ArithmeticSequence
#
# **Positional Arguments**
#
# With optional positional arguments `to` and `by`, their values (or defaults)
# determine the step and limit:
#
# squares = []
# 4.step(10, 2) {|i| squares.push(i*i) } # => 4
# squares # => [16, 36, 64, 100]
# squares = []
# 4.step(10) {|i| squares.push(i*i) }
# squares # => [16, 25, 36, 49, 64, 81, 100]
# squares = []
# 4.step {|i| squares.push(i*i); break if i > 10 } # => nil
# squares # => [16, 25, 36, 49, 64, 81, 100, 121]
#
# **Implementation Notes**
#
# If all the arguments are integers, the loop operates using an integer counter.
#
# If any of the arguments are floating point numbers, all are converted to
# floats, and the loop is executed *floor(n + n*Float::EPSILON) + 1* times,
# where *n = (limit - self)/step*.
#
def step: (?Numeric limit, ?Numeric step) { (Numeric) -> void } -> self
| (?Numeric limit, ?Numeric step) -> Enumerator::ArithmeticSequence
| (?by: Numeric, ?to: Numeric) { (Numeric) -> void } -> self
| (?by: Numeric, ?to: Numeric) -> Enumerator::ArithmeticSequence
# <!--
# rdoc-file=complex.c
# - to_c -> complex
# -->
# Returns `self` as a Complex object.
#
def to_c: () -> Complex
# <!--
# rdoc-file=numeric.c
# - to_int -> integer
# -->
# Returns `self` as an integer; converts using method `to_i` in the derived
# class.
#
# Of the Core and Standard Library classes, only Rational and Complex use this
# implementation.
#
# Examples:
#
# Rational(1, 2).to_int # => 0
# Rational(2, 1).to_int # => 2
# Complex(2, 0).to_int # => 2
# Complex(2, 1).to_int # Raises RangeError (non-zero imaginary part)
#
def to_int: () -> Integer
# <!--
# rdoc-file=numeric.c
# - truncate(digits = 0) -> integer or float
# -->
# Returns `self` truncated (toward zero) to a precision of `digits` decimal
# digits.
#
# Numeric implements this by converting `self` to a Float and invoking
# Float#truncate.
#
def truncate: () -> Integer
| (Integer ndigits) -> (Integer | Numeric)
# <!--
# rdoc-file=numeric.c
# - zero? -> true or false
# -->
# Returns `true` if `zero` has a zero value, `false` otherwise.
#
# Of the Core and Standard Library classes, only Rational and Complex use this
# implementation.
#
def zero?: () -> bool
# <!--
# rdoc-file=numeric.c
# - clone(freeze: true) -> self
# -->
# Returns `self`.
#
# Raises an exception if the value for `freeze` is neither `true` nor `nil`.
#
# Related: Numeric#dup.
#
def clone: (?freeze: true?) -> self
end