What is the float.prev_float method in Ruby?

Overview

In order to find the predecessor of a floating-point number, we use the prev_float method.

Syntax

float_value.prev_float

Parameters

float_value: This is the float value whose predecessor value we want to find.

Return value

The predecessor of float_value is returned.

Code example

# create some float values
f1 = 2.3
f2 = -1.0
f3 = 0.1111111
f4 = 200.00
# get previous values
# and print results
puts f1.prev_float
puts f2.prev_float
puts f3.prev_float
puts f4.prev_float

Explanation

  • Lines 2-5: We create some float values: f1, f2, f3, and f4.
  • Lines 9-12: We invoke the prev_float method on the float values and print the results to the console.

Free Resources