Class: Numeric

Inherits:
Object show all
Includes:
Comparable
Defined in:
opal/opal/corelib/numeric.rb

Direct Known Subclasses

Complex, Float, Integer, Number, Rational

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?, #clamp, normalize

Instance Method Details

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



46
47
48
# File 'opal/opal/corelib/numeric.rb', line 46

def %(other)
  self - other * self.div(other)
end

#+@Object



38
39
40
# File 'opal/opal/corelib/numeric.rb', line 38

def +@
  self
end

#-@Object



42
43
44
# File 'opal/opal/corelib/numeric.rb', line 42

def -@
  0 - self
end

#<=>(other) ⇒ Object



30
31
32
33
34
35
36
# File 'opal/opal/corelib/numeric.rb', line 30

def <=>(other)
  if equal? other
    return 0
  end

  nil
end

#__coerced__(method, other) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'opal/opal/corelib/numeric.rb', line 14

def __coerced__(method, other)
  begin
    a, b = other.coerce(self)
  rescue
    case method
    when :+, :-, :*, :/, :%, :&, :|, :^, :**
      raise TypeError, "#{other.class} can't be coerce into Numeric"

    when :>, :>=, :<, :<=, :<=>
      raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
    end
  end

  a.__send__ method, b
end

#absObject Also known as: magnitude



50
51
52
# File 'opal/opal/corelib/numeric.rb', line 50

def abs
  self < 0 ? -self : self
end

#abs2Object



54
55
56
# File 'opal/opal/corelib/numeric.rb', line 54

def abs2
  self * self
end

#angleObject Also known as: arg



58
59
60
# File 'opal/opal/corelib/numeric.rb', line 58

def angle
  self < 0 ? Math::PI : 0
end

#ceilObject



64
65
66
# File 'opal/opal/corelib/numeric.rb', line 64

def ceil
  to_f.ceil
end

#clone(freeze: true) ⇒ Object



178
179
180
# File 'opal/opal/corelib/numeric.rb', line 178

def clone(freeze: true)
  self
end

#coerce(other) ⇒ Object



6
7
8
9
10
11
12
# File 'opal/opal/corelib/numeric.rb', line 6

def coerce(other)
  if other.instance_of? self.class
    return [other, self]
  end

  [Float(other), Float(self)]
end

#conjObject Also known as: conjugate



68
69
70
# File 'opal/opal/corelib/numeric.rb', line 68

def conj
  self
end

#denominatorObject



74
75
76
# File 'opal/opal/corelib/numeric.rb', line 74

def denominator
  to_r.denominator
end

#div(other) ⇒ Object

Raises:



78
79
80
81
82
# File 'opal/opal/corelib/numeric.rb', line 78

def div(other)
  raise ZeroDivisionError, "divided by o" if other == 0

  (self / other).floor
end

#divmod(other) ⇒ Object



84
85
86
# File 'opal/opal/corelib/numeric.rb', line 84

def divmod(other)
  [div(other), self % other]
end

#dupObject



174
175
176
# File 'opal/opal/corelib/numeric.rb', line 174

def dup
  self
end

#fdiv(other) ⇒ Object



88
89
90
# File 'opal/opal/corelib/numeric.rb', line 88

def fdiv(other)
  self.to_f / other
end

#floorObject



92
93
94
# File 'opal/opal/corelib/numeric.rb', line 92

def floor
  to_f.floor
end

#iObject



96
97
98
# File 'opal/opal/corelib/numeric.rb', line 96

def i
  Complex(0, self)
end

#imagObject Also known as: imaginary



100
101
102
# File 'opal/opal/corelib/numeric.rb', line 100

def imag
  0
end

#integer?Boolean

Returns:



106
107
108
# File 'opal/opal/corelib/numeric.rb', line 106

def integer?
  false
end

#negative?Boolean

Returns:



170
171
172
# File 'opal/opal/corelib/numeric.rb', line 170

def negative?
  self < 0
end

#nonzero?Boolean

Returns:



114
115
116
# File 'opal/opal/corelib/numeric.rb', line 114

def nonzero?
  zero? ? nil : self
end

#numeratorObject



118
119
120
# File 'opal/opal/corelib/numeric.rb', line 118

def numerator
  to_r.numerator
end

#polarObject



124
125
126
# File 'opal/opal/corelib/numeric.rb', line 124

def polar
  return abs, arg
end

#positive?Boolean

Returns:



166
167
168
# File 'opal/opal/corelib/numeric.rb', line 166

def positive?
  self > 0
end

#quo(other) ⇒ Object



128
129
130
# File 'opal/opal/corelib/numeric.rb', line 128

def quo(other)
  Opal.coerce_to!(self, Rational, :to_r) / other
end

#realObject



132
133
134
# File 'opal/opal/corelib/numeric.rb', line 132

def real
  self
end

#real?Boolean

Returns:



136
137
138
# File 'opal/opal/corelib/numeric.rb', line 136

def real?
  true
end

#rectObject Also known as: rectangular



140
141
142
# File 'opal/opal/corelib/numeric.rb', line 140

def rect
  [self, 0]
end

#round(digits = undefined) ⇒ Object



146
147
148
# File 'opal/opal/corelib/numeric.rb', line 146

def round(digits = undefined)
  to_f.round(digits)
end

#to_cObject



150
151
152
# File 'opal/opal/corelib/numeric.rb', line 150

def to_c
  Complex(self, 0)
end

#to_intObject



154
155
156
# File 'opal/opal/corelib/numeric.rb', line 154

def to_int
  to_i
end

#truncateObject



158
159
160
# File 'opal/opal/corelib/numeric.rb', line 158

def truncate
  to_f.truncate
end

#zero?Boolean

Returns:



162
163
164
# File 'opal/opal/corelib/numeric.rb', line 162

def zero?
  self == 0
end