-
Notifications
You must be signed in to change notification settings - Fork 1
Fix temperature conversion formula bugs #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughIntroduces a new module defining two temperature conversion functions (Celsius↔Fahrenheit) and executes two top-level print statements demonstrating conversions after function definitions. No error handling or I/O beyond prints. Changes
Sequence Diagram(s)sequenceDiagram
actor User
participant Script as buggy_temperature.py
participant C2F as celsius_to_fahrenheit()
participant F2C as fahrenheit_to_celsius()
User->>Script: Run module
Note over Script: Define functions
Script->>C2F: celsius_to_fahrenheit(100)
C2F-->>Script: 212.0
Script->>Script: print("100C in F:", 212.0)
Script->>F2C: fahrenheit_to_celsius(212)
F2C-->>Script: 100.0
Script->>Script: print("212F in C:", 100.0)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (5)
buggy_temperature.py (5)
1-9: Add type hints and short docstrings for clarity.Keeps API explicit; ints still accepted where float is expected.
-def celsius_to_fahrenheit(celsius): - # Bug fixed: correct formula C * 9/5 + 32 - fahrenheit = celsius * 9/5 + 32 - return fahrenheit +def celsius_to_fahrenheit(celsius: float) -> float: + """Convert Celsius to Fahrenheit (C * 9/5 + 32).""" + return celsius * 9/5 + 32 @@ -def fahrenheit_to_celsius(fahrenheit): - # Bug fixed: correct order of operations (F - 32) * 5/9 - celsius = (fahrenheit - 32) * 5/9 - return celsius +def fahrenheit_to_celsius(fahrenheit: float) -> float: + """Convert Fahrenheit to Celsius ((F - 32) * 5/9).""" + return (fahrenheit - 32) * 5/9
1-9: Confirm domain expectations: enforce absolute-zero bounds?If desired, reject temperatures below −273.15°C / −459.67°F; otherwise leave unconstrained.
def celsius_to_fahrenheit(celsius: float) -> float: + if celsius < -273.15: + raise ValueError("Celsius below absolute zero") return celsius * 9/5 + 32 @@ def fahrenheit_to_celsius(fahrenheit: float) -> float: + if fahrenheit < -459.67: + raise ValueError("Fahrenheit below absolute zero") return (fahrenheit - 32) * 5/9
11-12: Nit: pretty-print without trailing .0 in demo.Optional display tweak.
- print(f"100°C = {celsius_to_fahrenheit(100)}°F") - print(f"212°F = {fahrenheit_to_celsius(212)}°C") + print(f"100°C = {celsius_to_fahrenheit(100):g}°F") + print(f"212°F = {fahrenheit_to_celsius(212):g}°C")
1-12: Consider renaming module to temperature.py.Current name “buggy_temperature.py” is misleading post-fix.
1-12: Add tests, including round-trip property.Covers key points and tolerances for floats.
# tests/test_temperature.py import math from buggy_temperature import celsius_to_fahrenheit, fahrenheit_to_celsius def test_known_points(): cases = [(-40, -40), (0, 32), (100, 212)] for c, f in cases: assert math.isclose(celsius_to_fahrenheit(c), f, rel_tol=0, abs_tol=1e-9) assert math.isclose(fahrenheit_to_celsius(f), c, rel_tol=0, abs_tol=1e-9) def test_round_trip(): for c in [-100, -40, 0, 37, 100]: assert math.isclose(fahrenheit_to_celsius(celsius_to_fahrenheit(c)), c, rel_tol=1e-12, abs_tol=0)Want me to open a follow-up PR with these tests?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
buggy_temperature.py(1 hunks)
🔇 Additional comments (2)
buggy_temperature.py (2)
1-4: Correct Celsius→Fahrenheit formula — LGTM.Uses C * 9/5 + 32 and returns float as expected.
6-9: Correct Fahrenheit→Celsius formula — LGTM.Uses (F - 32) * 5/9 with proper parentheses.
| print(f"100°C = {celsius_to_fahrenheit(100)}°F") | ||
| print(f"212°F = {fahrenheit_to_celsius(212)}°C") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Avoid import-time side effects; gate demo prints with main.
Top-level prints execute on import, which can break consumers/tests.
-print(f"100°C = {celsius_to_fahrenheit(100)}°F")
-print(f"212°F = {fahrenheit_to_celsius(212)}°C")
+if __name__ == "__main__":
+ print(f"100°C = {celsius_to_fahrenheit(100)}°F")
+ print(f"212°F = {fahrenheit_to_celsius(212)}°C")📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| print(f"100°C = {celsius_to_fahrenheit(100)}°F") | |
| print(f"212°F = {fahrenheit_to_celsius(212)}°C") | |
| if __name__ == "__main__": | |
| print(f"100°C = {celsius_to_fahrenheit(100)}°F") | |
| print(f"212°F = {fahrenheit_to_celsius(212)}°C") |
🤖 Prompt for AI Agents
In buggy_temperature.py around lines 11 to 12 the module contains top-level demo
print statements that run on import; move those prints into a guarded block so
they only run when the module is executed directly: wrap the two print calls in
an if __name__ == "__main__": block (indent them accordingly) so importing the
module by tests or other code won’t produce side-effect output.
Fixed both celsius_to_fahrenheit and fahrenheit_to_celsius functions with correct mathematical formulas: C to F: C * 9/5 + 32, F to C: (F - 32) * 5/9. Added proper parentheses for order of operations.
Summary by CodeRabbit