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

Instance Method Details

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



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

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

#+@Object



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

def +@
  self
end

#-@Object



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

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

#[](bit) ⇒ Object



38
39
40
41
42
43
44
# File 'opal/opal/corelib/numeric.rb', line 38

def [](bit)
  bit = Opal.coerce_to! bit, Integer, :to_int
  min = -(2**30)
  max =  (2**30) - 1

  `(#{bit} < #{min} || #{bit} > #{max}) ? 0 : (self >> #{bit}) % 2`
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



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

def abs
  self < 0 ? -self : self
end

#abs2Object



62
63
64
# File 'opal/opal/corelib/numeric.rb', line 62

def abs2
  self * self
end

#angleObject Also known as: arg



66
67
68
# File 'opal/opal/corelib/numeric.rb', line 66

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

#ceilObject



72
73
74
# File 'opal/opal/corelib/numeric.rb', line 72

def ceil
  to_f.ceil
end

#cloneObject

Raises:



186
187
188
# File 'opal/opal/corelib/numeric.rb', line 186

def clone
  raise TypeError, "can't clone #{self.class}"
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



76
77
78
# File 'opal/opal/corelib/numeric.rb', line 76

def conj
  self
end

#denominatorObject



82
83
84
# File 'opal/opal/corelib/numeric.rb', line 82

def denominator
  to_r.denominator
end

#div(other) ⇒ Object

Raises:



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

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

  (self / other).floor
end

#divmod(other) ⇒ Object



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

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

#dupObject

Raises:



182
183
184
# File 'opal/opal/corelib/numeric.rb', line 182

def dup
  raise TypeError, "can't dup #{self.class}"
end

#fdiv(other) ⇒ Object



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

def fdiv(other)
  self.to_f / other
end

#floorObject



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

def floor
  to_f.floor
end

#iObject



104
105
106
# File 'opal/opal/corelib/numeric.rb', line 104

def i
  Complex(0, self)
end

#imagObject Also known as: imaginary



108
109
110
# File 'opal/opal/corelib/numeric.rb', line 108

def imag
  0
end

#integer?Boolean

Returns:



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

def integer?
  false
end

#negative?Boolean

Returns:



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

def negative?
  self < 0
end

#nonzero?Boolean

Returns:



122
123
124
# File 'opal/opal/corelib/numeric.rb', line 122

def nonzero?
  zero? ? nil : self
end

#numeratorObject



126
127
128
# File 'opal/opal/corelib/numeric.rb', line 126

def numerator
  to_r.numerator
end

#polarObject



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

def polar
  return abs, arg
end

#positive?Boolean

Returns:



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

def positive?
  self > 0
end

#quo(other) ⇒ Object



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

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

#realObject



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

def real
  self
end

#real?Boolean

Returns:



144
145
146
# File 'opal/opal/corelib/numeric.rb', line 144

def real?
  true
end

#rectObject Also known as: rectangular



148
149
150
# File 'opal/opal/corelib/numeric.rb', line 148

def rect
  [self, 0]
end

#round(digits = undefined) ⇒ Object



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

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

#to_cObject



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

def to_c
  Complex(self, 0)
end

#to_intObject



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

def to_int
  to_i
end

#truncateObject



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

def truncate
  to_f.truncate
end

#zero?Boolean

Returns:



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

def zero?
  self == 0
end