Class: Float

Inherits:
Numeric show all
Includes:
Precision

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)

Instance Method Summary (collapse)

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

#between?

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.

Returns:

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

Overloads:

- (Float) *(other)

Returns a new float which is the product of float and other.

Returns:

- (Float) **(other)

Raises float the other power.

2.0**3      #=> 8.0

Returns:

- (Float) +(other)

Returns a new float which is the sum of float and other.

Returns:

- (Float) -(other)

Returns a new float which is the difference of float and other.

Returns:

- (Float) -

Returns float, negated.

Returns:

- (Float) /(other)

Returns a new float which is the result of dividing float by other.

Returns:

- (Boolean) <(real)

true if flt is less than real.

Returns:

  • (Boolean)

- (Boolean) <=(real)

true if flt is less than or equal to real.

Returns:

  • (Boolean)

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

Returns:

  • (-1, 0, +1, nil)

- (Boolean) ==(obj)

Returns true only if obj has the same value as flt. Contrast this with Float#eql?, which requires obj to be a Float.

1.0 == 1   #=> true

Returns:

  • (Boolean)

- (Boolean) >(real)

true if flt is greater than real.

Returns:

  • (Boolean)

- (Boolean) >=(real)

true if flt is greater than or equal to real.

Returns:

  • (Boolean)

- (Float) abs - (Float) magnitude

Returns the absolute value of flt.

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

Overloads:

  • - abs

    Returns:

  • - magnitude

    Returns:

- (0, Float) arg - (0, Float) angle - (0, Float) phase

Returns 0 if the value is positive, pi otherwise.

Overloads:

  • - arg

    Returns:

  • - angle

    Returns:

  • - phase

    Returns:

- (0, Float) arg - (0, Float) angle - (0, Float) phase

Returns 0 if the value is positive, pi otherwise.

Overloads:

  • - arg

    Returns:

  • - angle

    Returns:

  • - phase

    Returns:

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

Returns:

- (Array) coerce(numeric)

Returns an array with both aNumeric and flt represented as Float objects. This is achieved by converting aNumeric to a Float.

1.2.coerce(3)       #=> [3.0, 1.2]
2.5.coerce(1.1)     #=> [1.1, 2.5]

Returns:

- (Integer) denominator

Returns the denominator (always positive). The result is machine dependent.

See numerator.

Returns:

- (Array) divmod(numeric)

See Numeric#divmod.

Returns:

- (Boolean) eql?(obj)

Returns true only if obj is a Float with the same value as flt. Contrast this with Float#==, which performs type conversions.

1.0.eql?(1)   #=> false

Returns:

  • (Boolean)

Returns:

  • (Boolean)

- (Float) quo(numeric)

Returns float / numeric.

Returns:

- (Boolean) finite?

Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).

Returns:

  • (Boolean)

Returns:

  • (Boolean)

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

Returns:

- (Integer) hash

Returns a hash code for this float.

Returns:

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

Returns:

  • (nil, -1, +1)

Returns:

  • (Boolean)

- (Float) abs - (Float) magnitude

Returns the absolute value of flt.

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

Overloads:

  • - abs

    Returns:

  • - magnitude

    Returns:

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

Overloads:

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

Returns:

  • (Boolean)

Returns:

  • (Boolean)

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

Returns:

- (0, Float) arg - (0, Float) angle - (0, Float) phase

Returns 0 if the value is positive, pi otherwise.

Overloads:

  • - arg

    Returns:

  • - angle

    Returns:

  • - phase

    Returns:

- (Float) quo(numeric)

Returns float / numeric.

Returns:

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

Returns:

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

Returns:

- (self) to_f

As flt is already a float, returns self.

Returns:

  • (self)

- (Integer) to_i - (Integer) to_int - (Integer) truncate

Returns flt truncated to an Integer.

Overloads:

- (Integer) to_i - (Integer) to_int - (Integer) truncate

Returns flt truncated to an Integer.

Overloads:

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

Returns:

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

Returns:

- (Integer) to_i - (Integer) to_int - (Integer) truncate

Returns flt truncated to an Integer.

Overloads:

- (Boolean) zero?

Returns true if flt is 0.0.

Returns:

  • (Boolean)

Returns:

  • (Boolean)