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 valuesf1 = 2.3f2 = -1.0f3 = 0.1111111f4 = 200.00# get previous values# and print resultsputs f1.prev_floatputs f2.prev_floatputs f3.prev_floatputs f4.prev_float
Explanation
- Lines 2-5: We create some float values:
f1,f2,f3, andf4. - Lines 9-12: We invoke the
prev_floatmethod on the float values and print the results to the console.