Class: Number

Inherits:
Numeric show all
Defined in:
opal/opal/corelib/number.rb

Class Method Summary collapse

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?, #clamp, normalize

Class Method Details

.allocateObject

Raises:



9
10
11
# File 'opal/opal/corelib/number.rb', line 9

def allocate
  raise TypeError, "allocator undefined for #{self.name}"
end

Instance Method Details

#%(other) ⇒ Object Also known as: modulo



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'opal/opal/corelib/number.rb', line 88

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

#&(other) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'opal/opal/corelib/number.rb', line 110

def &(other)
  %x{
    if (other.$$is_number) {
      return self & other;
    }
    else {
      return #{__coerced__ :&, other};
    }
  }
end

#*(other) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'opal/opal/corelib/number.rb', line 64

def *(other)
  %x{
    if (other.$$is_number) {
      return self * other;
    }
    else {
      return #{__coerced__ :*, other};
    }
  }
end

#**(other) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'opal/opal/corelib/number.rb', line 256

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

#+(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'opal/opal/corelib/number.rb', line 42

def +(other)
  %x{
    if (other.$$is_number) {
      return self + other;
    }
    else {
      return #{__coerced__ :+, other};
    }
  }
end

#+@Object



244
245
246
# File 'opal/opal/corelib/number.rb', line 244

def +@
  `+self`
end

#-(other) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'opal/opal/corelib/number.rb', line 53

def -(other)
  %x{
    if (other.$$is_number) {
      return self - other;
    }
    else {
      return #{__coerced__ :-, other};
    }
  }
end

#-@Object



248
249
250
# File 'opal/opal/corelib/number.rb', line 248

def -@
  `-self`
end

#/(other) ⇒ Object Also known as: fdiv



75
76
77
78
79
80
81
82
83
84
# File 'opal/opal/corelib/number.rb', line 75

def /(other)
  %x{
    if (other.$$is_number) {
      return self / other;
    }
    else {
      return #{__coerced__ :/, other};
    }
  }
end

#<(other) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'opal/opal/corelib/number.rb', line 143

def <(other)
  %x{
    if (other.$$is_number) {
      return self < other;
    }
    else {
      return #{__coerced__ :<, other};
    }
  }
end

#<<(count) ⇒ Object



218
219
220
221
222
# File 'opal/opal/corelib/number.rb', line 218

def <<(count)
  count = Opal.coerce_to! count, Integer, :to_int

  `#{count} > 0 ? self << #{count} : self >> -#{count}`
end

#<=(other) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'opal/opal/corelib/number.rb', line 154

def <=(other)
  %x{
    if (other.$$is_number) {
      return self <= other;
    }
    else {
      return #{__coerced__ :<=, other};
    }
  }
end

#<=>(other) ⇒ Object



210
211
212
213
214
215
216
# File 'opal/opal/corelib/number.rb', line 210

def <=>(other)
  %x{
    return spaceship_operator(self, other);
  }
rescue ArgumentError
  nil
end

#==(other) ⇒ Object Also known as: eql?



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'opal/opal/corelib/number.rb', line 286

def ==(other)
  %x{
    if (other.$$is_number) {
      return self.valueOf() === other.valueOf();
    }
    else if (#{other.respond_to? :==}) {
      return #{other == self};
    }
    else {
      return false;
    }
  }
end

#===(other) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'opal/opal/corelib/number.rb', line 272

def ===(other)
  %x{
    if (other.$$is_number) {
      return self.valueOf() === other.valueOf();
    }
    else if (#{other.respond_to? :==}) {
      return #{other == self};
    }
    else {
      return false;
    }
  }
end

#>(other) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'opal/opal/corelib/number.rb', line 165

def >(other)
  %x{
    if (other.$$is_number) {
      return self > other;
    }
    else {
      return #{__coerced__ :>, other};
    }
  }
end

#>=(other) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'opal/opal/corelib/number.rb', line 176

