Class: RubyJS.Numeric

Defined in: lib/corelib/numeric.coffee
Inherits: RubyJS.Object

Direct Known Subclasses

RubyJS.Float, RubyJS.Integer

Class Method Summary

Instance Method Summary

Inherited Method Summary

Methods inherited from RubyJS.Object

.include, .__add_default_aliases__, #send, #respond_to, #to_enum, #tap

Class Method Details

+ (void) isNumeric(obj)

true for: - number primitive - native Number object - valueOf() returns a number primitive

+ (void) try_convert(obj)

+ (void) new(value)

+ (void) typecast(value)

Instance Method Details

'-instance'> - (void) '<=>'(other)

- (R.Numeric) abs()

Returns the absolute value of num.

Examples:

  R(12).abs()         #=> 12
  (-34.56).abs()      #=> 34.56
  R(-34.56).abs()     #=> 34.56

Returns:

  • R.Numeric

- (R.Numeric) abs2()

Returns square of self.

Returns:

  • R.Numeric

- (R.Fixnum) ceil()

Returns the smallest Integer greater than or equal to num. Class Numeric achieves this by converting itself to a Float then invoking Float#ceil.

Examples:

  R(1).ceil()      #=> 1
  R(1.2).ceil()    #=> 2
  R(-1.2).ceil()   #=> -1
  R(-1.0).ceil()   #=> -1

Returns:

  • R.Fixnum

- (void) coerce(other)

TODO: this needs some serious love

If a Numeric is the same type as num, returns an array containing aNumeric and num. Otherwise, returns an array with both aNumeric and num represented as Float objects. This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator.

Examples:

  R(1).coerce(2.5)   #=> [2.5, 1.0]
  R(1.2).coerce(3)   #=> [3.0, 1.2]
  R(1).coerce(2)     #=> [2, 1]

- (R.Float) div(other)

Uses / to perform division, then converts the result to an integer. numeric does not define the / operator; this is left to subclasses.

Equivalent to num.divmod(aNumeric).

See {R.Numeric#divmod}

Returns:

  • R.Float

- (void) divmod(other)

- (Boolean) eql(other)

Returns true if num and numeric are the same type and have equal values.

Examples:

  new R.Fixnum(1).equals(new R.Float(1.0))  #=> true
  new R.Fixnum(1).eql(new R.Float(1.0))     #=> false
  new R.Float(1).eql(new R.Float(1.0))      #=> true

Returns:

  • Boolean

- (R.String) inspect()

Returns:

  • R.String

- (R.Float) fdiv(other)

Returns float division.

Returns:

  • R.Float

- (R.Fixnum) floor()

Returns the largest integer less than or equal to num. Numeric implements this by converting anInteger to a Float and invoking Float#floor.

Examples:

  R( 1).floor()   #=> 1
  R(-1).floor()   #=> -1

Returns:

  • R.Fixnum

- (void) magnitude()

Returns the absolute value of num.

@alias #abs

- (void) modulo(other)

Alias to #divmod

@alias #divmod

- (null, this) nonzero()

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

Returns:

  • null, this

- (void) quo(other)

Returns most exact division (rational for integers, float for floats).

- (void) rect()

Returns an array; [num, 0].

- (void) remainder(other)

x.remainder(y) means x-y*(x/y).truncate

See also:

  • {R.Numeric#divmod}

- (R.Numeric) round(n)

Rounds num to a given precision in decimal digits (default 0 digits). Precision may be negative. Returns a floating point number when ndigits is more than zero. Numeric implements this by converting itself to a Float and invoking Float#round.

Returns:

  • R.Numeric

- (this, R.Enumerator) step(limit, step = 1, block)

Invokes block with the sequence of numbers starting at num, incremented by step (default 1) on each call. 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). If all the arguments are integers, the loop operates using an integer counter. If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed floor(n + n*epsilon)+ 1 times, where n = (limit - num)/step. Otherwise, the loop starts at num, uses either the < or > operator to compare the counter against limit, and increments itself using the + operator.

If no block is given, an enumerator is returned instead.

Examples:

  R(1).step(10, 2, function (i) { R.puts(i)} )
  R(Math.E).step(Math.PI, 0.2, function (i) { R.puts(i)} )
  # produces:
  # 1 3 5 7 9
  # 2.71828182845905 2.91828182845905 3.11828182845905

Returns:

  • this, R.Enumerator

- (R.Fixnum) truncate()

Returns num truncated to an integer. Numeric implements this by converting its value to a float and invoking Float#truncate.

Returns:

  • R.Fixnum

- (R.Numeric) uminus()

Unary Minus—Returns the receiver’s value, negated.

Returns:

  • R.Numeric

- (Boolean) zero()

Returns true if num has a zero value.

Returns:

  • Boolean