Class: Numeric

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/source/ruby.rb

Overview

Numeric objects represent native JavaScript numbers. Class Numeric subsumes the methods of Ruby’s Float, Integer, Bignum, and Fixnum.

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #>, #>=, #between?

Instance Method Details

#%(n) ⇒ Object

call-seq:

num % other       -> numeric
num.modulo(other) -> numeric

Returns num modulo other. See Numeric#divmod for more information.



4165
4166
4167
# File 'lib/source/ruby.rb', line 4165

def %(n)
  `this%n`
end

#&(n) ⇒ Object

call-seq:

num & other -> integer

Performs bitwise AND between num and other. Floats are truncated to integers before operation.



4175
4176
4177
# File 'lib/source/ruby.rb', line 4175

def &(n)
  `this&n`
end

#*(n) ⇒ Object

call-seq:

num * other -> numeric

Multiplies num by other, then returns the result.



4225
4226
4227
# File 'lib/source/ruby.rb', line 4225

def *(n)
  `this*n`
end

#**(n) ⇒ Object

call-seq:

num ** n -> numeric

Raises num to the nth power, which may be negative or fractional.



4234
4235
4236
# File 'lib/source/ruby.rb', line 4234

def **(n)
  `Math.pow(this,n)`
end

#+(n) ⇒ Object

call-seq:

num + other -> numeric

Adds num to other, then returns the result.



4243
4244
4245
# File 'lib/source/ruby.rb', line 4243

def +(n)
  `this+n`
end

#+@Object

call-seq:

+num -> num

Unary Plus – returns the value of num.



4252
4253
4254
# File 'lib/source/ruby.rb', line 4252

def +@()
  `this`
end

#-(n) ⇒ Object

call-seq:

num - other -> numeric

Subtracts other from num, then returns the result.



4261
4262
4263
# File 'lib/source/ruby.rb', line 4261

def -(n)
  `this-n`
end

#-@Object

call-seq:

-num -> numeric

Unary Minus – returns the value of num negated.



4270
4271
4272
# File 'lib/source/ruby.rb', line 4270

def -@()
  `-this`
end

#/(n) ⇒ Object

call-seq:

num / other    -> integer
num.div(other) -> integer

