Class: Float
Overview
Float objects represent real numbers using the native architecture's double-precision floating point representation.
Constant Summary
- ROUNDS
- RADIX
- MANT_DIG
- DIG
- MIN_EXP
- MAX_EXP
- MIN_10_EXP
- MAX_10_EXP
- MIN
- MAX
- EPSILON
- INFINITY
- NAN
Class Method Summary (collapse)
-
+ induced_from
Convert obj to a float.
Instance Method Summary (collapse)
-
- %
Return the modulo after division of flt by other.
-
- *
Returns a new float which is the product of float and other.
-
- **
Raises float the other power.
-
- +
Returns a new float which is the sum of float and other.
-
- -
Returns a new float which is the difference of float and other.
-
- -
Returns float, negated.
-
- /
Returns a new float which is the result of dividing float by other.
-
- <
true if flt is less than real.
-
- <=
true if flt is less than or equal to real.
-
- <=>
Returns -1, 0, +1 or nil depending on whether flt is less than, equal to, or greater than real.
-
- ==
Returns true only if obj has the same value as flt.
-
- >
true if flt is greater than real.
-
- >=
true if flt is greater than or equal to real.
-
- abs
Returns the absolute value of flt.
-
- angle
Returns 0 if the value is positive, pi otherwise.
-
- arg
Returns 0 if the value is positive, pi otherwise.
-
- ceil
Returns the smallest Integer greater than or equal to flt.
-
- coerce
Returns an array with both aNumeric and flt represented as Float objects.
-
- denominator
Returns the denominator (always positive).
-
- divmod
See Numeric#divmod.
-
- eql?
Returns true only if obj is a Float with the same value as flt.
-
- quo
Returns float / numeric.
-
- finite?
Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).
-
- floor
Returns the largest integer less than or equal to flt.
-
- hash
Returns a hash code for this float.
-
- infinite?
Returns nil, -1, or +1 depending on whether flt is finite, -infinity, or +infinity.
-
- magnitude
Returns the absolute value of flt.
-
- modulo
Return the modulo after division of flt by other.
-
- nan?
Returns true if flt is an invalid IEEE floating point number.
-
- numerator
Returns the numerator.
-
- phase
Returns 0 if the value is positive, pi otherwise.
-
- quo
Returns float / numeric.
-
- rationalize
Returns a simpler approximation of the value (flt-|eps|
-
- round
Rounds flt to a given precision in decimal digits (default 0 digits).
-
- to_f
As flt is already a float, returns self.
-
- to_i
Returns flt truncated to an Integer.
-
- to_int
Returns flt truncated to an Integer.
-
- to_r
Returns the value as a rational.
-
- to_s
Returns a string containing a representation of self.
-
- truncate
Returns flt truncated to an Integer.
-
- zero?
Returns true if flt is 0.0.
Methods included from Precision
included, #prec, #prec_f, #prec_i
Methods inherited from Numeric
#+@, #abs2, #class, #conj, #conjugate, #div, #dup, #i, #imag, #imaginary, #initialize_copy, #integer?, #nonzero?, #polar, #real, #real?, #rect, #rectangular, #remainder, #scalar?, #singleton_method_added, #step, #to_c
Methods included from Comparable
Methods inherited from NSNumber
#boolValue, #charValue, #compare:, #decimalValue, #descriptionWithLocale:, #doubleValue, #floatValue, #initWithBool:, #initWithChar:, #initWithDouble:, #initWithFloat:, #initWithInt:, #initWithInteger:, #initWithLong:, #initWithLongLong:, #initWithShort:, #initWithUnsignedChar:, #initWithUnsignedInt:, #initWithUnsignedInteger:, #initWithUnsignedLong:, #initWithUnsignedLongLong:, #initWithUnsignedShort:, #intValue, #integerValue, #isEqualToNumber:, #longLongValue, #longValue, numberWithBool:, numberWithChar:, numberWithDouble:, numberWithFloat:, numberWithInt:, numberWithInteger:, numberWithLong:, numberWithLongLong:, numberWithShort:, numberWithUnsignedChar:, numberWithUnsignedInt:, numberWithUnsignedInteger:, numberWithUnsignedLong:, numberWithUnsignedLongLong:, numberWithUnsignedShort:, #objCType, #shortValue, #stringValue, #unsignedCharValue, #unsignedIntValue, #unsignedIntegerValue, #unsignedLongLongValue, #unsignedLongValue, #unsignedShortValue
Methods inherited from NSValue
#getValue:, #initWithBytes:objCType:, #isEqualToValue:, #nonretainedObjectValue, #objCType, #pointerValue, #rangeValue, value:withObjCType:, valueWithBytes:objCType:, valueWithNonretainedObject:, valueWithPointer:, valueWithRange:
Methods inherited from NSObject
#!, #!=, #!~, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Float) induced_from(obj)
Convert obj to a float.
Instance Method Details
- (Float) %(other) - (Float) modulo(other)
Return the modulo after division of flt by other.
6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996
- (Float) *(other)
Returns a new float which is the product of float and other.
- (Float) **(other)
Raises float the other power.
2.0**3 #=> 8.0
- (Float) +(other)
Returns a new float which is the sum of float and other.
- (Float) -(other)
Returns a new float which is the difference of float and other.
- (Float) -
Returns float, negated.
- (Float) /(other)
Returns a new float which is the result of dividing float by other.
- (Boolean) <(real)
true if flt is less than real.
- (Boolean) <=(real)
true if flt is less than or equal to real.
- (-1, ...) <=>(real)
Returns -1, 0, +1 or nil depending on whether flt is less than, equal to, or greater than real. This is the basis for the tests in Comparable.
- (Boolean) ==(obj)
- (Boolean) >(real)
true if flt is greater than real.
- (Boolean) >=(real)
true if flt is greater than or equal to real.
- (Float) abs - (Float) magnitude
Returns the absolute value of flt.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
- (0, Float) arg - (0, Float) angle - (0, Float) phase
Returns 0 if the value is positive, pi otherwise.
- (0, Float) arg - (0, Float) angle - (0, Float) phase
Returns 0 if the value is positive, pi otherwise.
- (Integer) ceil
Returns the smallest Integer greater than or equal to flt.
1.2.ceil #=> 2
2.0.ceil #=> 2
(-1.2).ceil #=> -1
(-2.0).ceil #=> -2
- (Array) coerce(numeric)
- (Integer) denominator
Returns the denominator (always positive). The result is machine dependent.
See numerator.
- (Boolean) eql?(obj)
- (Float) quo(numeric)
Returns float / numeric.
- (Boolean) finite?
Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).
- (Integer) floor
Returns the largest integer less than or equal to flt.
1.2.floor #=> 1
2.0.floor #=> 2
(-1.2).floor #=> -2
(-2.0).floor #=> -2
- (Integer) hash
Returns a hash code for this float.
- (nil, ...) infinite?
Returns nil, -1, or +1 depending on whether flt is finite, -infinity, or +infinity.
(0.0).infinite? #=> nil
(-1.0/0.0).infinite? #=> -1
(+1.0/0.0).infinite? #=> 1
- (Float) abs - (Float) magnitude
Returns the absolute value of flt.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
- (Float) %(other) - (Float) modulo(other)
Return the modulo after division of flt by other.
6543.21.modulo(137) #=> 104.21
6543.21.modulo(137.24) #=> 92.9299999999996
- (Boolean) nan?
Returns true if flt is an invalid IEEE floating point number.
a = -1.0 #=> -1.0
a.nan? #=> false
a = 0.0/0.0 #=> NaN
a.nan? #=> true
- (Integer) numerator
Returns the numerator. The result is machine dependent.
For example:
n = 0.3.numerator #=> 5404319552844595
d = 0.3.denominator #=> 18014398509481984
n.fdiv(d) #=> 0.3
- (0, Float) arg - (0, Float) angle - (0, Float) phase
Returns 0 if the value is positive, pi otherwise.
- (Float) quo(numeric)
Returns float / numeric.
- (Rational) rationalize([eps])
Returns a simpler approximation of the value (flt-|eps|
For example:
0.3.rationalize #=> (3/10)
1.333.rationalize #=> (1333/1000)
1.333.rationalize(0.01) #=> (4/3)
- (Integer, Float) round([ndigits])
Rounds flt 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.
1.4.round #=> 1
1.5.round #=> 2
1.6.round #=> 2
(-1.5).round #=> -2
1.234567.round(2) #=> 1.23
1.234567.round(3) #=> 1.235
1.234567.round(4) #=> 1.2346
1.234567.round(5) #=> 1.23457
34567.89.round(-5) #=> 0
34567.89.round(-4) #=> 30000
34567.89.round(-3) #=> 35000
34567.89.round(-2) #=> 34600
34567.89.round(-1) #=> 34570
34567.89.round(0) #=> 34568
34567.89.round(1) #=> 34567.9
34567.89.round(2) #=> 34567.89
34567.89.round(3) #=> 34567.89
- (self) to_f
As flt is already a float, returns self.
- (Rational) to_r
Returns the value as a rational.
NOTE: 0.3.to_r isn't the same as '0.3'.to_r. The latter is equivalent to '3/10'.to_r, but the former isn't so.
For example:
2.0.to_r #=> (2/1)
2.5.to_r #=> (5/2)
-0.75.to_r #=> (-3/4)
0.0.to_r #=> (0/1)
- (String) to_s
Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return "NaN", "Infinity", and "-Infinity".
- (Boolean) zero?
Returns true if flt is 0.0.