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.



4125
4126
4127
# File 'lib/source/ruby.rb', line 4125

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.



4135
4136
4137
# File 'lib/source/ruby.rb', line 4135

def &(n)
  `this&n`
end

#*(n) ⇒ Object

call-seq:

num * other -> numeric

Multiplies num by other, then returns the result.



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

def *(n)
  `this*n`
end

#**(n) ⇒ Object

call-seq:

num ** n -> numeric

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



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

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

#+(n) ⇒ Object

call-seq:

num + other -> numeric

Adds num to other, then returns the result.



4203
4204
4205
# File 'lib/source/ruby.rb', line 4203

def +(n)
  `this+n`
end

#+@Object

call-seq:

+num -> num

Unary Plus – returns the value of num.



4212
4213
4214
# File 'lib/source/ruby.rb', line 4212

def +@()
  `this`
end

#-(n) ⇒ Object

call-seq:

num - other -> numeric

Subtracts other from num, then returns the result.



4221
4222
4223
# File 'lib/source/ruby.rb', line 4221

def -(n)
  `this-n`
end

#-@Object

call-seq:

-num -> numeric

Unary Minus – returns the value of num negated.



4230
4231
4232
# File 'lib/source/ruby.rb', line 4230

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.)



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

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.



4240
4241
4242
# File 'lib/source/ruby.rb', line 4240

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.



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

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

#==(n) ⇒ Object

:nodoc:



4268
4269
4270
# File 'lib/source/ruby.rb', line 4268

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

#===(n) ⇒ Object

:nodoc:



4272
4273
4274
# File 'lib/source/ruby.rb', line 4272

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.



4250
4251
4252
# File 'lib/source/ruby.rb', line 4250

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


4292
4293
4294
4295
4296
# File 'lib/source/ruby.rb', line 4292

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.



4155
4156
4157
# File 'lib/source/ruby.rb', line 4155

def ^(n)
  `this^n`
end

#absObject

call-seq:

num.abs -> numeric

Returns the absolute value of num.

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


4306
4307
4308
# File 'lib/source/ruby.rb', line 4306

def abs
  `Math.abs(this)`
end

#ceilObject

call-seq:

num.ceil -> integer

Returns the smallest integer greater than or equal to num.



4315
4316
4317
# File 'lib/source/ruby.rb', line 4315

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"


4328
4329
4330
# File 'lib/source/ruby.rb', line 4328

def chr
  `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.



4339
4340
4341
# File 'lib/source/ruby.rb', line 4339

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.)



4350
4351
4352
# File 'lib/source/ruby.rb', line 4350

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

#divmodObject

FIX: Incomplete



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

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..


4374
4375
4376
4377
# File 'lib/source/ruby.rb', line 4374

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._value;break;case 'redo':++i;break;default:throw(e);};};}`
  return self
end

#eql?(n) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


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

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

#equal?(n) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


4383
4384
4385
# File 'lib/source/ruby.rb', line 4383

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)


4392
4393
4394
4395
# File 'lib/source/ruby.rb', line 4392

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.



4402
4403
4404
# File 'lib/source/ruby.rb', line 4402

def floor
  `Math.floor(this)`
end

#hashObject

:nodoc:



4406
4407
4408
# File 'lib/source/ruby.rb', line 4406

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

#id2nameObject

FIX: Incomplete



4411
4412
# File 'lib/source/ruby.rb', line 4411

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)


4424
4425
4426
4427
4428
# File 'lib/source/ruby.rb', line 4424

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)


4435
4436
4437
# File 'lib/source/ruby.rb', line 4435

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.



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

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)


4457
4458
4459
# File 'lib/source/ruby.rb', line 4457

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


4471
4472
4473
# File 'lib/source/ruby.rb', line 4471

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)


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

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


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

def quo(n)
  `this/n`
end

#remainderObject

FIX: Incomplete



4502
4503
# File 'lib/source/ruby.rb', line 4502

def remainder
end

#roundObject

call-seq:

num.round -> integer

Rounds num to the nearest integer.



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

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


4533
4534
4535
4536
4537
# File 'lib/source/ruby.rb', line 4533

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._value;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._value;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


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

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


4571
4572
4573
4574
# File 'lib/source/ruby.rb', line 4571

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._value;break;case 'redo':--i;break;default:throw(e);};}}`
  return self
end

#to_fObject

call-seq:

num.to_f -> float

Returns _self_.


4581
4582
4583
# File 'lib/source/ruby.rb', line 4581

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.



4592
4593
4594
# File 'lib/source/ruby.rb', line 4592

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.



4603
4604
4605
# File 'lib/source/ruby.rb', line 4603

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"


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

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

#to_symObject

FIX: Incomplete



4625
4626
# File 'lib/source/ruby.rb', line 4625

def to_sym
end

#truncateObject

call-seq:

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

Returns num truncated to an integer.



4635
4636
4637
# File 'lib/source/ruby.rb', line 4635

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..


4653
4654
4655
4656
# File 'lib/source/ruby.rb', line 4653

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._value;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)


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

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.



4145
4146
4147
# File 'lib/source/ruby.rb', line 4145

def |(n)
  `this|n`
end

#~Object

call-seq:

~num -> integer

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



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

def ~
  `~this`
end