Divides num by other, then truncates the result to an integer. (See also Numeric#quo.)



4216
4217
4218
# File 'lib/source/ruby.rb', line 4216

def /(n)
  `parseInt(this/n)`
end

#<<(n) ⇒ Object

call-seq:

num << n -> integer

Shifts num left n positions (right if n is negative). Floats are truncated to integers before operation.



4280
4281
4282
# File 'lib/source/ruby.rb', line 4280

def <<(n)
  `Math.floor(parseInt(this)*Math.pow(2,parseInt(n)))`
end

#<=>(n) ⇒ Object

call-seq:

num <=> numeric -> -1, 0, 1

Comparison – returns -1, 0, or 1 depending on whether num is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.



4301
4302
4303
4304
4305
4306
# File 'lib/source/ruby.rb', line 4301

def <=>(n)
  `if(n.constructor!=Number){return nil;}`
  `if(this>n){return 1;}`
  `if(this<n){return -1;}`
  return 0
end

#==(n) ⇒ Object

:nodoc:



4308
4309
4310
# File 'lib/source/ruby.rb', line 4308

def ==(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#===(n) ⇒ Object

:nodoc:



4312
4313
4314
# File 'lib/source/ruby.rb', line 4312

def ===(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#>>(n) ⇒ Object

call-seq:

num << n -> integer

Shifts num right n positions (left if n is negative). Floats are truncated to integers before operation.



4290
4291
4292
# File 'lib/source/ruby.rb', line 4290

def >>(n)
  `Math.floor(parseInt(this)/Math.pow(2,parseInt(n)))`
end

#[](n) ⇒ Object

call-seq:

num[n] -> 0, 1

Bit Reference – returns the nth bit in the binary representation of num, where num[0] is the least significant bit. Floats are truncated to integers before operation.

a = 1234
"%b" % a    #=> "10011010010"

15.downto(0) {|n| puts a[n] }

produces:

0000010011010010


4332
4333
4334
4335
4336
# File 'lib/source/ruby.rb', line 4332

def [](n)
  `var str=parseInt(this).toString(2)`
  `if(n>=str.length){return 0;}`
  `parseInt(str[(str.length-n-1)])`
end

#^(n) ⇒ Object

call-seq:

num ^ other -> integer

Performs bitwise XOR between num and other. Floats are truncated to integers before operation.



4195
4196
4197
# File 'lib/source/ruby.rb', line 4195

def ^(n)
  `this^n`
end

#absObject

call-seq:

num.abs -> numeric

Returns the absolute value of num.

-1234.abs   #=> 1234
1234.abs    #=> 1234


4346
4347
4348
# File 'lib/source/ruby.rb', line 4346

def abs
  `Math.abs(this)`
end

#ceilObject

call-seq:

num.ceil -> integer

Returns the smallest integer greater than or equal to num.



4355
4356
4357
# File 'lib/source/ruby.rb', line 4355

def ceil
  `Math.ceil(this)`
end

#chrObject

call-seq:

num.chr -> string

Returns a string containing the ASCII character represented by num. Floats are truncated to integers before operation.

65.chr    #=> "A"
?a.chr    #=> "a"


4368
4369
4370
# File 'lib/source/ruby.rb', line 4368

def chr
  `$q(String.fromCharCode(parseInt(this)))`
end

#coerce(n) ⇒ Object

call-seq:

num.coerce -> array

Returns an array containing num and other. This method is used by Ruby to handle mixed-type numeric operations and is included in Red for compatibility reasons only.



4379
4380
4381
# File 'lib/source/ruby.rb', line 4379

def coerce(n)
  `[this,n]`
end

#div(n) ⇒ Object

call-seq:

num./(other)   -> integer
num.div(other) -> integer

Performs division, then truncates the result to an integer. (See also Numeric#quo.)



4390
4391
4392
# File 'lib/source/ruby.rb', line 4390

def div(n)
  `parseInt(this/n)`
end

#divmodObject

FIX: Incomplete



4395
4396
# File 'lib/source/ruby.rb', line 4395

def divmod
end

#downto(limit) ⇒ Object

call-seq:

num.downto(limit) { |i| block } -> num

Iterates block, passing decreasing values from num down to and including limit, then returns num.

5.downto(1) { |n| puts "#{n}.." }    #=> 5

produces:

5..
4..
3..
2..
1..


4414
4415
4416
4417
# File 'lib/source/ruby.rb', line 4414

def downto(limit)
  `for(var i=this.valueOf();i>=limit;--i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':++i;break;default:throw(e);};};}`
  return self
end

#eql?(n) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


4419
4420
4421
# File 'lib/source/ruby.rb', line 4419

def eql?(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#equal?(n) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


4423
4424
4425
# File 'lib/source/ruby.rb', line 4423

def equal?(n) # :nodoc:
  `this.valueOf()===n.valueOf()`
end

#finite?Boolean

call-seq:

num.finite? -> true or false

Returns true if num is not infinite and num.nan? is false.

Returns:

  • (Boolean)


4432
4433
4434
4435
# File 'lib/source/ruby.rb', line 4432

def finite?
  `if(this.valueOf()===Infinity||this.valueOf()===-Infinity||this.toString()==='NaN'){return false;}`
  return true
end

#floorObject

call-seq:

num.floor -> integer

Returns the largest integer less than or equal to num.



4442
4443
4444
# File 'lib/source/ruby.rb', line 4442

def floor
  `Math.floor(this)`
end

#hashObject

:nodoc:



4446
4447
4448
# File 'lib/source/ruby.rb', line 4446

def hash # :nodoc:
  `'n_'+this`
end

#id2nameObject

FIX: Incomplete



4451
4452
# File 'lib/source/ruby.rb', line 4451

def id2name
end

#infinite?Boolean

call-seq:

num.infinite? -> -1, nil, 1

Returns -1, nil, or 1 depending on whether num is -infinity, finite, or infinity.

(0).infinite?       #=> nil
(-1/0).infinite?    #=> -1
(+1/0).infinite?    #=> 1

Returns:

  • (Boolean)


4464
4465
4466
4467
4468
# File 'lib/source/ruby.rb', line 4464

def infinite?
  `if(this.valueOf()===Infinity){return 1;}`
  `if(this.valueOf()===-Infinity){return -1;}`
  return nil
end

#integer?Boolean

call-seq:

num.integer? -> true or false

Returns true if num is an integer.

Returns:

  • (Boolean)


4475
4476
4477
# File 'lib/source/ruby.rb', line 4475

def integer?
  `this%1===0`
end

#modulo(n) ⇒ Object

call-seq:

num % other       -> numeric
num.modulo(other) -> numeric

Returns num modulo other. See Numeric#divmod for more information.



4486
4487
4488
# File 'lib/source/ruby.rb', line 4486

def modulo(n)
  `this%n`
end

#nan?Boolean

call-seq:

num.nan? -> true or false

Returns true if num is NaN.

(0/0).nan?    #=> true

Returns:

  • (Boolean)


4497
4498
4499
# File 'lib/source/ruby.rb', line 4497

def nan?
  `this.toString()==='NaN'`
end

#nextObject

call-seq:

num.next -> integer
num.succ -> integer

Returns the integer equal to num.truncate + 1.

num = 1.5
num = num.next    #=> 2
num = num.next    #=> 3


4511
4512
4513
# File 'lib/source/ruby.rb', line 4511

def next
  `parseInt(this)+1`
end

#nonzero?Boolean

call-seq:

num.nonzero? -> num or nil

Returns num if num is not zero, nil otherwise. This behavior is useful when chaining comparisons:

a = %w(z Bb bB bb BB a aA Aa AA A)
b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
b   #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]

Returns:

  • (Boolean)


4525
4526
4527
# File 'lib/source/ruby.rb', line 4525

def nonzero?
  `this.valueOf()===0?nil:this`
end

#quo(n) ⇒ Object

call-seq:

num.quo(numeric) -> float

Returns the floating point result of dividing num by numeric.

654321.quo(13731)      #=> 47.6528293642124
654321.quo(13731.24)   #=> 47.6519964693647


4537
4538
4539
# File 'lib/source/ruby.rb', line 4537

def quo(n)
  `this/n`
end

#remainderObject

FIX: Incomplete



4542
4543
# File 'lib/source/ruby.rb', line 4542

def remainder
end

#roundObject

call-seq:

num.round -> integer

Rounds num to the nearest integer.



4550
4551
4552
# File 'lib/source/ruby.rb', line 4550

def round
  `Math.round(this)`
end

#step(limit, step) ⇒ Object

call-seq:

num.step(limit, step ) {|i| block } -> num

Invokes block with the sequence of numbers starting at num, incremented by step on each call, then returns self. The loop finishes when the value to be passed to the block is greater than limit (if step is positive) or less than limit (if step is negative).

1.step(5, 2) { |i| puts i }
Math::E.step(Math::PI, 0.2) { |f| puts i }

produces:

1
3
5
2.71828182845905
2.91828182845905
3.11828182845905


4573
4574
4575
4576
4577
# File 'lib/source/ruby.rb', line 4573

def step(limit, step)
  `var i=this.valueOf()`
  `if(step>0){if(i<limit){for(;limit>=i;i+=step){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':i-=step;break;default:throw(e);};};};};}else{if(i>limit){for(;limit<=i;i+=step){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':i-=step;break;default:throw(e);};}};;};}`
  return self
end

#succObject

call-seq:

num.next -> integer
num.succ -> integer

Returns the integer equal to num.truncate + 1.

num = 1.5
num = num.succ    #=> 2
num = num.succ    #=> 3


4589
4590
4591
# File 'lib/source/ruby.rb', line 4589

def succ
  `parseInt(this)+1`
end

#timesObject

call-seq:

num.times { |i| block } -> num

Iterates block num times, passing in values from zero to num - 1, then returns num.

5.times do |i|
  puts i
end

produces:

0
1
2
3
4


4611
4612
4613
4614
# File 'lib/source/ruby.rb', line 4611

def times
  `for(var i=0,l=this.valueOf();i<l;++i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};}}`
  return self
end

#to_fObject

call-seq:

num.to_f -> float

Returns _self_.


4621
4622
4623
# File 'lib/source/ruby.rb', line 4621

def to_f
  `this`
end

#to_iObject

call-seq:

num.to_i     -> integer
num.to_int   -> integer
num.truncate -> integer

Returns num truncated to an integer.



4632
4633
4634
# File 'lib/source/ruby.rb', line 4632

def to_i
  `parseInt(this)`
end

#to_intObject

call-seq:

num.to_i     -> integer
num.to_int   -> integer
num.truncate -> integer

Returns num truncated to an integer.



4643
4644
4645
# File 'lib/source/ruby.rb', line 4643

def to_int
  `parseInt(this)`
end

#to_s(base = 10) ⇒ Object

call-seq:

num.to_s(base = 10) -> aString

Returns a string containing the representation of num radix base (between 2 and 36).

12345.to_s       #=> "12345"
12345.to_s(2)    #=> "11000000111001"
12345.to_s(8)    #=> "30071"
12345.to_s(10)   #=> "12345"
12345.to_s(16)   #=> "3039"
12345.to_s(36)   #=> "9ix"


4660
4661
4662
# File 'lib/source/ruby.rb', line 4660

def to_s(base = 10)
  `$q(this.toString(base))`
end

#to_symObject

FIX: Incomplete



4665
4666
# File 'lib/source/ruby.rb', line 4665

def to_sym
end

#truncateObject

call-seq:

num.to_i     -> integer
num.to_int   -> integer
num.truncate -> integer

Returns num truncated to an integer.



4675
4676
4677
# File 'lib/source/ruby.rb', line 4675

def truncate
  `parseInt(this)`
end

#upto(limit) ⇒ Object

call-seq:

num.upto(limit) { |i| block } -> num

Iterates block, passing increasing values from num up to and including limit, then returns num.

98.upto(100) { |n| puts "#{n}.." }    #=> 98

produces:

98..
99..
100..


4693
4694
4695
4696
# File 'lib/source/ruby.rb', line 4693

def upto(limit)
  `for(var i=this.valueOf();i<=limit;++i){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};}`
  return self
end

#zero?Boolean

call-seq:

num.zero? -> true or false

Returns true if num is zero.

Returns:

  • (Boolean)


4703
4704
4705
# File 'lib/source/ruby.rb', line 4703

def zero?
  `this.valueOf()===0`
end

#|(n) ⇒ Object

call-seq:

num | other -> integer

Performs bitwise OR between num and other. Floats are truncated to integers before operation.



4185
4186
4187
# File 'lib/source/ruby.rb', line 4185

def |(n)
  `this|n`
end

#~Object

call-seq:

~num -> integer

Performs bitwise NOT on num. Floats are truncated to integers before operation.



4205
4206
4207
# File 'lib/source/ruby.rb', line 4205

def ~
  `~this`
end