What is float.eql?() method in Ruby?

Overview

The eql?() method can be used to know if two float values are equal. It returns a Boolean value — i.e., either true or false.

Syntax

floatVal.eql?(anotherFloatVal)
Syntax for the float.eql?() method

Parameters

anotherFloatVal: This is the float value that we want to compare with the float value floatVal which invokes the method eql?().

Return value

The value returned is a Boolean value. It returns true if both values are equal. Otherwise, it returns false.

Code

# create some float values
f1 = 34.344
f2 = 2.423
f3 = -5/2.3
f4 = f2 / 1.0
# invoke the coerce method
puts f1.eql?(f2)
puts f2.eql?(f3)
puts f3.eql?(f1)
puts f4.eql?(f2)

Explanation

  • Lines 2-5: We create some float variables f1, f2, f3, and f4, and initialize them with some values.
  • Lines 7-10: We check if the float values are equal using the eql?() method, and then printed the results.

Free Resources