Class: Number
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Numeric
#__coerced__, #clone, #conj, #div, #dup, #i, #imag, #polar, #real, #real?, #rect, #step, #to_c
Methods included from Comparable
#between?, #clamp
Class Method Details
13
14
15
|
# File 'opal/opal/corelib/number.rb', line 13
def allocate
::Kernel.raise ::TypeError, "allocator undefined for #{name}"
end
|
Instance Method Details
#%(other) ⇒ Object
Also known as:
modulo
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'opal/opal/corelib/number.rb', line 113
def %(other)
%x{
if (other.$$is_number) {
if (other == -Infinity) {
return other;
}
else if (other == 0) {
#{::Kernel.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
|
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
|
91
92
93
94
95
96
97
98
99
100
|
# File 'opal/opal/corelib/number.rb', line 91
def *(other)
%x{
if (other.$$is_number) {
return self * other;
}
else {
return #{__coerced__ :*, other};
}
}
end
|
#**(other) ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'opal/opal/corelib/number.rb', line 279
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
|
69
70
71
72
73
74
75
76
77
78
|
# File 'opal/opal/corelib/number.rb', line 69
def +(other)
%x{
if (other.$$is_number) {
return self + other;
}
else {
return #{__coerced__ :+, other};
}
}
end
|
267
268
269
|
# File 'opal/opal/corelib/number.rb', line 267
def +@
`+self`
end
|
80
81
82
83
84
85
86
87
88
89
|
# File 'opal/opal/corelib/number.rb', line 80
def -(other)
%x{
if (other.$$is_number) {
return self - other;
}
else {
return #{__coerced__ :-, other};
}
}
end
|
271
272
273
|
# File 'opal/opal/corelib/number.rb', line 271
def -@
`-self`
end
|
#/(other) ⇒ Object
Also known as:
fdiv
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
|
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
241
242
243
244
245
|
# File 'opal/opal/corelib/number.rb', line 241
def <<(count)
count = ::Opal.coerce_to! count, ::Integer, :to_int
`#{count} > 0 ? self << #{count} : self >> -#{count}`
end
|
#<=(other) ⇒ Object
179
180
181
182
183
184
185
186
187
188
|
# File 'opal/opal/corelib/number.rb', line 179
def <=(other)
%x{
if (other.$$is_number) {
return self <= other;
}
else {
return #{__coerced__ :<=, other};
}
}
end
|
#<=>(other) ⇒ Object
235
236
237
238
239
|
# File 'opal/opal/corelib/number.rb', line 235
def <=>(other)
`spaceship_operator(self, other)`
rescue ::ArgumentError
nil
end
|
#==(other) ⇒ Object
Also known as:
===, eql?
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'opal/opal/corelib/number.rb', line 295
def ==(other)
%x{
if (other.$$is_number) {
return self.valueOf() === other.valueOf();
}
else if (#{other.respond_to? :==}) {
return #{other == self};
}
else {
return false;
}
}
end
|
190
191
192
193
194
195
196
197
198
199
|
# File 'opal/opal/corelib/number.rb', line 190
def >(other)
%x{
if (other.$$is_number) {
return self > other;
}
else {
return #{__coerced__ :>, other};
}
}
end
|
#>=(other) ⇒ Object
201
202
203
204
205
206
207
208
209
210
|
# File 'opal/opal/corelib/number.rb', line 201
def >=(other)
%x{
if (other.$$is_number) {
return self >= other;
}
else {
return #{__coerced__ :>=, other};
}
}
end
|
#>>(count) ⇒ Object
247
248
249
250
251
|
# File 'opal/opal/corelib/number.rb', line 247
def >>(count)
count = ::Opal.coerce_to! count, ::Integer, :to_int
`#{count} > 0 ? self >> #{count} : self << -#{count}`
end
|
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'opal/opal/corelib/number.rb', line 253
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
|
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
|
#__id__ ⇒ Object
Also known as:
object_id
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'opal/opal/corelib/number.rb', line 40
def __id__
%x{
// Binary-safe integers
if (self|0 === self) {
return (self * 2) + 1;
}
else {
if (number_id_map.has(self)) {
return number_id_map.get(self);
}
var id = Opal.uid();
number_id_map.set(self, id);
return id;
}
}
end
|
#abs ⇒ Object
Also known as:
magnitude
311
312
313
|
# File 'opal/opal/corelib/number.rb', line 311
def abs
`Math.abs(self)`
end
|
315
316
317
|
# File 'opal/opal/corelib/number.rb', line 315
def abs2
`Math.abs(self * self)`
end
|
#allbits?(mask) ⇒ Boolean
319
320
321
322
|
# File 'opal/opal/corelib/number.rb', line 319
def allbits?(mask)
mask = ::Opal.coerce_to! mask, ::Integer, :to_int
`(self & mask) == mask`
end
|
#angle ⇒ Object
Also known as:
arg, phase
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# File 'opal/opal/corelib/number.rb', line 329
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
|
#anybits?(mask) ⇒ Boolean
324
325
326
327
|
# File 'opal/opal/corelib/number.rb', line 324
def anybits?(mask)
mask = ::Opal.coerce_to! mask, ::Integer, :to_int
`(self & mask) !== 0`
end
|
#bit_length ⇒ Object
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
# File 'opal/opal/corelib/number.rb', line 350
def bit_length
unless ::Integer === self
::Kernel.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
|
#ceil(ndigits = 0) ⇒ Object
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'opal/opal/corelib/number.rb', line 372
def ceil(ndigits = 0)
%x{
var f = #{to_f};
if (f % 1 === 0 && ndigits >= 0) {
return f;
}
var factor = Math.pow(10, ndigits),
result = Math.ceil(f * factor) / factor;
if (f % 1 === 0) {
result = Math.round(result);
}
return result;
}
end
|
#chr(encoding = undefined) ⇒ Object
391
392
393
|
# File 'opal/opal/corelib/number.rb', line 391
def chr(encoding = undefined)
`Opal.enc(String.fromCharCode(self), encoding || "BINARY")`
end
|
#coerce(other) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'opal/opal/corelib/number.rb', line 20
def coerce(other)
%x{
if (other === nil) {
#{::Kernel.raise ::TypeError, "can't convert #{other.class} into Float"};
}
else if (other.$$is_string) {
return [#{::Kernel.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 {
#{::Kernel.raise ::TypeError, "can't convert #{other.class} into Float"};
}
}
end
|
#denominator ⇒ Object
395
396
397
398
399
400
401
|
# File 'opal/opal/corelib/number.rb', line 395
def denominator
if nan? || infinite?
1
else
super
end
end
|
#digits(base = 10) ⇒ Object
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
|
# File 'opal/opal/corelib/number.rb', line 739
def digits(base = 10)
if self < 0
::Kernel.raise ::Math::DomainError, 'out of domain'
end
base = ::Opal.coerce_to! base, ::Integer, :to_int
if base < 2
::Kernel.raise ::ArgumentError, "invalid radix #{base}"
end
%x{
if (self != parseInt(self)) #{::Kernel.raise ::NoMethodError, "undefined method `digits' for #{inspect}"}
var value = self, result = [];
if (self == 0) {
return [0];
}
while (value != 0) {
result.push(value % base);
value = parseInt(value / base, 10);
}
return result;
}
end
|
#downto(stop, &block) ⇒ Object
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
# File 'opal/opal/corelib/number.rb', line 403
def downto(stop, &block)
unless block_given?
return enum_for(:downto, stop) do
::Kernel.raise ::ArgumentError, "comparison of #{self.class} with #{stop.class} failed" unless ::Numeric === stop
stop > self ? 0 : self - stop + 1
end
end
%x{
if (!stop.$$is_number) {
#{::Kernel.raise ::ArgumentError, "comparison of #{self.class} with #{stop.class} failed"}
}
for (var i = self; i >= stop; i--) {
block(i);
}
}
self
end
|
#equal?(other) ⇒ Boolean
423
424
425
|
# File 'opal/opal/corelib/number.rb', line 423
def equal?(other)
self == other || `isNaN(self) && isNaN(other)`
end
|
427
428
429
|
# File 'opal/opal/corelib/number.rb', line 427
def even?
`self % 2 === 0`
end
|
811
812
813
|
# File 'opal/opal/corelib/number.rb', line 811
def finite?
`self != Infinity && self != -Infinity && !isNaN(self)`
end
|
#floor(ndigits = 0) ⇒ Object
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'opal/opal/corelib/number.rb', line 431
def floor(ndigits = 0)
%x{
var f = #{to_f};
if (f % 1 === 0 && ndigits >= 0) {
return f;
}
var factor = Math.pow(10, ndigits),
result = Math.floor(f * factor) / factor;
if (f % 1 === 0) {
result = Math.round(result);
}
return result;
}
end
|
#gcd(other) ⇒ Object
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
# File 'opal/opal/corelib/number.rb', line 450
def gcd(other)
unless ::Integer === other
::Kernel.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
470
471
472
|
# File 'opal/opal/corelib/number.rb', line 470
def gcdlcm(other)
[gcd(other), lcm(other)]
end
|
57
58
59
60
61
62
63
64
65
66
67
|
# File 'opal/opal/corelib/number.rb', line 57
def hash
%x{
// Binary-safe integers
if (self|0 === self) {
return #{__id__}
}
else {
return self.toString().$hash();
}
}
end
|
815
816
817
818
819
820
821
822
823
824
825
826
827
|
# File 'opal/opal/corelib/number.rb', line 815
def infinite?
%x{
if (self == Infinity) {
return +1;
}
else if (self == -Infinity) {
return -1;
}
else {
return nil;
}
}
end
|
#instance_of?(klass) ⇒ Boolean
486
487
488
489
490
491
492
|
# File 'opal/opal/corelib/number.rb', line 486
def instance_of?(klass)
return true if klass == ::Integer && ::Integer === self
return true if klass == ::Integer && ::Integer === self
return true if klass == ::Float && ::Float === self
super
end
|
474
475
476
|
# File 'opal/opal/corelib/number.rb', line 474
def integer?
`self % 1 === 0`
end
|
#is_a?(klass) ⇒ Boolean
Also known as:
kind_of?
478
479
480
481
482
483
484
|
# File 'opal/opal/corelib/number.rb', line 478
def is_a?(klass)
return true if klass == ::Integer && ::Integer === self
return true if klass == ::Integer && ::Integer === self
return true if klass == ::Float && ::Float === self
super
end
|
#lcm(other) ⇒ Object
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
# File 'opal/opal/corelib/number.rb', line 494
def lcm(other)
unless ::Integer === other
::Kernel.raise ::TypeError, 'not an integer'
end
%x{
if (self == 0 || other == 0) {
return 0;
}
else {
return Math.abs(self * other / #{gcd(other)});
}
}
end
|
807
808
809
|
# File 'opal/opal/corelib/number.rb', line 807
def nan?
`isNaN(self)`
end
|
833
834
835
|
# File 'opal/opal/corelib/number.rb', line 833
def negative?
`self == -Infinity || 1 / self < 0`
end
|
#next ⇒ Object
Also known as:
succ
509
510
511
|
# File 'opal/opal/corelib/number.rb', line 509
def next
`self + 1`
end
|
#next_float ⇒ Object
875
876
877
878
879
880
881
882
883
884
885
|
# File 'opal/opal/corelib/number.rb', line 875
def next_float
return ::Float::INFINITY if self == ::Float::INFINITY
return ::Float::NAN if nan?
if self >= 0
`incrementNumberBit(Math.abs(self))`
else
`decrementNumberBit(self)`
end
end
|
#nobits?(mask) ⇒ Boolean
513
514
515
516
|
# File 'opal/opal/corelib/number.rb', line 513
def nobits?(mask)
mask = ::Opal.coerce_to! mask, ::Integer, :to_int
`(self & mask) == 0`
end
|
518
519
520
|
# File 'opal/opal/corelib/number.rb', line 518
def nonzero?
`self == 0 ? nil : self`
end
|
#numerator ⇒ Object
522
523
524
525
526
527
528
|
# File 'opal/opal/corelib/number.rb', line 522
def numerator
if nan? || infinite?
self
else
super
end
end
|
530
531
532
|
# File 'opal/opal/corelib/number.rb', line 530
def odd?
`self % 2 !== 0`
end
|
534
535
536
|
# File 'opal/opal/corelib/number.rb', line 534
def ord
self
end
|
829
830
831
|
# File 'opal/opal/corelib/number.rb', line 829
def positive?
`self != 0 && (self == Infinity || 1 / self > 0)`
end
|
#pow(b, m = undefined) ⇒ Object
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
|
# File 'opal/opal/corelib/number.rb', line 538
def pow(b, m = undefined)
%x{
if (self == 0) {
#{::Kernel.raise ::ZeroDivisionError, 'divided by 0'}
}
if (m === undefined) {
return #{self**b};
} else {
if (!(#{::Integer === b})) {
#{::Kernel.raise ::TypeError, 'Integer#pow() 2nd argument not allowed unless a 1st argument is integer'}
}
if (b < 0) {
#{::Kernel.raise ::TypeError, 'Integer#pow() 1st argument cannot be negative when 2nd argument specified'}
}
if (!(#{::Integer === m})) {
#{::Kernel.raise ::TypeError, 'Integer#pow() 2nd argument not allowed unless all arguments are integers'}
}
if (m === 0) {
#{::Kernel.raise ::ZeroDivisionError, 'divided by 0'}
}
return #{(self**b) % m}
}
}
end
|
568
569
570
|
# File 'opal/opal/corelib/number.rb', line 568
def pred
`self - 1`
end
|
#prev_float ⇒ Object
887
888
889
890
891
892
893
894
895
896
|
# File 'opal/opal/corelib/number.rb', line 887
def prev_float
return -::Float::INFINITY if self == -::Float::INFINITY
return ::Float::NAN if nan?
if self > 0
`decrementNumberBit(self)`
else
`-incrementNumberBit(Math.abs(self))`
end
end
|
#quo(other) ⇒ Object
572
573
574
575
576
577
578
|
# File 'opal/opal/corelib/number.rb', line 572
def quo(other)
if ::Integer === self
super
else
self / other
end
end
|
#rationalize(eps = undefined) ⇒ Object
#remainder(y) ⇒ Object
604
605
606
|
# File 'opal/opal/corelib/number.rb', line 604
def remainder(y)
self - y * (self / y).truncate
end
|
#round(ndigits = undefined) ⇒ Object
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
|
# File 'opal/opal/corelib/number.rb', line 608
def round(ndigits = undefined)
if ::Integer === self
if `ndigits == null`
return self
end
if ::Float === ndigits && ndigits.infinite?
::Kernel.raise ::RangeError, 'Infinity'
end
ndigits = ::Opal.coerce_to!(ndigits, ::Integer, :to_int)
if ndigits < ::Integer::MIN
::Kernel.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(self) + f / 2) / f) * f;
return self < 0 ? -x : x;
}
else
if nan? && `ndigits == null`
::Kernel.raise ::FloatDomainError, 'NaN'
end
ndigits = ::Opal.coerce_to!(`ndigits || 0`, ::Integer, :to_int)
if ndigits <= 0
if nan?
::Kernel.raise ::RangeError, 'NaN'
elsif infinite?
::Kernel.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.
803
804
805
|
# File 'opal/opal/corelib/number.rb', line 803
def size
4
end
|
#times(&block) ⇒ Object
673
674
675
676
677
678
679
680
681
682
683
|
# File 'opal/opal/corelib/number.rb', line 673
def times(&block)
return enum_for(:times) { self } unless block
%x{
for (var i = 0; i < self; i++) {
block(i);
}
}
self
end
|
685
686
687
|
# File 'opal/opal/corelib/number.rb', line 685
def to_f
self
end
|
#to_i ⇒ Object
Also known as:
to_int
689
690
691
|
# File 'opal/opal/corelib/number.rb', line 689
def to_i
`self < 0 ? Math.ceil(self) : Math.floor(self)`
end
|
693
694
695
696
697
698
699
700
701
702
703
|
# File 'opal/opal/corelib/number.rb', line 693
def to_r
if ::Integer === self
::Rational.new(self, 1)
else
f, e = ::Math.frexp(self)
f = ::Math.ldexp(f, ::Float::MANT_DIG).to_i
e -= ::Float::MANT_DIG
(f * (::Float::RADIX**e)).to_r
end
end
|
#to_s(base = 10) ⇒ Object
Also known as:
inspect
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
# File 'opal/opal/corelib/number.rb', line 705
def to_s(base = 10)
base = ::Opal.coerce_to! base, ::Integer, :to_int
if base < 2 || base > 36
::Kernel.raise ::ArgumentError, "invalid radix #{base}"
end
if self == 0 && `1/self === -Infinity`
return '-0.0'
end
`self.toString(base)`
end
|
#truncate(ndigits = 0) ⇒ Object
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
# File 'opal/opal/corelib/number.rb', line 720
def truncate(ndigits = 0)
%x{
var f = #{to_f};
if (f % 1 === 0 && ndigits >= 0) {
return f;
}
var factor = Math.pow(10, ndigits),
result = parseInt(f * factor, 10) / factor;
if (f % 1 === 0) {
result = Math.round(result);
}
return result;
}
end
|
#upto(stop, &block) ⇒ Object
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
|
# File 'opal/opal/corelib/number.rb', line 778
def upto(stop, &block)
unless block_given?
return enum_for(:upto, stop) do
::Kernel.raise ::ArgumentError, "comparison of #{self.class} with #{stop.class} failed" unless ::Numeric === stop
stop < self ? 0 : stop - self + 1
end
end
%x{
if (!stop.$$is_number) {
#{::Kernel.raise ::ArgumentError, "comparison of #{self.class} with #{stop.class} failed"}
}
for (var i = self; i <= stop; i++) {
block(i);
}
}
self
end
|
798
799
800
|
# File 'opal/opal/corelib/number.rb', line 798
def zero?
`self == 0`
end
|
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
|
275
276
277
|
# File 'opal/opal/corelib/number.rb', line 275
def ~
`~self`
end
|