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?)
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #>>(count) ⇒ Object
- #[](bit) ⇒ Object
- #^(other) ⇒ Object
- #__id__ ⇒ Object (also: #object_id)
- #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
- #equal?(other) ⇒ Boolean
- #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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'opal/opal/corelib/numeric.rb', line 89 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
105 106 107 108 109 110 111 112 113 114 |
# File 'opal/opal/corelib/numeric.rb', line 105 def &(other) %x{ if (other.$$is_number) { return self & other; } else { return #{send_coerced :&, other}; } } end |
#*(other) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'opal/opal/corelib/numeric.rb', line 67 def *(other) %x{ if (other.$$is_number) { return self * other; } else { return #{send_coerced :*, other}; } } end |
#**(other) ⇒ Object
227 228 229 230 231 232 233 234 235 236 |
# File 'opal/opal/corelib/numeric.rb', line 227 def **(other) %x{ if (other.$$is_number) { return Math.pow(self, other); } else { return #{send_coerced :**, other}; } } end |
#+(other) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'opal/opal/corelib/numeric.rb', line 45 def +(other) %x{ if (other.$$is_number) { return self + other; } else { return #{send_coerced :+, other}; } } end |
#+@ ⇒ Object
215 216 217 |
# File 'opal/opal/corelib/numeric.rb', line 215 def +@ `+self` end |
#-(other) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'opal/opal/corelib/numeric.rb', line 56 def -(other) %x{ if (other.$$is_number) { return self - other; } else { return #{send_coerced :-, other}; } } end |
#-@ ⇒ Object
219 220 221 |
# File 'opal/opal/corelib/numeric.rb', line 219 def -@ `-self` end |
#/(other) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'opal/opal/corelib/numeric.rb', line 78 def /(other) %x{ if (other.$$is_number) { return self / other; } else { return #{send_coerced :/, other}; } } end |
#<(other) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'opal/opal/corelib/numeric.rb', line 138 def <(other) %x{ if (other.$$is_number) { return self < other; } else { return #{send_coerced :<, other}; } } end |
#<<(count) ⇒ Object
195 196 197 198 199 |
# File 'opal/opal/corelib/numeric.rb', line 195 def <<(count) count = Opal.coerce_to! count, Integer, :to_int `#{count} > 0 ? self << #{count} : self >> -#{count}` end |
#<=(other) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'opal/opal/corelib/numeric.rb', line 149 def <=(other) %x{ if (other.$$is_number) { return self <= other; } else { return #{send_coerced :<=, other}; } } end |
#<=>(other) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'opal/opal/corelib/numeric.rb', line 182 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?
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'opal/opal/corelib/numeric.rb', line 238 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
160 161 162 163 164 165 166 167 168 169 |
# File 'opal/opal/corelib/numeric.rb', line 160 def >(other) %x{ if (other.$$is_number) { return self > other; } else { return #{send_coerced :>, other}; } } end |
#>=(other) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'opal/opal/corelib/numeric.rb', line 171 def >=(other) %x{ if (other.$$is_number) { return self >= other; } else { return #{send_coerced :>=, other}; } } end |
#>>(count) ⇒ Object
201 202 203 204 205 |
# File 'opal/opal/corelib/numeric.rb', line 201 def >>(count) count = Opal.coerce_to! count, Integer, :to_int `#{count} > 0 ? self >> #{count} : self << -#{count}` end |
#[](bit) ⇒ Object
207 208 209 210 211 212 213 |
# File 'opal/opal/corelib/numeric.rb', line 207 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
127 128 129 130 131 132 133 134 135 136 |
# File 'opal/opal/corelib/numeric.rb', line 127 def ^(other) %x{ if (other.$$is_number) { return self ^ other; } else { return #{send_coerced :^, other}; } } end |
#__id__ ⇒ Object Also known as: object_id
8 9 10 |
# File 'opal/opal/corelib/numeric.rb', line 8 def __id__ `(self * 2) + 1` end |
#abs ⇒ Object Also known as: magnitude
252 253 254 |
# File 'opal/opal/corelib/numeric.rb', line 252 def abs `Math.abs(self)` end |
#ceil ⇒ Object
256 257 258 |
# File 'opal/opal/corelib/numeric.rb', line 256 def ceil `Math.ceil(self)` end |
#chr(encoding = undefined) ⇒ Object
260 261 262 |
# File 'opal/opal/corelib/numeric.rb', line 260 def chr(encoding=undefined) `String.fromCharCode(self)` end |
#coerce(other, type = :operation) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'opal/opal/corelib/numeric.rb', line 13 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
264 265 266 |
# File 'opal/opal/corelib/numeric.rb', line 264 def conj self end |
#divmod(rhs) ⇒ Object
458 459 460 461 462 463 |
# File 'opal/opal/corelib/numeric.rb', line 458 def divmod(rhs) q = (self / rhs).floor r = self % rhs [q, r] end |
#downto(finish, &block) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'opal/opal/corelib/numeric.rb', line 270 def downto(finish, &block) return enum_for :downto, finish unless block %x{ if (!finish.$$is_number) { #{raise ArgumentError, "comparison of #{self.class} with #{finish.class} failed"} } for (var i = self; i >= finish; i--) { if (block(i) === $breaker) { return $breaker.$v; } } } self end |
#equal?(other) ⇒ Boolean
289 290 291 |
# File 'opal/opal/corelib/numeric.rb', line 289 def equal?(other) self == other || `isNaN(self) && isNaN(other)` end |
#even? ⇒ Boolean
293 294 295 |
# File 'opal/opal/corelib/numeric.rb', line 293 def even? `self % 2 === 0` end |
#finite? ⇒ Boolean
495 496 497 |
# File 'opal/opal/corelib/numeric.rb', line 495 def finite? `self != Infinity && self != -Infinity` end |
#floor ⇒ Object
297 298 299 |
# File 'opal/opal/corelib/numeric.rb', line 297 def floor `Math.floor(self)` end |
#gcd(other) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'opal/opal/corelib/numeric.rb', line 301 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
321 322 323 |
# File 'opal/opal/corelib/numeric.rb', line 321 def gcdlcm(other) [gcd, lcm] end |
#hash ⇒ Object
325 326 327 |
# File 'opal/opal/corelib/numeric.rb', line 325 def hash `'Numeric:'+self.toString()` end |
#infinite? ⇒ Boolean
499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'opal/opal/corelib/numeric.rb', line 499 def infinite? %x{ if (self == Infinity) { return +1; } else if (self == -Infinity) { return -1; } else { return nil; } } end |
#instance_of?(klass) ⇒ Boolean
343 344 345 346 347 348 349 |
# File 'opal/opal/corelib/numeric.rb', line 343 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
329 330 331 |
# File 'opal/opal/corelib/numeric.rb', line 329 def integer? `self % 1 === 0` end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
333 334 335 336 337 338 339 |
# File 'opal/opal/corelib/numeric.rb', line 333 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
351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'opal/opal/corelib/numeric.rb', line 351 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
491 492 493 |
# File 'opal/opal/corelib/numeric.rb', line 491 def nan? `isNaN(self)` end |
#negative? ⇒ Boolean
517 518 519 |
# File 'opal/opal/corelib/numeric.rb', line 517 def negative? `1 / self < 0` end |
#next ⇒ Object Also known as: succ
370 371 372 |
# File 'opal/opal/corelib/numeric.rb', line 370 def next `self + 1` end |
#nonzero? ⇒ Boolean
374 375 376 |
# File 'opal/opal/corelib/numeric.rb', line 374 def nonzero? `self == 0 ? nil : self` end |
#odd? ⇒ Boolean
378 379 380 |
# File 'opal/opal/corelib/numeric.rb', line 378 def odd? `self % 2 !== 0` end |
#ord ⇒ Object
382 383 384 |
# File 'opal/opal/corelib/numeric.rb', line 382 def ord self end |
#positive? ⇒ Boolean
513 514 515 |
# File 'opal/opal/corelib/numeric.rb', line 513 def positive? `1 / self > 0` end |
#pred ⇒ Object
386 387 388 |
# File 'opal/opal/corelib/numeric.rb', line 386 def pred `self - 1` end |
#round(ndigits = 0) ⇒ Object
390 391 392 393 394 395 |
# File 'opal/opal/corelib/numeric.rb', line 390 def round(ndigits=0) %x{ var scale = Math.pow(10, ndigits); return Math.round(self * scale) / scale; } end |
#send_coerced(method, other) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'opal/opal/corelib/numeric.rb', line 32 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.
487 488 489 |
# File 'opal/opal/corelib/numeric.rb', line 487 def size 4 end |
#step(limit, step = 1, &block) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'opal/opal/corelib/numeric.rb', line 397 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
424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'opal/opal/corelib/numeric.rb', line 424 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
438 439 440 |
# File 'opal/opal/corelib/numeric.rb', line 438 def to_f self end |
#to_i ⇒ Object Also known as: to_int
442 443 444 |
# File 'opal/opal/corelib/numeric.rb', line 442 def to_i `parseInt(self)` end |
#to_s(base = 10) ⇒ Object Also known as: inspect
448 449 450 451 452 453 454 |
# File 'opal/opal/corelib/numeric.rb', line 448 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
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'opal/opal/corelib/numeric.rb', line 465 def upto(finish, &block) return enum_for :upto, finish unless block %x{ if (!finish.$$is_number) { #{raise ArgumentError, "comparison of #{self.class} with #{finish.class} failed"} } for (var i = self; i <= finish; i++) { if (block(i) === $breaker) { return $breaker.$v; } } } self end |
#zero? ⇒ Boolean
482 483 484 |
# File 'opal/opal/corelib/numeric.rb', line 482 def zero? `self == 0` end |
#|(other) ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'opal/opal/corelib/numeric.rb', line 116 def |(other) %x{ if (other.$$is_number) { return self | other; } else { return #{send_coerced :|, other}; } } end |
#~ ⇒ Object
223 224 225 |
# File 'opal/opal/corelib/numeric.rb', line 223 def ~ `~self` end |