Class: Numeric
- Includes:
- Comparable
- Defined in:
- opal/opal/corelib/numeric.rb
Instance Method Summary collapse
- #%(other) ⇒ Object (also: #modulo)
- #+@ ⇒ Object
- #-@ ⇒ Object
- #<=>(other) ⇒ Object
- #[](bit) ⇒ Object
- #__coerced__(method, other) ⇒ Object
- #abs ⇒ Object (also: #magnitude)
- #abs2 ⇒ Object
- #angle ⇒ Object (also: #arg)
- #ceil ⇒ Object
- #clone ⇒ Object
- #coerce(other) ⇒ Object
- #conj ⇒ Object (also: #conjugate)
- #denominator ⇒ Object
- #div(other) ⇒ Object
- #divmod(other) ⇒ Object
- #dup ⇒ Object
- #fdiv(other) ⇒ Object
- #floor ⇒ Object
- #i ⇒ Object
- #imag ⇒ Object (also: #imaginary)
- #integer? ⇒ Boolean
- #negative? ⇒ Boolean
- #nonzero? ⇒ Boolean
- #numerator ⇒ Object
- #polar ⇒ Object
- #positive? ⇒ Boolean
- #quo(other) ⇒ Object
- #real ⇒ Object
- #real? ⇒ Boolean
- #rect ⇒ Object (also: #rectangular)
- #round(digits = undefined) ⇒ Object
- #to_c ⇒ Object
- #to_int ⇒ Object
- #truncate ⇒ Object
- #zero? ⇒ Boolean
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 |
#abs ⇒ Object Also known as: magnitude
58 59 60 |
# File 'opal/opal/corelib/numeric.rb', line 58 def abs self < 0 ? -self : self end |
#abs2 ⇒ Object
62 63 64 |
# File 'opal/opal/corelib/numeric.rb', line 62 def abs2 self * self end |
#angle ⇒ Object Also known as: arg
66 67 68 |
# File 'opal/opal/corelib/numeric.rb', line 66 def angle self < 0 ? Math::PI : 0 end |
#ceil ⇒ Object
72 73 74 |
# File 'opal/opal/corelib/numeric.rb', line 72 def ceil to_f.ceil end |
#clone ⇒ Object
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 |
#conj ⇒ Object Also known as: conjugate
76 77 78 |
# File 'opal/opal/corelib/numeric.rb', line 76 def conj self end |
#denominator ⇒ Object
82 83 84 |
# File 'opal/opal/corelib/numeric.rb', line 82 def denominator to_r.denominator end |
#div(other) ⇒ Object
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 |
#dup ⇒ Object
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 |
#floor ⇒ Object
100 101 102 |
# File 'opal/opal/corelib/numeric.rb', line 100 def floor to_f.floor end |
#imag ⇒ Object Also known as: imaginary
108 109 110 |
# File 'opal/opal/corelib/numeric.rb', line 108 def imag 0 end |
#integer? ⇒ Boolean
114 115 116 |
# File 'opal/opal/corelib/numeric.rb', line 114 def integer? false end |
#negative? ⇒ Boolean
178 179 180 |
# File 'opal/opal/corelib/numeric.rb', line 178 def negative? self < 0 end |
#nonzero? ⇒ Boolean
122 123 124 |
# File 'opal/opal/corelib/numeric.rb', line 122 def nonzero? zero? ? nil : self end |
#numerator ⇒ Object
126 127 128 |
# File 'opal/opal/corelib/numeric.rb', line 126 def numerator to_r.numerator end |
#polar ⇒ Object
132 133 134 |
# File 'opal/opal/corelib/numeric.rb', line 132 def polar return abs, arg end |
#positive? ⇒ Boolean
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 |
#real ⇒ Object
140 141 142 |
# File 'opal/opal/corelib/numeric.rb', line 140 def real self end |
#rect ⇒ Object 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_c ⇒ Object
158 159 160 |
# File 'opal/opal/corelib/numeric.rb', line 158 def to_c Complex(self, 0) end |
#to_int ⇒ Object
162 163 164 |
# File 'opal/opal/corelib/numeric.rb', line 162 def to_int to_i end |
#truncate ⇒ Object
166 167 168 |
# File 'opal/opal/corelib/numeric.rb', line 166 def truncate to_f.truncate end |
#zero? ⇒ Boolean
170 171 172 |
# File 'opal/opal/corelib/numeric.rb', line 170 def zero? self == 0 end |