Class: Numeric
- Includes:
- Comparable
- Defined in:
- opal/opal/corelib/numeric.rb
Instance Method Summary collapse
- #%(other) ⇒ Object (also: #modulo)
- #+@ ⇒ Object
- #-@ ⇒ Object
- #<=>(other) ⇒ Object
- #__coerced__(method, other) ⇒ Object
- #abs ⇒ Object (also: #magnitude)
- #abs2 ⇒ Object
- #angle ⇒ Object (also: #arg)
- #ceil(ndigits = 0) ⇒ Object
- #clone(freeze: true) ⇒ Object
- #coerce(other) ⇒ Object
- #conj ⇒ Object (also: #conjugate)
- #denominator ⇒ Object
- #div(other) ⇒ Object
- #divmod(other) ⇒ Object
- #dup ⇒ Object
- #fdiv(other) ⇒ Object
- #finite? ⇒ Boolean
- #floor(ndigits = 0) ⇒ Object
- #i ⇒ Object
- #imag ⇒ Object (also: #imaginary)
- #infinite? ⇒ Boolean
- #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
- #step(limit = undefined, step = undefined, to: undefined, by: undefined, &block) ⇒ Object
- #to_c ⇒ Object
- #to_int ⇒ Object
- #truncate(ndigits = 0) ⇒ Object
- #zero? ⇒ Boolean
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?, #clamp
Instance Method Details
#%(other) ⇒ Object Also known as: modulo
47 48 49 |
# File 'opal/opal/corelib/numeric.rb', line 47 def %(other) self - other * div(other) end |
#+@ ⇒ Object
39 40 41 |
# File 'opal/opal/corelib/numeric.rb', line 39 def +@ self end |
#-@ ⇒ Object
43 44 45 |
# File 'opal/opal/corelib/numeric.rb', line 43 def -@ 0 - self end |
#<=>(other) ⇒ Object
31 32 33 34 35 36 37 |
# File 'opal/opal/corelib/numeric.rb', line 31 def <=>(other) if equal? other return 0 end nil end |
#__coerced__(method, other) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'opal/opal/corelib/numeric.rb', line 17 def __coerced__(method, other) if other.respond_to?(:coerce) a, b = other.coerce(self) a.__send__ method, b else case method when :+, :-, :*, :/, :%, :&, :|, :^, :** ::Kernel.raise ::TypeError, "#{other.class} can't be coerced into Numeric" when :>, :>=, :<, :<=, :<=> ::Kernel.raise ::ArgumentError, "comparison of #{self.class} with #{other.class} failed" end end end |
#abs ⇒ Object Also known as: magnitude
51 52 53 |
# File 'opal/opal/corelib/numeric.rb', line 51 def abs self < 0 ? -self : self end |
#abs2 ⇒ Object
55 56 57 |
# File 'opal/opal/corelib/numeric.rb', line 55 def abs2 self * self end |
#angle ⇒ Object Also known as: arg
59 60 61 |
# File 'opal/opal/corelib/numeric.rb', line 59 def angle self < 0 ? ::Math::PI : 0 end |
#ceil(ndigits = 0) ⇒ Object
63 64 65 |
# File 'opal/opal/corelib/numeric.rb', line 63 def ceil(ndigits = 0) to_f.ceil(ndigits) end |
#clone(freeze: true) ⇒ Object
331 332 333 |
# File 'opal/opal/corelib/numeric.rb', line 331 def clone(freeze: true) self end |
#coerce(other) ⇒ Object
9 10 11 12 13 14 15 |
# File 'opal/opal/corelib/numeric.rb', line 9 def coerce(other) if other.instance_of? self.class return [other, self] end [::Kernel.Float(other), ::Kernel.Float(self)] end |
#conj ⇒ Object Also known as: conjugate
67 68 69 |
# File 'opal/opal/corelib/numeric.rb', line 67 def conj self end |
#denominator ⇒ Object
71 72 73 |
# File 'opal/opal/corelib/numeric.rb', line 71 def denominator to_r.denominator end |
#div(other) ⇒ Object
75 76 77 78 79 |
# File 'opal/opal/corelib/numeric.rb', line 75 def div(other) ::Kernel.raise ::ZeroDivisionError, 'divided by o' if other == 0 (self / other).floor end |
#divmod(other) ⇒ Object
81 82 83 |
# File 'opal/opal/corelib/numeric.rb', line 81 def divmod(other) [div(other), self % other] end |
#dup ⇒ Object
327 328 329 |
# File 'opal/opal/corelib/numeric.rb', line 327 def dup self end |
#fdiv(other) ⇒ Object
85 86 87 |
# File 'opal/opal/corelib/numeric.rb', line 85 def fdiv(other) to_f / other end |
#floor(ndigits = 0) ⇒ Object
89 90 91 |
# File 'opal/opal/corelib/numeric.rb', line 89 def floor(ndigits = 0) to_f.floor(ndigits) end |
#i ⇒ Object
93 94 95 |
# File 'opal/opal/corelib/numeric.rb', line 93 def i ::Kernel.Complex(0, self) end |
#imag ⇒ Object Also known as: imaginary
97 98 99 |
# File 'opal/opal/corelib/numeric.rb', line 97 def imag 0 end |
#infinite? ⇒ Boolean
339 340 341 |
# File 'opal/opal/corelib/numeric.rb', line 339 def infinite? nil end |
#integer? ⇒ Boolean
101 102 103 |
# File 'opal/opal/corelib/numeric.rb', line 101 def integer? false end |
#negative? ⇒ Boolean
323 324 325 |
# File 'opal/opal/corelib/numeric.rb', line 323 def negative? self < 0 end |
#nonzero? ⇒ Boolean
105 106 107 |
# File 'opal/opal/corelib/numeric.rb', line 105 def nonzero? zero? ? nil : self end |
#numerator ⇒ Object
109 110 111 |
# File 'opal/opal/corelib/numeric.rb', line 109 def numerator to_r.numerator end |
#polar ⇒ Object
113 114 115 |
# File 'opal/opal/corelib/numeric.rb', line 113 def polar [abs, arg] end |
#positive? ⇒ Boolean
319 320 321 |
# File 'opal/opal/corelib/numeric.rb', line 319 def positive? self > 0 end |
#quo(other) ⇒ Object
117 118 119 |
# File 'opal/opal/corelib/numeric.rb', line 117 def quo(other) ::Opal.coerce_to!(self, ::Rational, :to_r) / other end |
#real ⇒ Object
121 122 123 |
# File 'opal/opal/corelib/numeric.rb', line 121 def real self end |
#rect ⇒ Object Also known as: rectangular
129 130 131 |
# File 'opal/opal/corelib/numeric.rb', line 129 def rect [self, 0] end |
#round(digits = undefined) ⇒ Object
133 134 135 |
# File 'opal/opal/corelib/numeric.rb', line 133 def round(digits = undefined) to_f.round(digits) end |
#step(limit = undefined, step = undefined, to: undefined, by: undefined, &block) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'opal/opal/corelib/numeric.rb', line 137 def step(limit = undefined, step = undefined, to: undefined, by: undefined, &block) %x{ if (limit !== undefined && to !== undefined) { #{::Kernel.raise ::ArgumentError, 'to is given twice'} } if (step !== undefined && by !== undefined) { #{::Kernel.raise ::ArgumentError, 'step is given twice'} } if (to !== undefined) { limit = to; } if (by !== undefined) { step = by; } if (limit === undefined) { limit = nil; } function validateParameters() { if (step === nil) { #{::Kernel.raise ::TypeError, 'step must be numeric'} } if (step != null && #{step == 0}) { #{::Kernel.raise ::ArgumentError, "step can't be 0"} } if (step === nil || step == null) { step = 1; } var sign = #{step <=> 0}; if (sign === nil) { #{::Kernel.raise ::ArgumentError, "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? if (!limit || limit.is_a?(::Numeric)) && (!step || step.is_a?(::Numeric)) return ::Enumerator::ArithmeticSequence.new( [limit, step, ('to: ' if to), ('by: ' if by)], self ) else return enum_for(:step, limit, step, &`stepSize`) end end %x{ validateParameters(); var isDesc = #{step.negative?}, isInf = #{step == 0} || (limit === Infinity && !isDesc) || (limit === -Infinity && isDesc); if (self.$$is_number && step.$$is_number && limit.$$is_number) { if (self % 1 === 0 && (isInf || limit % 1 === 0) && step % 1 === 0) { var value = self; if (isInf) { for (;; value += step) { block(value); } } else if (isDesc) { for (; value >= limit; value += step) { block(value); } } else { for (; value <= limit; value += step) { block(value); } } return self; } else { var begin = #{to_f}.valueOf(); step = #{step.to_f}.valueOf(); limit = #{limit.to_f}.valueOf(); var n = stepFloatSize(); if (!isFinite(step)) { if (n !== 0) block(begin); } else if (step === 0) { while (true) { block(begin); } } else { for (var i = 0; i < n; i++) { var d = i * step + self; if (step >= 0 ? limit < d : limit > d) { d = limit; } block(d); } } return self; } } } counter = self while `isDesc ? #{counter >= limit} : #{counter <= limit}` yield counter counter += step end end |
#to_c ⇒ Object
303 304 305 |
# File 'opal/opal/corelib/numeric.rb', line 303 def to_c ::Kernel.Complex(self, 0) end |
#to_int ⇒ Object
307 308 309 |
# File 'opal/opal/corelib/numeric.rb', line 307 def to_int to_i end |
#truncate(ndigits = 0) ⇒ Object
311 312 313 |
# File 'opal/opal/corelib/numeric.rb', line 311 def truncate(ndigits = 0) to_f.truncate(ndigits) end |
#zero? ⇒ Boolean
315 316 317 |
# File 'opal/opal/corelib/numeric.rb', line 315 def zero? self == 0 end |