Skip to content
instance method

Float#<=>

(::Numeric) -> ::Integer?
self <=> other → -1, 0, 1, or nil

Compares self and other.

Returns:

  • -1, if self is less than other.

  • 0, if self is equal to other.

  • 1, if self is greater than other.

  • nil, if the two values are incommensurate.

Examples:

Example
2.0 <=> 2.1            # => -1
2.0 <=> 2              # => 0
2.0 <=> 2.0            # => 0
2.0 <=> Rational(2, 1) # => 0
2.0 <=> Complex(2, 0)  # => 0
2.0 <=> 1.9            # => 1
2.0 <=> 'foo'          # => nil

Float::NAN <=> Float::NAN returns an implementation-dependent value.

Class Float includes module Comparable, each of whose methods uses Float#<=> for comparison.

See also #<= #== #< #=== also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0