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(encoding = undefined) ⇒ 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(ndigits = 0) ⇒ 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.$$is_number) { 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.$$is_number) { 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.$$is_number) { return self * other; } else { return #{send_coerced :*, other}; } } end |
#**(other) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'opal/opal/corelib/numeric.rb', line 222 def **(other) %x{ if (other.$$is_number) { 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.$$is_number) { 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
51 52 53 54 55 56 57 58 59 60 |
# File 'opal/opal/corelib/numeric.rb', line 51 def -(other) %x{ if (other.$$is_number) { return self - other; } else { return #{send_coerced :-, other}; } } end |
#-@ ⇒ Object
214 215 216 |
# File 'opal/opal/corelib/numeric.rb', line 214 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.$$is_number) { 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.$$is_number) { return self < other; } else { return #{send_coerced :<, other}; } } end |
#<<(count) ⇒ Object
190 191 192 193 194 |
# File 'opal/opal/corelib/numeric.rb', line 190 def <<(count) count = Opal.coerce_to! count, Integer, :to_int `#{count} > 0 ? self << #{count} : self >> -#{count}` 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.$$is_number) { 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.$$is_number) { return self > other ? 1 : (self < other ? -1 : 0); } else { return #{send_coerced :<=>, other}; } } rescue ArgumentError nil end |
#==(other) ⇒ Object Also known as: eql?, equal?
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'opal/opal/corelib/numeric.rb', line 233 def ==(other) %x{ if (other.$$is_number) { 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.$$is_number) { 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.$$is_number) { return self >= other; } else { return #{send_coerced :>=, other}; } } end |
#>>(count) ⇒ Object
196 197 198 199 200 |
# File 'opal/opal/corelib/numeric.rb', line 196 def >>(count) count = Opal.coerce_to! count, Integer, :to_int `#{count} > 0 ? self >> #{count} : self << -#{count}` end |
#[](bit) ⇒ Object
202 203 204 205 206 207 208 |
# File 'opal/opal/corelib/numeric.rb', line 202 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.$$is_number) { return self ^ other; } else { return #{send_coerced :^, other}; } } end |
#abs ⇒ Object Also known as: magnitude
247 248 249 |
# File 'opal/opal/corelib/numeric.rb', line 247 def abs `Math.abs(self)` end |
#ceil ⇒ Object
251 252 253 |
# File 'opal/opal/corelib/numeric.rb', line 251 def ceil `Math.ceil(self)` end |
#chr(encoding = undefined) ⇒ Object
255 256 257 |
# File 'opal/opal/corelib/numeric.rb', line 255 def chr(encoding=undefined) `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.$$is_number) { return [self, other]; } else { return #{other.coerce(self)}; } } rescue case type when :operation raise TypeError, "#{other.class} can't be coerced into Numeric" when :comparison raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end end |
#conj ⇒ Object Also known as: conjugate
259 260 261 |
# File 'opal/opal/corelib/numeric.rb', line 259 def conj self end |
#divmod(rhs) ⇒ Object
447 448 449 450 451 452 |
# File 'opal/opal/corelib/numeric.rb', line 447 def divmod(rhs) q = (self / rhs).floor r = self % rhs [q, r] end |
#downto(finish, &block) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'opal/opal/corelib/numeric.rb', line 265 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
282 283 284 |
# File 'opal/opal/corelib/numeric.rb', line 282 def even? `self % 2 === 0` end |
#finite? ⇒ Boolean
481 482 483 |
# File 'opal/opal/corelib/numeric.rb', line 481 def finite? `self != Infinity && self != -Infinity` end |
#floor ⇒ Object
286 287 288 |
# File 'opal/opal/corelib/numeric.rb', line 286 def floor `Math.floor(self)` end |
#gcd(other) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'opal/opal/corelib/numeric.rb', line 290 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
310 311 312 |
# File 'opal/opal/corelib/numeric.rb', line 310 def gcdlcm(other) [gcd, lcm] end |
#hash ⇒ Object
314 315 316 |
# File 'opal/opal/corelib/numeric.rb', line 314 def hash `'Numeric:'+self.toString()` end |
#infinite? ⇒ Boolean
485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'opal/opal/corelib/numeric.rb', line 485 def infinite? %x{ if (self == Infinity) { return +1; } else if (self == -Infinity) { return -1; } else { return nil; } } end |
#instance_of?(klass) ⇒ Boolean
332 333 334 335 336 337 338 |
# File 'opal/opal/corelib/numeric.rb', line 332 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
318 319 320 |
# File 'opal/opal/corelib/numeric.rb', line 318 def integer? `self % 1 === 0` end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
322 323 324 325 326 327 328 |
# File 'opal/opal/corelib/numeric.rb', line 322 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
340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'opal/opal/corelib/numeric.rb', line 340 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
477 478 479 |
# File 'opal/opal/corelib/numeric.rb', line 477 def nan? `isNaN(self)` end |
#negative? ⇒ Boolean
503 504 505 |
# File 'opal/opal/corelib/numeric.rb', line 503 def negative? `1 / self < 0` end |
#next ⇒ Object Also known as: succ
359 360 361 |
# File 'opal/opal/corelib/numeric.rb', line 359 def next `self + 1` end |
#nonzero? ⇒ Boolean
363 364 365 |
# File 'opal/opal/corelib/numeric.rb', line 363 def nonzero? `self == 0 ? nil : self` end |
#odd? ⇒ Boolean
367 368 369 |
# File 'opal/opal/corelib/numeric.rb', line 367 def odd? `self % 2 !== 0` end |
#ord ⇒ Object
371 372 373 |
# File 'opal/opal/corelib/numeric.rb', line 371 def ord self end |
#positive? ⇒ Boolean
499 500 501 |
# File 'opal/opal/corelib/numeric.rb', line 499 def positive? `1 / self > 0` end |
#pred ⇒ Object
375 376 377 |
# File 'opal/opal/corelib/numeric.rb', line 375 def pred `self - 1` end |
#round(ndigits = 0) ⇒ Object
379 380 381 382 383 384 |
# File 'opal/opal/corelib/numeric.rb', line 379 def round(ndigits=0) %x{ var scale = Math.pow(10, ndigits); return Math.round(self * scale) / scale; } 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.
473 474 475 |
# File 'opal/opal/corelib/numeric.rb', line 473 def size 4 end |
#step(limit, step = 1, &block) ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'opal/opal/corelib/numeric.rb', line 386 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
413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'opal/opal/corelib/numeric.rb', line 413 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
427 428 429 |
# File 'opal/opal/corelib/numeric.rb', line 427 def to_f self end |
#to_i ⇒ Object Also known as: to_int
431 432 433 |
# File 'opal/opal/corelib/numeric.rb', line 431 def to_i `parseInt(self)` end |
#to_s(base = 10) ⇒ Object Also known as: inspect
437 438 439 440 441 442 443 |
# File 'opal/opal/corelib/numeric.rb', line 437 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
454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'opal/opal/corelib/numeric.rb', line 454 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
468 469 470 |
# File 'opal/opal/corelib/numeric.rb', line 468 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.$$is_number) { return self | other; } else { return #{send_coerced :|, other}; } } end |
#~ ⇒ Object
218 219 220 |
# File 'opal/opal/corelib/numeric.rb', line 218 def ~ `~self` end |