Class: Numeric
- Inherits:
-
Object
- Object
- Numeric
- Includes:
- Comparable
- Defined in:
- opal/opal/corelib/numeric.rb
Instance Method Summary collapse
- #%(other) ⇒ Object (also: #modulo)
- #&(other) ⇒ Object
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #<<(count) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?, #equal?)
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #>>(count) ⇒ Object
- #[](bit) ⇒ Object
- #^(other) ⇒ Object
- #abs ⇒ Object (also: #magnitude)
- #ceil ⇒ Object
- #chr ⇒ Object
- #coerce(other, type = :operation) ⇒ Object
- #conj ⇒ Object (also: #conjugate)
- #divmod(rhs) ⇒ Object
- #downto(finish, &block) ⇒ Object
- #even? ⇒ Boolean
- #finite? ⇒ Boolean
- #floor ⇒ Object
- #gcd(other) ⇒ Object
- #gcdlcm(other) ⇒ Object
- #hash ⇒ Object
- #infinite? ⇒ Boolean
- #instance_of?(klass) ⇒ Boolean
- #integer? ⇒ Boolean
- #is_a?(klass) ⇒ Boolean (also: #kind_of?)
- #lcm(other) ⇒ Object
- #nan? ⇒ Boolean
- #negative? ⇒ Boolean
- #next ⇒ Object (also: #succ)
- #nonzero? ⇒ Boolean
- #odd? ⇒ Boolean
- #ord ⇒ Object
- #positive? ⇒ Boolean
- #pred ⇒ Object
- #round ⇒ Object
- #send_coerced(method, other) ⇒ Object
-
#size ⇒ Object
Since bitwise operations are 32 bit, declare it to be so.
- #step(limit, step = 1, &block) ⇒ Object
- #times(&block) ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object (also: #to_int)
- #to_s(base = 10) ⇒ Object (also: #inspect)
- #upto(finish, &block) ⇒ Object
- #zero? ⇒ Boolean
- #|(other) ⇒ Object
- #~ ⇒ Object
Methods included from Comparable
Instance Method Details
#%(other) ⇒ Object Also known as: modulo
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'opal/opal/corelib/numeric.rb', line 84 def %(other) %x{ if (other._isNumber) { if (other < 0 || self < 0) { return (self % other + other) % other; } else { return self % other; } } else { return #{send_coerced :%, other}; } } end |
#&(other) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'opal/opal/corelib/numeric.rb', line 100 def &(other) %x{ if (other._isNumber) { return self & other; } else { return #{send_coerced :&, other}; } } end |
#*(other) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'opal/opal/corelib/numeric.rb', line 62 def *(other) %x{ if (other._isNumber) { return self * other; } else { return #{send_coerced :*, other}; } } end |
#**(other) ⇒ Object
218 219 220 221 222 223 224 225 226 227 |
# File 'opal/opal/corelib/numeric.rb', line 218 def **(other) %x{ if (other._isNumber) { return Math.pow(self, other); } else { return #{send_coerced :**, other}; } } end |
#+(other) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'opal/opal/corelib/numeric.rb', line 40 def +(other) %x{ if (other._isNumber) { return self + other; } else { return #{send_coerced :+, other}; } } end |
#+@ ⇒ Object
206 207 208 |
# File 'opal/opal/corelib/numeric.rb', line 206 def +@ `+self` end |
#-(other) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'opal/opal/corelib/numeric.rb', line 51 def -(other) %x{ if (other._isNumber) { return self - other; } else { return #{send_coerced :-, other}; } } end |
#-@ ⇒ Object
210 211 212 |
# File 'opal/opal/corelib/numeric.rb', line 210 def -@ `-self` end |
#/(other) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'opal/opal/corelib/numeric.rb', line 73 def /(other) %x{ if (other._isNumber) { return self / other; } else { return #{send_coerced :/, other}; } } end |
#<(other) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'opal/opal/corelib/numeric.rb', line 133 def <(other) %x{ if (other._isNumber) { return self < other; } else { return #{send_coerced :<, other}; } } end |
#<<(count) ⇒ Object
190 191 192 |
# File 'opal/opal/corelib/numeric.rb', line 190 def <<(count) `self << #{count.to_int}` end |
#<=(other) ⇒ Object
144 145 146 147 148 149 150 151 152 153 |
# File 'opal/opal/corelib/numeric.rb', line 144 def <=(other) %x{ if (other._isNumber) { return self <= other; } else { return #{send_coerced :<=, other}; } } end |
#<=>(other) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'opal/opal/corelib/numeric.rb', line 177 def <=>(other) %x{ if (other._isNumber) { return self > other ? 1 : (self < other ? -1 : 0); } else { return #{send_coerced :<=>, other}; } } rescue ArgumentError nil end |
#==(other) ⇒ Object Also known as: eql?, equal?
229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'opal/opal/corelib/numeric.rb', line 229 def ==(other) %x{ if (other._isNumber) { return self == Number(other); } else if (#{other.respond_to? :==}) { return #{other == self}; } else { return false; } } end |
#>(other) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'opal/opal/corelib/numeric.rb', line 155 def >(other) %x{ if (other._isNumber) { return self > other; } else { return #{send_coerced :>, other}; } } end |
#>=(other) ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'opal/opal/corelib/numeric.rb', line 166 def >=(other) %x{ if (other._isNumber) { return self >= other; } else { return #{send_coerced :>=, other}; } } end |
#>>(count) ⇒ Object
194 195 196 |
# File 'opal/opal/corelib/numeric.rb', line 194 def >>(count) `self >> #{count.to_int}` end |
#[](bit) ⇒ Object
198 199 200 201 202 203 204 |
# File 'opal/opal/corelib/numeric.rb', line 198 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 |
#^(other) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'opal/opal/corelib/numeric.rb', line 122 def ^(other) %x{ if (other._isNumber) { return self ^ other; } else { return #{send_coerced :^, other}; } } end |
#abs ⇒ Object Also known as: magnitude
243 244 245 |
# File 'opal/opal/corelib/numeric.rb', line 243 def abs `Math.abs(self)` end |
#ceil ⇒ Object
247 248 249 |
# File 'opal/opal/corelib/numeric.rb', line 247 def ceil `Math.ceil(self)` end |
#chr ⇒ Object
251 252 253 |
# File 'opal/opal/corelib/numeric.rb', line 251 def chr `String.fromCharCode(self)` end |
#coerce(other, type = :operation) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'opal/opal/corelib/numeric.rb', line 8 def coerce(other, type = :operation) %x{ if (other._isNumber) { return [self, other]; } else { return #{other.coerce(self)}; } } rescue case type when :operation raise TypeError, "#{other.class} can't be coerce into Numeric" when :comparison raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end end |
#conj ⇒ Object Also known as: conjugate
255 256 257 |
# File 'opal/opal/corelib/numeric.rb', line 255 def conj self end |
#divmod(rhs) ⇒ Object
440 441 442 443 444 445 |
# File 'opal/opal/corelib/numeric.rb', line 440 def divmod(rhs) q = (self / rhs).floor r = self % rhs [q, r] end |
#downto(finish, &block) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'opal/opal/corelib/numeric.rb', line 261 def downto(finish, &block) return enum_for :downto, finish unless block %x{ for (var i = self; i >= finish; i--) { if (block(i) === $breaker) { return $breaker.$v; } } } self end |
#even? ⇒ Boolean
278 279 280 |
# File 'opal/opal/corelib/numeric.rb', line 278 def even? `self % 2 === 0` end |
#finite? ⇒ Boolean
474 475 476 |
# File 'opal/opal/corelib/numeric.rb', line 474 def finite? `self != Infinity && self != -Infinity` end |
#floor ⇒ Object
282 283 284 |
# File 'opal/opal/corelib/numeric.rb', line 282 def floor `Math.floor(self)` end |
#gcd(other) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'opal/opal/corelib/numeric.rb', line 286 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
306 307 308 |
# File 'opal/opal/corelib/numeric.rb', line 306 def gcdlcm(other) [gcd, lcm] end |
#hash ⇒ Object
310 311 312 |
# File 'opal/opal/corelib/numeric.rb', line 310 def hash `self.toString()` end |
#infinite? ⇒ Boolean
478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'opal/opal/corelib/numeric.rb', line 478 def infinite? %x{ if (self == Infinity) { return +1; } else if (self == -Infinity) { return -1; } else { return nil; } } end |
#instance_of?(klass) ⇒ Boolean
328 329 330 331 332 333 334 |
# File 'opal/opal/corelib/numeric.rb', line 328 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
314 315 316 |
# File 'opal/opal/corelib/numeric.rb', line 314 def integer? `self % 1 === 0` end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
318 319 320 321 322 323 324 |
# File 'opal/opal/corelib/numeric.rb', line 318 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
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'opal/opal/corelib/numeric.rb', line 336 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
470 471 472 |
# File 'opal/opal/corelib/numeric.rb', line 470 def nan? `isNaN(self)` end |
#negative? ⇒ Boolean
496 497 498 |
# File 'opal/opal/corelib/numeric.rb', line 496 def negative? `1 / self < 0` end |
#next ⇒ Object Also known as: succ
355 356 357 |
# File 'opal/opal/corelib/numeric.rb', line 355 def next `self + 1` end |
#nonzero? ⇒ Boolean
359 360 361 |
# File 'opal/opal/corelib/numeric.rb', line 359 def nonzero? `self == 0 ? nil : self` end |
#odd? ⇒ Boolean
363 364 365 |
# File 'opal/opal/corelib/numeric.rb', line 363 def odd? `self % 2 !== 0` end |
#ord ⇒ Object
367 368 369 |
# File 'opal/opal/corelib/numeric.rb', line 367 def ord self end |
#positive? ⇒ Boolean
492 493 494 |
# File 'opal/opal/corelib/numeric.rb', line 492 def positive? `1 / self > 0` end |
#pred ⇒ Object
371 372 373 |
# File 'opal/opal/corelib/numeric.rb', line 371 def pred `self - 1` end |
#round ⇒ Object
375 376 377 |
# File 'opal/opal/corelib/numeric.rb', line 375 def round `Math.round(self)` end |
#send_coerced(method, other) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'opal/opal/corelib/numeric.rb', line 27 def send_coerced(method, other) type = case method when :+, :-, :*, :/, :%, :&, :|, :^, :** :operation when :>, :>=, :<, :<=, :<=> :comparison end a, b = coerce(other, type) a.__send__ method, b end |
#size ⇒ Object
Since bitwise operations are 32 bit, declare it to be so.
466 467 468 |
# File 'opal/opal/corelib/numeric.rb', line 466 def size 4 end |
#step(limit, step = 1, &block) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'opal/opal/corelib/numeric.rb', line 379 def step(limit, step = 1, &block) return enum_for :step, limit, step unless block raise ArgumentError, 'step cannot be 0' if `step == 0` %x{ var value = self; if (step > 0) { while (value <= limit) { block(value); value += step; } } else { while (value >= limit) { block(value); value += step; } } } self end |
#times(&block) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'opal/opal/corelib/numeric.rb', line 406 def times(&block) return enum_for :times unless block %x{ for (var i = 0; i < self; i++) { if (block(i) === $breaker) { return $breaker.$v; } } } self end |
#to_f ⇒ Object
420 421 422 |
# File 'opal/opal/corelib/numeric.rb', line 420 def to_f self end |
#to_i ⇒ Object Also known as: to_int
424 425 426 |
# File 'opal/opal/corelib/numeric.rb', line 424 def to_i `parseInt(self)` end |
#to_s(base = 10) ⇒ Object Also known as: inspect
430 431 432 433 434 435 436 |
# File 'opal/opal/corelib/numeric.rb', line 430 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(finish, &block) ⇒ Object
447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'opal/opal/corelib/numeric.rb', line 447 def upto(finish, &block) return enum_for :upto, finish unless block %x{ for (var i = self; i <= finish; i++) { if (block(i) === $breaker) { return $breaker.$v; } } } self end |
#zero? ⇒ Boolean
461 462 463 |
# File 'opal/opal/corelib/numeric.rb', line 461 def zero? `self == 0` end |
#|(other) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'opal/opal/corelib/numeric.rb', line 111 def |(other) %x{ if (other._isNumber) { return self | other; } else { return #{send_coerced :|, other}; } } end |
#~ ⇒ Object
214 215 216 |
# File 'opal/opal/corelib/numeric.rb', line 214 def ~ `~self` end |