EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Ruby

Ruby Classes

In Ruby, the Float class is a subclass of the Numeric class. Objects of the Float class represent real numbers using the system’s native double-precision floating-point representation.

Public Instance Methods

Arithmetic Operations

The Float class supports various arithmetic operations, including addition, subtraction, multiplication, division, modulo, and exponentiation.

  • Addition: Returns the sum of a float and a numeric value as a floating-point number.
float + numeric
  • Subtraction: Returns the difference between a float and a numeric value as a floating-point number.
float - numeric
  • Multiplication: Returns the product of a float and a numeric value as a floating-point number.
float * numeric
  • Division: Returns the quotient of a float and a numeric value as a floating-point number.
float / numeric
  • Modulo: Returns the remainder when a float is divided by a numeric value.
float % numeric
  • Exponentiation: Raises the float to the power of a numeric value.
float ** numeric
  • Unary Minus: Returns the negative of the float.
-float

Example:

a = 5.5
b = 2

# Addition
puts a + b  # Output: 7.5

# Subtraction
puts a - b  # Output: 3.5

# Multiplication
puts a * b  # Output: 11.0

# Division
puts a / b  # Output: 2.75

# Modulo
puts a % b  # Output: 1.5

# Exponentiation
puts a ** b  # Output: 30.25

# Unary Minus
puts -a  # Output: -5.5

Comparison Operators

  • Spaceship operator (<=>): Returns -1 if the float is less than the numeric value, 0 if they are equal, and 1 if the float is greater.
float <=> numeric

Example:

puts 3.2 <=> 5   # Output: -1
puts 5.0 <=> 5   # Output: 0
puts 7.4 <=> 5   # Output: 1
  • Equality (==): Returns true if the float is equal to another object.
float == obj

Example:

p 45.5.divmod(6)  # Output: [7, 3.5]

Other Useful Methods

  • abs: Returns the absolute value of the float.
float.abs
  • eql?: Returns true if the float is equal to another object, with the same value and type.
float.eql?(obj)
  • ceil: Returns the smallest integer greater than or equal to the float.
float.ceil
  • divmod: Returns an array containing the quotient and remainder when divided by a numeric value.
float.divmod(numeric)

Example:

p 45.5.divmod(6)  # Output: [7, 3.5]
  • eql?: Returns true if the float is equal to another object, with the same value and type.
float.eql?(obj)

Example:

puts 5.5.eql?(5.5)  # Output: true

Output:

Title: Ruby Programming
Author: John Doe
Title: Learning Ruby
Author: Jane Doe
  • finite?: Returns true if the float is a valid IEEE floating-point number.
float.finite?

Example:

puts 10.0.finite?  # Output: true
  • floor: Returns the largest integer less than or equal to the float.
float.floor

Output:

Global count in FirstClass: 5
Global count in SecondClass: 5
  • floor: Returns the largest integer less than or equal to the float.
float.floor
  • infinite?: Returns nil-1, or 1 depending on whether the float is finite, negative infinity, or positive infinity.
float.infinite?

Example:

puts (1.0/0.0).infinite?  # Output: 1
  • round: Rounds the float to the nearest integer or to the specified number of decimal places.
float.round(digits=0)

Example:

puts 5.67.round   # Output: 6
puts 5.67.round(1) # Output: 5.7
  • to_f: Returns the float itself.
float.to_f
  • to_i: Truncates the float to return its integer value.
float.to_i
  • zero?: Returns true if the float is 0.0.
float.zero?

Example:

puts 0.0.zero?  # Output: true

Constants in the Float Class

  • EPSILON: Smallest floating-point number greater than 1 (2.2204460492503131e-16).
  • MANT_DIG: Number of mantissa digits (53 by default).
  • MAX: Largest double-precision floating-point number (1.7976931348623157e+308).
  • MIN: Smallest positive normalized number (2.2250738585072014e-308).
  • \INFINITY: Represents positive infinity.
  • NAN: Represents “Not a Number.”
End of lesson.