Ruby Numeric Functions

EditRocket provides the following information on Numeric functions in the Ruby source code builder.

abs - Returns the absolute value of num.

angle() - "Alias for #arg"

arg() - See Complex#arg.

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.

coerce(numeric) - "If aNumeric 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."

conj() - "Alias for #conjugate"

conjugate() - "See Complex#conjugate (short answer: returns self)."

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

divmod( aNumeric ) - Returns an array containing the quotient and modulus obtained by dividing num by aNumeric. If q, r = x.divmod(y), then

eql?(numeric) - Returns true if num and numeric are the same type and have equal values.

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

im() - Returns a Complex number (0,self).

imag() - "Alias for #image"

image() - The imaginary part of a complex number, i.e. 0.

integer? - Returns true if num is an Integer (including Fixnum and Bignum).

modulo(numeric) - Equivalent to num.divmod(aNumeric)[1].

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

polar() - See Complex#polar.

quo(numeric) - Equivalent to Numeric#/, but overridden in subclasses.

real() - The real part of a complex number, i.e. self.

remainder(numeric) - If num and numeric have different signs, returns mod-numeric; otherwise, returns mod. In both cases mod is the value num.modulo(numeric). The differences between remainder and modulo (%) are shown in the table under Numeric#divmod.

round - Rounds num to the nearest integer. Numeric implements this by converting itself to a Float and invoking Float#round.

singleton_method_added(p1) - Trap attempts to add methods to Numeric objects. Always raises a TypeError

step(limit, step ) - Invokes block with the sequence of numbers starting at num, incremented by step 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.

to_int - Invokes the child class's to_i method to convert num to an integer.

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

zero? - Returns true if num has a zero value.