Skip to content

conversions/temperature_conversions: fahrenheit_to_kelvin missing +273.15 (returns Celsius); integer division truncates fractional degrees #236

@aimasteracc

Description

@aimasteracc

Description

conversions/temperature_conversions.rb has two correctness bugs (verified with Ruby).

1. fahrenheit_to_kelvin is missing + 273.15 — it returns Celsius, labeled as Kelvin

The docstring states the correct formula K = [(F - 32) * 5 / 9] + 273.15, but the code omits the + 273.15:

def self.fahrenheit_to_kelvin(fahrenheit_input)
  kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2)   # missing + 273.15
  puts "#{fahrenheit_input}°F = #{kelvin_output}K"
...

So it computes the Celsius value and prints it as Kelvin:

fahrenheit_to_kelvin(32)  => "32°F = 0K"     # correct: 273.15 K
fahrenheit_to_kelvin(212) => "212°F = 100K"  # correct: 373.15 K

(The .round(2).round(2) double-round is also redundant.)

2. Integer division truncates fractional results

celsius_to_fahrenheit, fahrenheit_to_celsius, and fahrenheit_to_kelvin use integer arithmetic like celsius_input * 9 / 5. When the input is an Integer, / is integer division, so fractional degrees are silently dropped:

celsius_to_fahrenheit(37) => "37°C = 98°F"   # correct: 98.6 °F
fahrenheit_to_celsius(100) => "100°F = 37°C" # correct: 37.78 °C

Expected behavior

  • fahrenheit_to_kelvin(32)273.15 K, fahrenheit_to_kelvin(212)373.15 K.
  • celsius_to_fahrenheit(37)98.6 °F.

Actual behavior

  • fahrenheit_to_kelvin(32)0 K (Celsius value, missing the + 273.15 offset).
  • celsius_to_fahrenheit(37)98 °F (fractional part truncated by integer division).

Suggested fix

  • Add the Kelvin offset: kelvin_output = ((fahrenheit_input - 32) * 5.0 / 9 + 273.15).round(2).
  • Use floating-point division everywhere (e.g. * 9.0 / 5, * 5.0 / 9) so integer inputs don't truncate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions