--- title: "Temperature Conversion" id: 57021 type: "computer_media" slug: "temperature-conversion" url: "http://localhost/computer_media/temperature-conversion/" markdown_url: "http://localhost/computer_media/temperature-conversion.md" published_at: "2024-10-02T07:43:54+00:00" modified_at: "2026-04-03T07:58:44+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/45_Temp.png" excerpt: "A two-way temperature converter with a subtle but consequential arithmetic bug hiding in one of its conversion subroutines — can you spot it?" category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Science" slug: "science" taxonomy: "genre" url: "http://localhost/type/science/" media_contents: - id: 56732 title: "Timex Sinclair Public Domain Library Tape 1001" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1001/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/45_Temp.png" media_type_tags: "Science" --- # Temperature Conversion This program performs bidirectional temperature conversion between Celsius and Fahrenheit. At startup the user chooses which direction to convert; Celsius-to-Fahrenheit conversions use the subroutine at line 1000 (formula F = (9/5)×C + 32), while Fahrenheit-to-Celsius conversions use the subroutine at line 2000. After each conversion the user is asked whether they are finished, allowing repeated conversions in a loop via GOTO. The program contains a notable bug at line 2010: the Fahrenheit-to-Celsius formula incorrectly subtracts 12 instead of 32, giving C = (F−12)×(5/9) rather than the correct C = (F−32)×(5/9). *** ## Program Analysis ### Program Structure The program is divided into three logical regions: a main menu and dispatch block (lines 5–120), a Celsius-to-Fahrenheit subroutine (lines 1000–1030), and a Fahrenheit-to-Celsius subroutine (lines 2000–2030). Lines 3000–3010 handle saving and restarting the program. 1. **Lines 5–11:** Title display. 2. **Lines 20–30:** User selects conversion direction; branching on `A$="Y"` jumps to the C→F path at line 80. 3. **Lines 40–75:** F→C loop: prompts for Fahrenheit input, calls `GOSUB 2000`, asks if finished, repeats or stops. 4. **Lines 80–120:** C→F loop: prompts for Celsius input, calls `GOSUB 1000`, asks if finished, repeats or stops. 5. **Lines 1000–1030:** Subroutine — computes and prints Celsius-to-Fahrenheit conversion. 6. **Lines 2000–2030:** Subroutine — computes and prints Fahrenheit-to-Celsius conversion. ### Conversion Formulas | Subroutine | Line | Formula Used | Correct Formula | Status | | --- | --- | --- | --- | --- | | C → F | 1010 | `F=(9/5)*C+32` | F = (9/5)×C + 32 | ✓ Correct | | F → C | 2010 | `C=(F-12)*(5/9)` | C = (F−32)×(5/9) | ✗ Bug: −12 should be −32 | ### Bug: Incorrect Fahrenheit-to-Celsius Constant Line 2010 reads `LET C=(F-12)*(5/9)`. The correct offset is 32, not 12. This means every Fahrenheit-to-Celsius result will be wrong; for example, 32°F would yield approximately 11.1°C instead of the correct 0°C. The C→F subroutine at line 1010 is correct, making the error asymmetric. ### Key BASIC Idioms and Techniques - **GOSUB/RETURN structure:** Conversion logic is cleanly separated into subroutines at lines 1000 and 2000, keeping the main loop concise. - **Input echo:** Lines 42 and 82 each `PRINT` the just-entered value immediately after `INPUT`, providing visual confirmation of the entered number. - **Y/N loop control:** Continuation is handled by testing for `"N"` rather than `"Y"` — the program exits on anything other than an explicit “N”, meaning any unexpected input also terminates the loop gracefully via `STOP`. - **Variable reuse:** The variable `C` serves double duty — it holds the Celsius input in the main block and is also written by the F→C subroutine at line 2010. Equally, `F` is used both as direct input and as output from the C→F subroutine. This creates no conflict given the program’s linear flow but could cause issues if the structure were extended. ### Notable Anomalies - The program uses three separate input variables for the Y/N prompts (`A$`, `B$`, `C$`) where a single variable would suffice, slightly inflating memory use. - Line 11 uses a bare `PRINT` to emit a blank line — a standard idiom for spacing output. - The label in the `SAVE` command at line 3000 is an unusual alphanumeric string (`"1004%5"`), suggesting a filing or catalogue convention rather than a descriptive name. ## Source Code ``` 5 REM TEMP CONVERSION 10 PRINT "TEMPERATURE CONVERSION" 11 PRINT 20 PRINT "DO YOU WISH TO CONVERT C TO F(Y/N)" 21 INPUT A$ 30 IF A$="Y" THEN GOTO 80 40 PRINT "ENTER DEGREES F" 41 INPUT F 42 PRINT F 50 GOSUB 2000 60 PRINT "ARE YOU FINISHED(Y/N)" 61 INPUT B$ 70 IF B$="N" THEN GOTO 40 75 STOP 80 PRINT "ENTER DEGREES C" 81 INPUT C 82 PRINT C 90 GOSUB 1000 100 PRINT "ARE YOU FINISHED(Y/N)" 101 INPUT C$ 110 IF C$="N" THEN GOTO 80 120 STOP 1000 REM CEL TO FAHR CONV 1010 LET F=(9/5)*C+32 1020 PRINT C;" DEGREES CELSIUS= ";F;" DEGREES FAHRENHEIT" 1030 RETURN 2000 REM FAHR TO CEL CONV 2010 LET C=(F-12)*(5/9) 2020 PRINT F;" DEGREES FAHRENHEIT= ";C;" DEGREES CELSIUS" 2030 RETURN 3000 SAVE "1004%5" 3010 RUN ```