Class: Number
Instance Method Summary
collapse
Methods inherited from Numeric
#__coerced__, #clone, #conj, #div, #dup, #i, #imag, #polar, #real, #real?, #rect, #to_c
Methods included from Comparable
#between?, normalize
Instance Method Details
#%(other) ⇒ Object
Also known as:
modulo
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'opal/opal/corelib/number.rb', line 80
def %(other)
%x{
if (other.$$is_number) {
if (other == -Infinity) {
return other;
}
else if (other == 0) {
#{raise ZeroDivisionError, "divided by 0"};
}
else if (other < 0 || self < 0) {
return (self % other + other) % other;
}
else {
return self % other;
}
}
else {
return #{__coerced__ :%, other};
}
}
end
|
102
103
104
105
106
107
108
109
110
111
|
# File 'opal/opal/corelib/number.rb', line 102
def &(other)
%x{
if (other.$$is_number) {
return self & other;
}
else {
return #{__coerced__ :&, other};
}
}
end
|
56
57
58
59
60
61
62
63
64
65
|
# File 'opal/opal/corelib/number.rb', line 56
def *(other)
%x{
if (other.$$is_number) {
return self * other;
}
else {
return #{__coerced__ :*, other};
}
}
end
|
#**(other) ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'opal/opal/corelib/number.rb', line 248
def **(other)
if Integer === other
if !(Integer === self) || other > 0
`Math.pow(self, other)`
else
Rational.new(self, 1) ** other
end
elsif self < 0 && (Float === other || Rational === other)
Complex.new(self, 0) ** other.to_f
elsif `other.$$is_number != null`
`Math.pow(self, other)`
else
__coerced__ :**, other
end
end
|
34
35
36
37
38
39
40
41
42
43
|
# File 'opal/opal/corelib/number.rb', line 34
def +(other)
%x{
if (other.$$is_number) {
return self + other;
}
else {
return #{__coerced__ :+, other};
}
}
end
|
236
237
238
|
# File 'opal/opal/corelib/number.rb', line 236
def +@
`+self`
end
|
45
46
47
48
49
50
51
52
53
54
|
# File 'opal/opal/corelib/number.rb', line 45
def -(other)
%x{
if (other.$$is_number) {
return self - other;
}
else {
return #{__coerced__ :-, other};
}
}
end
|
240
241
242
|
# File 'opal/opal/corelib/number.rb', line 240
def -@
`-self`
end
|
#/(other) ⇒ Object
Also known as:
fdiv
67
68
69
70
71
72
73
74
75
76
|
# File 'opal/opal/corelib/number.rb', line 67
def /(other)
%x{
if (other.$$is_number) {
return self / other;
}
else {
return #{__coerced__ :/, other};
}
}
end
|
135
136
137
138
139
140
141
142
143
144
|
# File 'opal/opal/corelib/number.rb', line 135
def <(other)
%x{
if (other.$$is_number) {
return self < other;
}
else {
return #{__coerced__ :<, other};
}
}
end
|
#<<(count) ⇒ Object
210
211
212
213
214
|
# File 'opal/opal/corelib/number.rb', line 210
def <<(count)
count = Opal.coerce_to! count, Integer, :to_int
`#{count} > 0 ? self << #{count} : self >> -#{count}`
end
|
#<=(other) ⇒ Object
146
147
148
149
150
151
152
153
154
155
|
# File 'opal/opal/corelib/number.rb', line 146
def <=(other)
%x{
if (other.$$is_number) {
return self <= other;
}
else {
return #{__coerced__ :<=, other};
}
}
end
|
#<=>(other) ⇒ Object
202
203
204
205
206
207
208
|
# File 'opal/opal/corelib/number.rb', line 202
def <=>(other)
%x{
return spaceship_operator(self, other);
}
rescue ArgumentError
nil
end
|
#==(other) ⇒ Object
Also known as:
eql?
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# File 'opal/opal/corelib/number.rb', line 264
def ==(other)
%x{
if (other.$$is_number) {
return self == Number(other);
}
else if (#{other.respond_to? :==}) {
return #{other == self};
}
else {
return false;
}
}
end
|
157
158
159
160
161
162
163
164
165
166
|
# File 'opal/opal/corelib/number.rb', line 157
def >(other)
%x{
if (other.$$is_number) {
return self > other;
}
else {
return #{__coerced__ :>, other};
}
}
end
|
#>=(other) ⇒ Object
168
169
170
171
172
173
174
175
176
177
|
# File 'opal/opal/corelib/number.rb', line 168
def >=(other)
%x{
if (other.$$is_number) {
return self >= other;
}
else {
return #{__coerced__ :>=, other};
}
}
end
|
#>>(count) ⇒ Object
216
217
218
219
220
|
# File 'opal/opal/corelib/number.rb', line 216
def >>(count)
count = Opal.coerce_to! count, Integer, :to_int
`#{count} > 0 ? self >> #{count} : self << -#{count}`
end
|
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'opal/opal/corelib/number.rb', line 222
def [](bit)
bit = Opal.coerce_to! bit, Integer, :to_int
%x{
if (#{bit} < 0) {
return 0;
}
if (#{bit} >= 32) {
return #{ self } < 0 ? 1 : 0;
}
return (self >> #{bit}) & 1;
}
end
|
124
125
126
127
128
129
130
131
132
133
|
# File 'opal/opal/corelib/number.rb', line 124
def ^(other)
%x{
if (other.$$is_number) {
return self ^ other;
}
else {
return #{__coerced__ :^, other};
}
}
end
|
#__id__ ⇒ Object
Also known as:
object_id
28
29
30
|
# File 'opal/opal/corelib/number.rb', line 28
def __id__
`(self * 2) + 1`
end
|
#abs ⇒ Object
Also known as:
magnitude
278
279
280
|
# File 'opal/opal/corelib/number.rb', line 278
def abs
`Math.abs(self)`
end
|
282
283
284
|
# File 'opal/opal/corelib/number.rb', line 282
def abs2
`Math.abs(self * self)`
end
|
#angle ⇒ Object
Also known as:
arg, phase
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# File 'opal/opal/corelib/number.rb', line 286
def angle
return self if nan?
%x{
if (self == 0) {
if (1 / self > 0) {
return 0;
}
else {
return Math.PI;
}
}
else if (self < 0) {
return Math.PI;
}
else {
return 0;
}
}
end
|
#bit_length ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
# File 'opal/opal/corelib/number.rb', line 310
def bit_length
unless Integer === self
raise NoMethodError.new("undefined method `bit_length` for #{self}:Float", 'bit_length')
end
%x{
if (self === 0 || self === -1) {
return 0;
}
var result = 0,
value = self < 0 ? ~self : self;
while (value != 0) {
result += 1;
value >>>= 1;
}
return result;
}
end
|
332
333
334
|
# File 'opal/opal/corelib/number.rb', line 332
def ceil
`Math.ceil(self)`
end
|
#chr(encoding = undefined) ⇒ Object
336
337
338
|
# File 'opal/opal/corelib/number.rb', line 336
def chr(encoding = undefined)
`String.fromCharCode(self)`
end
|
#coerce(other) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'opal/opal/corelib/number.rb', line 8
def coerce(other)
%x{
if (other === nil) {
#{raise TypeError, "can't convert #{other.class} into Float"};
}
else if (other.$$is_string) {
return [#{Float(other)}, self];
}
else if (#{other.respond_to?(:to_f)}) {
return [#{Opal.coerce_to!(other, Float, :to_f)}, self];
}
else if (other.$$is_number) {
return [other, self];
}
else {
#{raise TypeError, "can't convert #{other.class} into Float"};
}
}
end
|
#denominator ⇒ Object
340
341
342
343
344
345
346
|
# File 'opal/opal/corelib/number.rb', line 340
def denominator
if nan? || infinite?
1
else
super
end
end
|
#divmod(other) ⇒ Object
648
649
650
651
652
653
654
655
656
|
# File 'opal/opal/corelib/number.rb', line 648
def divmod(other)
if nan? || other.nan?
raise FloatDomainError, "NaN"
elsif infinite?
raise FloatDomainError, "Infinity"
else
super
end
end
|
#downto(stop, &block) ⇒ Object
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# File 'opal/opal/corelib/number.rb', line 348
def downto(stop, &block)
return enum_for(:downto, stop){
raise ArgumentError, "comparison of #{self.class} with #{stop.class} failed" unless Numeric === stop
stop > self ? 0 : self - stop + 1
} unless block_given?
%x{
if (!stop.$$is_number) {
#{raise ArgumentError, "comparison of #{self.class} with #{stop.class} failed"}
}
for (var i = self; i >= stop; i--) {
block(i);
}
}
self
end
|
#equal?(other) ⇒ Boolean
368
369
370
|
# File 'opal/opal/corelib/number.rb', line 368
def equal?(other)
self == other || `isNaN(self) && isNaN(other)`
end
|
372
373
374
|
# File 'opal/opal/corelib/number.rb', line 372
def even?
`self % 2 === 0`
end
|
689
690
691
|
# File 'opal/opal/corelib/number.rb', line 689
def finite?
`self != Infinity && self != -Infinity && !isNaN(self)`
end
|
376
377
378
|
# File 'opal/opal/corelib/number.rb', line 376
def floor
`Math.floor(self)`
end
|
#gcd(other) ⇒ Object
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
# File 'opal/opal/corelib/number.rb', line 380
def gcd(other)
unless Integer === other
raise TypeError, 'not an integer'
end
%x{
var min = Math.abs(self),
max = Math.abs(other);
while (min > 0) {
var tmp = min;
min = max % min;
max = tmp;
}
return max;
}
end
|
#gcdlcm(other) ⇒ Object
400
401
402
|
# File 'opal/opal/corelib/number.rb', line 400
def gcdlcm(other)
[gcd, lcm]
end
|
693
694
695
696
697
698
699
700
701
702
703
704
705
|
# File 'opal/opal/corelib/number.rb', line 693
def infinite?
%x{
if (self == Infinity) {
return +1;
}
else if (self == -Infinity) {
return -1;
}
else {
return nil;
}
}
end
|
#instance_of?(klass) ⇒ Boolean
418
419
420
421
422
423
424
|
# File 'opal/opal/corelib/number.rb', line 418
def instance_of?(klass)
return true if klass == Fixnum && Integer === self
return true if klass == Integer && Integer === self
return true if klass == Float && Float === self
super
end
|
404
405
406
|
# File 'opal/opal/corelib/number.rb', line 404
def integer?
`self % 1 === 0`
end
|
#is_a?(klass) ⇒ Boolean
Also known as:
kind_of?
408
409
410
411
412
413
414
|
# File 'opal/opal/corelib/number.rb', line 408
def is_a?(klass)
return true if klass == Fixnum && Integer === self
return true if klass == Integer && Integer === self
return true if klass == Float && Float === self
super
end
|
#lcm(other) ⇒ Object
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
# File 'opal/opal/corelib/number.rb', line 426
def lcm(other)
unless Integer === other
raise TypeError, 'not an integer'
end
%x{
if (self == 0 || other == 0) {
return 0;
}
else {
return Math.abs(self * other / #{gcd(other)});
}
}
end
|
685
686
687
|
# File 'opal/opal/corelib/number.rb', line 685
def nan?
`isNaN(self)`
end
|
711
712
713
|
# File 'opal/opal/corelib/number.rb', line 711
def negative?
`self == -Infinity || 1 / self < 0`
end
|
#next ⇒ Object
Also known as:
succ
445
446
447
|
# File 'opal/opal/corelib/number.rb', line 445
def next
`self + 1`
end
|
449
450
451
|
# File 'opal/opal/corelib/number.rb', line 449
def nonzero?
`self == 0 ? nil : self`
end
|
#numerator ⇒ Object
453
454
455
456
457
458
459
|
# File 'opal/opal/corelib/number.rb', line 453
def numerator
if nan? || infinite?
self
else
super
end
end
|
461
462
463
|
# File 'opal/opal/corelib/number.rb', line 461
def odd?
`self % 2 !== 0`
end
|
465
466
467
|
# File 'opal/opal/corelib/number.rb', line 465
def ord
self
end
|
707
708
709
|
# File 'opal/opal/corelib/number.rb', line 707
def positive?
`self == Infinity || 1 / self > 0`
end
|
469
470
471
|
# File 'opal/opal/corelib/number.rb', line 469
def pred
`self - 1`
end
|
#quo(other) ⇒ Object
473
474
475
476
477
478
479
|
# File 'opal/opal/corelib/number.rb', line 473
def quo(other)
if Integer === self
super
else
self / other
end
end
|
#rationalize(eps = undefined) ⇒ Object
#round(ndigits = undefined) ⇒ Object
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
|
# File 'opal/opal/corelib/number.rb', line 505
def round(ndigits = undefined)
if Integer === self
if `ndigits == null`
return self
end
if Float === ndigits && ndigits.infinite?
raise RangeError, "Infinity"
end
ndigits = Opal.coerce_to!(ndigits, Integer, :to_int)
if ndigits < Integer::MIN
raise RangeError, "out of bounds"
end
if `ndigits >= 0`
return self
end
ndigits = -ndigits;
%x{
if (0.415241 * ndigits - 0.125 > #{size}) {
return 0;
}
var f = Math.pow(10, ndigits),
x = Math.floor((Math.abs(x) + f / 2) / f) * f;
return self < 0 ? -x : x;
}
else
if nan? && `ndigits == null`
raise FloatDomainError, "NaN"
end
ndigits = Opal.coerce_to!(`ndigits || 0`, Integer, :to_int)
if ndigits <= 0
if nan?
raise RangeError, "NaN"
elsif infinite?
raise FloatDomainError, "Infinity"
end
elsif ndigits == 0
return `Math.round(self)`
elsif nan? || infinite?
return self
end
_, exp = Math.frexp(self)
if ndigits >= (Float::DIG + 2) - (exp > 0 ? exp / 4 : exp / 3 - 1)
return self
end
if ndigits < -(exp > 0 ? exp / 3 + 1 : exp / 4)
return 0
end
`Math.round(self * Math.pow(10, ndigits)) / Math.pow(10, ndigits)`
end
end
|
Since bitwise operations are 32 bit, declare it to be so.
681
682
683
|
# File 'opal/opal/corelib/number.rb', line 681
def size
4
end
|
#step(limit, step = 1, &block) ⇒ Object
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
|
# File 'opal/opal/corelib/number.rb', line 570
def step(limit, step = 1, &block)
return enum_for :step, limit, step unless block
raise ArgumentError, 'step cannot be 0' if `step == 0`
%x{
var value = self;
if (limit === Infinity || limit === -Infinity) {
block(value);
return self;
}
if (step > 0) {
while (value <= limit) {
block(value);
value += step;
}
}
else {
while (value >= limit) {
block(value);
value += step;
}
}
}
self
end
|
#times(&block) ⇒ Object
602
603
604
605
606
607
608
609
610
611
612
|
# File 'opal/opal/corelib/number.rb', line 602
def times(&block)
return enum_for(:times) { self } unless block
%x{
for (var i = 0; i < self; i++) {
block(i);
}
}
self
end
|
614
615
616
|
# File 'opal/opal/corelib/number.rb', line 614
def to_f
self
end
|
#to_i ⇒ Object
Also known as:
to_int, truncate
618
619
620
|
# File 'opal/opal/corelib/number.rb', line 618
def to_i
`parseInt(self, 10)`
end
|
#to_s(base = 10) ⇒ Object
Also known as:
inspect
636
637
638
639
640
641
642
|
# File 'opal/opal/corelib/number.rb', line 636
def to_s(base = 10)
if base < 2 || base > 36
raise ArgumentError, 'base must be between 2 and 36'
end
`self.toString(base)`
end
|
#upto(stop, &block) ⇒ Object
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
# File 'opal/opal/corelib/number.rb', line 658
def upto(stop, &block)
return enum_for(:upto, stop){
raise ArgumentError, "comparison of #{self.class} with #{stop.class} failed" unless Numeric === stop
stop < self ? 0 : stop - self + 1
} unless block_given?
%x{
if (!stop.$$is_number) {
#{raise ArgumentError, "comparison of #{self.class} with #{stop.class} failed"}
}
for (var i = self; i <= stop; i++) {
block(i);
}
}
self
end
|
676
677
678
|
# File 'opal/opal/corelib/number.rb', line 676
def zero?
`self == 0`
end
|
113
114
115
116
117
118
119
120
121
122
|
# File 'opal/opal/corelib/number.rb', line 113
def |(other)
%x{
if (other.$$is_number) {
return self | other;
}
else {
return #{__coerced__ :|, other};
}
}
end
|
244
245
246
|
# File 'opal/opal/corelib/number.rb', line 244
def ~
`~self`
end
|