def >=(other)
  %x{
    if (other.$$is_number) {
      return self >= other;
    }
    else {
      return #{__coerced__ :>=, other};
    }
  }
end

#>>(count) ⇒ Object



224
225
226
227
228
# File 'opal/opal/corelib/number.rb', line 224

def >>(count)
  count = Opal.coerce_to! count, Integer, :to_int

  `#{count} > 0 ? self >> #{count} : self << -#{count}`
end

#[](bit) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'opal/opal/corelib/number.rb', line 230

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

#^(other) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'opal/opal/corelib/number.rb', line 132

def ^(other)
  %x{
    if (other.$$is_number) {
      return self ^ other;
    }
    else {
      return #{__coerced__ :^, other};
    }
  }
end

#__id__Object Also known as: object_id



36
37
38
# File 'opal/opal/corelib/number.rb', line 36

def __id__
  `(self * 2) + 1`
end

#absObject Also known as: magnitude



300
301
302
# File 'opal/opal/corelib/number.rb', line 300

def abs
  `Math.abs(self)`
end

#abs2Object



304
305
306
# File 'opal/opal/corelib/number.rb', line 304

def abs2
  `Math.abs(self * self)`
end

#angleObject Also known as: arg, phase



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'opal/opal/corelib/number.rb', line 308

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_lengthObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'opal/opal/corelib/number.rb', line 332

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

#ceilObject



354
355
356
# File 'opal/opal/corelib/number.rb', line 354

def ceil
  `Math.ceil(self)`
end

#chr(encoding = undefined) ⇒ Object



358
359
360
# File 'opal/opal/corelib/number.rb', line 358

def chr(encoding = undefined)
  `String.fromCharCode(self)`
end

#coerce(other) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'opal/opal/corelib/number.rb', line 16

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

#denominatorObject



362
363
364
365
366
367
368
# File 'opal/opal/corelib/number.rb', line 362

def denominator
  if nan? || infinite?
    1
  else
    super
  end
end

#divmod(other) ⇒ Object



818
819
820
821
822
823
824
825
826
# File 'opal/opal/corelib/number.rb', line 818

def divmod(other)
  if nan? || other.nan?
    raise FloatDomainError, "NaN"
  elsif infinite?
    raise FloatDomainError, "Infinity"
  else
    super
  end
end

#downto(stop, &block) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'opal/opal/corelib/number.rb', line 370

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

Returns:



390
391
392
# File 'opal/opal/corelib/number.rb', line 390

def equal?(other)
  self == other || `isNaN(self) && isNaN(other)`
end

#even?Boolean

Returns:



394
395
396
# File 'opal/opal/corelib/number.rb', line 394

def even?
  `self % 2 === 0`
end

#finite?Boolean

Returns:



859
860
861
# File 'opal/opal/corelib/number.rb', line 859

def finite?
  `self != Infinity && self != -Infinity && !isNaN(self)`
end

#floorObject



398
399
400
# File 'opal/opal/corelib/number.rb', line 398

def floor
  `Math.floor(self)`
end

#gcd(other) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'opal/opal/corelib/number.rb', line 402

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



422
423
424
# File 'opal/opal/corelib/number.rb', line 422

def gcdlcm(other)
  [gcd, lcm]
end

#infinite?Boolean

Returns:



863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'opal/opal/corelib/number.rb', line 863

def infinite?
  %x{
    if (self == Infinity) {
      return +1;
    }
    else if (self == -Infinity) {
      return -1;
    }
    else {
      return nil;
    }
  }
end

#instance_of?(klass) ⇒ Boolean

Returns:



440
441
442
443
444
445
446
# File 'opal/opal/corelib/number.rb', line 440

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

#integer?Boolean

Returns:



426
427
428
# File 'opal/opal/corelib/number.rb', line 426

def integer?
  `self % 1 === 0`
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:



430
431
432
433
434
435
436
# File 'opal/opal/corelib/number.rb', line 430

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



448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'opal/opal/corelib/number.rb', line 448

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

