Floating-point arithmetic represents numeric values through Ruby’s built-in Float class. Floats are scalar objects, and Ruby lets them respond to messages such as round. section 8.0 section 8.3
Use floating-point values when arithmetic should produce a non-integer result:
- Integer division remains integer division: 16 / 5 produces 3. section 8.3.2
- Including a Float produces floating-point division: 10 / 3.3 produces 3.0303030303. section 8.3.2
- Floating-point operands also support ordinary addition, such as 1.2 + 3.4 producing 4.6. section 8.3.2
Convert values with to_f when permissive string handling is appropriate: "1.23hello".to_f returns 1.23 and "hello".to_f returns 0.0. Float performs stricter conversion and raises an exception for invalid input. section 7.4.3
Temperature conversion
A Celsius-to-Fahrenheit calculation uses multiplication, division, and addition: fahrenheit = (celsius * 9 / 5) + 32. Using floating-point values makes the converter suitable for real-world fractional temperatures. section 1.1.5
Table 8.1 Common arithmetic expressions and their evaluative results
| Expression |
Result |
Comments |
|---|---|---|
|
|
2 |
Addition |
|
|
2 |
Integer division |
|
|
3 |
Integer division (no automatic floating-point conversion) |
|
|
3.0303030303 |
Floating-point division |
|
|
4.6 |
Floating-point addition |
|
|
–5 |
Subtraction |
|
|
1 |
Modulo (remainder) |
Compare integer and floating-point arithmetic in a compact table of Ruby expressions and results. section 8.3.2



















