Wikia

Ruby Wiki

Float

Talk0
27pages on
this wiki

Float is the class of floating point numbers. It uses the double type of the underlying C environment.

Inherits: Numeric.

Mixins: Comparable (from Numeric), Precision.

ConstantsEdit

All constants are equivalent to ones defined by Standard C in its limits.h header, but without the DBL_ prefix. That is, Float::MIN_EXP is defined as the value of DBL_MIN_EXP in the underlying C environment. The single exception is Float::ROUNDS, because this is common to all C floating-point types and only FLT_ROUNDS is defined (there is no DBL_ROUNDS). As such, some of the descriptions are taken from the ISO C Standard, ISO/IEC 9899:1999. Many are paraphrased for clarity. Where any conflict exists between the descriptions given here and that standard, the standard shall prevail.

ConstantDescription
ROUNDSAn integer constant giving the rounding mode for floating point arithmetic:
  • -1: unknown
  • 0: towards zero
  • 1: to nearest
  • 2: toward positive infinity
  • 3: toward negative infinity
  • other: implementation-defined
RADIXRadix of the floating point type
MANT_DIGNumber of base-RADIX digits in the floating point significand
DIGMaximum number of decimal digits that can be converted to a Float, converted back again, and rounded to DIG digits using round to nearest, without changing the decimal digits
MIN_EXPMinimum negative integer such that RADIX to that power is a normalized floating point number
MAX_EXPMaximum integer such that RADIX to that power is a representable finite floating point number
MIN_10_EXPMinimum negative integer such that 10 to that power is a normalized floating point number
MAX_10_EXPMaximum integer such that 10 to that power is a representable finite floating point number
MINMinimum normalized finite floating point number
MAXMaximum representable finite floating point number
EPSILONThe difference between 1 and the next higher representable floating point number

MethodsEdit

MethodParametersDescription
==1Returns true if self is equal to the parameter
>1Returns true if self is greater than the parameter
>=1Returns true if self is greater than or equal to the parameter
<1Returns true if self is less than the parameter
<=1Returns true if self is less than or equal to the parameter
<=>1"Spaceship" operator. Returns -1 if self is less than the parameter, 0 if equal, +1 if greater.
-@0Unary minus. Returns the negation of self.
+@0Unary plus. Returns self unchanged.
+1Addition operator. Returns the sum of self and the single parameter.
-1Subtraction operator. Returns the difference of self and the single parameter.
*1Multiplication operator. Returns the product of self and the single parameter.
/1Division operator. Returns the quotient of self and the single parameter.
%1Modulo operator. Returns the remainder from dividing self by the single paramter. The remainder is determined by rounding the quotient downward and has the same sign as the modulus.
**1Power operator. Returns self raised to the power of the single parameter.
abs0Returns the absolute value of self.
between?2self.between(min, max) returns true if self is not less than min and not greater than max. The limits are part of the range, so 2.between?(2, 6) returns true.
ceil0Returns the least integer that is not less than self.
coerce1Because self is always a Float in this case, self.coerce(other) returns [ other.to_f, self ].
divmod1self.divmod(other) returns an Array containing the integer quotient and the remainder, in that order, resulting from dividing self by other. The quotient is an Integer and is rounded downward; the remainder is either an Integer or a Float and satisfies (quo*other+rem) == self. This is equivalent to [self.div(other), self.modulo(other)], except that self and other are evaluated only once.
div1self.div(other) returns the integer quotient that results from dividing self by other. The quotient is an Integer and is rounded down. Equivalent to self.quo(other).floor and to self.divmod(other)[0].
eql?1self.eql?(other) returns true if self and other have the same type and equal values.
finite?0Returns true if self is a finite floating point number (neither an infinity nor a NaN).
floor0Returns the greatest integer that is not greater than self.
hash0Returns a hash for use by Hash objects.
infinite?0Returns true if self is a floating-point infinity.
integer?0Returns false.
modulo1self.modulo(other) returns the remainder that results from dividing self by other. The remainder has the same sign as other.
nan?0Returns true if self is a NaN.
nonzero?0Returns true if self is not equal to zero.
prec1self.prec(klass) converts self to an object of class klass, using the induced_from singleton method of that class.
prec_f1self.prec_f is equivalent to self.prec(Float).
prec_i1self.prec_i is equivalent to self.prec(Integer).
quo1self.quo(other) returns the result of self divided by other as a Float.
remainder1self.remainder(other) returns the remainder that results from dividing self by other. Different from modulo in that the remainder is derived by rounding the quotient toward zero rather than downward and has the same sign as self; the sign of other is ignored.
round0Rounds self to the nearest integer, with ties rounding away from zero.
step1 or 2self.step(limit, stride) calls a passed block, passing it self the first time, and then self+stride, self+2*stride, and so on until the limit is exceeded. A step value that is equal to the limit will call the block one last time. If stride is not present, it is taken to be 1.
to_i0Converts self to an Integer.
to_int0Synonym for to_i.
to_f0Returns self unchanged.
to_s0Returns a string representation of self.
truncate0Rounds toward zero; that is, returns the Integer with greatest absolute value that has the same sign as self and absolute value not greater than that of self.
zero?0Returns true if self is equal to zero.

New in Ruby 1.9Edit

MethodParametersDescription
fdiv1A synonym for quo.
round0 or 1With no parameter, round behaves as in Ruby 1.8. An optional numeric parameter gives the number of digits to the right of the decimal point to which self is to be rounded. This number may be negative, in which case self will be rounded to a power of ten equal to the negation of the digit count. If the number of digits is positive, the return value is a Float; otherwise it is an Integer.
scalar?0Returns true.
Advertisement | Your ad here

Around Wikia's network

Random Wiki