#nan?Boolean

Returns:



855
856
857
# File 'opal/opal/corelib/number.rb', line 855

def nan?
  `isNaN(self)`
end

#negative?Boolean

Returns:



881
882
883
# File 'opal/opal/corelib/number.rb', line 881

def negative?
  `self == -Infinity || 1 / self < 0`
end

#nextObject Also known as: succ



467
468
469
# File 'opal/opal/corelib/number.rb', line 467

def next
  `self + 1`
end

#nonzero?Boolean

Returns:



471
472
473
# File 'opal/opal/corelib/number.rb', line 471

def nonzero?
  `self == 0 ? nil : self`
end

#numeratorObject



475
476
477
478
479
480
481
# File 'opal/opal/corelib/number.rb', line 475

def numerator
  if nan? || infinite?
    self
  else
    super
  end
end

#odd?Boolean

Returns:



483
484
485
# File 'opal/opal/corelib/number.rb', line 483

def odd?
  `self % 2 !== 0`
end

#ordObject



487
488
489
# File 'opal/opal/corelib/number.rb', line 487

def ord
  self
end

#positive?Boolean

Returns:



877
878
879
# File 'opal/opal/corelib/number.rb', line 877

def positive?
  `self != 0 && (self == Infinity || 1 / self > 0)`
end

#predObject



491
492
493
# File 'opal/opal/corelib/number.rb', line 491

def pred
  `self - 1`
end

#quo(other) ⇒ Object



495
496
497
498
499
500
501
# File 'opal/opal/corelib/number.rb', line 495

def quo(other)
  if Integer === self
    super
  else
    self / other
  end
end

#rationalize(eps = undefined) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'opal/opal/corelib/number.rb', line 503

def rationalize(eps = undefined)
  %x{
    if (arguments.length > 1) {
      #{raise ArgumentError, "wrong number of arguments (#{`arguments.length`} for 0..1)"};
    }
  }

  if Integer === self
    Rational.new(self, 1)
  elsif infinite?
    raise FloatDomainError, "Infinity"
  elsif nan?
    raise FloatDomainError, "NaN"
  elsif `eps == null`
    f, n  = Math.frexp self
    f     = Math.ldexp(f, Float::MANT_DIG).to_i
    n    -= Float::MANT_DIG

    Rational.new(2 * f, 1 << (1 - n)).rationalize(Rational.new(1, 1 << (1 - n)))
  else
    to_r.rationalize(eps)
  end
end

#round(ndigits = undefined) ⇒ Object



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
# File 'opal/opal/corelib/number.rb', line 527

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

#sizeObject

Since bitwise operations are 32 bit, declare it to be so.



851
852
853
# File 'opal/opal/corelib/number.rb', line 851

def size
  4
end

#step(limit = undefined, step = undefined, to: undefined, by: undefined, &block) ⇒ Object



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
# File 'opal/opal/corelib/number.rb', line 592

def step(limit = undefined, step = undefined, to: undefined, by: undefined, &block)
  %x{
    if (limit !== undefined && to !== undefined) {
      #{raise ArgumentError, "to is given twice"}
    }

    if (step !== undefined && by !== undefined) {
      #{raise ArgumentError, "step is given twice"}
    }

    function validateParameters() {
      if (to !== undefined) {
        limit = to;
      }

      if (limit === undefined) {
        limit = nil;
      }

      if (step === nil) {
        #{raise TypeError, "step must be numeric"}
      }

      if (step === 0) {
        #{raise ArgumentError, "step can't be 0"}
      }

      if (by !== undefined) {
        step = by;
      }

      if (step === nil || step == null) {
        step = 1;
      }

      var sign = #{step <=> 0};

      if (sign === nil) {
        #{raise TypeError, "0 can't be coerced into #{step.class}"}
      }

      if (limit === nil || limit == null) {
        limit = sign > 0 ? #{Float::INFINITY} : #{-Float::INFINITY};
      }

      #{Opal.compare(self, limit)}
    }

    function stepFloatSize() {
      if ((step > 0 && self > limit) || (step < 0 && self < limit)) {
        return 0;
      } else if (step === Infinity || step === -Infinity) {
        return 1;
      } else {
        var abs = Math.abs, floor = Math.floor,
            err = (abs(self) + abs(limit) + abs(limit - self)) / abs(step) * #{Float::EPSILON};

        if (err === Infinity || err === -Infinity) {
          return 0;
        } else {
          if (err > 0.5) {
            err = 0.5;
          }

          return floor((limit - self) / step + err) + 1
        }
      }
    }

    function stepSize() {
      validateParameters();

      if (step === 0) {
        return Infinity;
      }

      if (step % 1 !== 0) {
        return stepFloatSize();
      } else if ((step > 0 && self > limit) || (step < 0 && self < limit)) {
        return 0;
      } else {
        var ceil = Math.ceil, abs = Math.abs,
            lhs = abs(self - limit) + 1,
            rhs = abs(step);

        return ceil(lhs / rhs);
      }
    }
  }

  unless block_given?
    positional_args = []
    keyword_args = {}

    %x{
      if (limit !== undefined) {
        positional_args.push(limit);
      }

      if (step !== undefined) {
        positional_args.push(step);
      }

      if (to !== undefined) {
        Opal.hash_put(keyword_args, "to", to);
      }

      if (by !== undefined) {
        Opal.hash_put(keyword_args, "by", by);
      }

      if (!#{keyword_args.empty?}) {
        positional_args.push(keyword_args);
      }
    }

    return enum_for(:step, *positional_args) { `stepSize()` }
  end

  %x{
    validateParameters();

    if (step === 0) {
      while (true) {
        block(self);
      }
    }

    if (self % 1 !== 0 || limit % 1 !== 0 || step % 1 !== 0) {
      var n = stepFloatSize();

      if (n > 0) {
        if (step === Infinity || step === -Infinity) {
          block(self);
        } else {
          var i = 0, d;

          if (step > 0) {
            while (i < n) {
              d = i * step + self;
              if (limit < d) {
                d = limit;
              }
              block(d);
              i += 1;
            }
          } else {
            while (i < n) {
              d = i * step + self;
              if (limit > d) {
                d = limit;
              }
              block(d);
              i += 1
            }
          }
        }
      }
    } else {
      var value = self;

      if (step > 0) {
        while (value <= limit) {
          block(value);
          value += step;
        }
      } else {
        while (value >= limit) {
          block(value);
          value += step
        }
      }
    }

    return self;
  }
end

#times(&block) ⇒ Object



772
773
774
775
776
777
778
779
780
781
782
# File 'opal/opal/corelib/number.rb', line 772

def times(&block)
  return enum_for(:times) { self } unless block

  %x{
    for (var i = 0; i < self; i++) {
      block(i);
    }
  }

  self
end

#to_fObject



784
785
786
# File 'opal/opal/corelib/number.rb', line 784

def to_f
  self
end

#to_iObject Also known as: to_int, truncate



788
789
790
# File 'opal/opal/corelib/number.rb', line 788

def to_i
  `parseInt(self, 10)`
end

#to_rObject



794
795
796
797
798
799
800
801
802
803
804
# File 'opal/opal/corelib/number.rb', line 794

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



806
807
808
809
810
811
812
# File 'opal/opal/corelib/number.rb', line 806

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



828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'opal/opal/corelib/number.rb', line 828

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

#zero?Boolean

Returns:



846
847
848
# File 'opal/opal/corelib/number.rb', line 846

def zero?
  `self == 0`
end

#|(other) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'opal/opal/corelib/number.rb', line 121

def |(other)
  %x{
    if (other.$$is_number) {
      return self | other;
    }
    else {
      return #{__coerced__ :|, other};
    }
  }
end

#~Object



252
253
254
# File 'opal/opal/corelib/number.rb', line 252

def ~
  `~self`
end