• entries
4
13
• views
6,958

# Lesson 3a: Better Temperature Conversion Words

1,140 views

We can write better temperature conversion words than we did at the end of the last lesson. If you thought that the words using /MOD were contrived to introduce /MOD , you were mostly on target. To obviate the necessity of using /MOD , all we need to do is to manage the dividend before division by adding half of the divisor. As before, we must adjust the sign of the rounding term to match the sign of the dividend and adjust the conversion formula such that the rounding term is an integer.

For TC>TF , the rounding formula is

F = 9C/5 ± 0.5 + 32

F = (9C ± 2.5)/5 +32

and, for an integer rounding term, we have

F = (18C ± 5)/10 + 32

Now, we can write the new definition for TC>TF :

: TC>TF
( degC -- degF )

18 * DUP SGN 5 * + 10 / 32 + ;

Similarly, for TF>TC :

C = 5(F - 32)/9 ± 0.5

C = (5(F - 32) ± 4.5)/9

C = (10(F - 32) ± 9)/18

and, the new definition for TF>TC :

: TF>TC
( degF -- degC )

32 - 10 * DUP SGN 9 * + 18 / ;

It would be a good exercise for you to walk through the execution and track the stack contents for each of the above words, much as we did in Lesson 3.

As always, I need your feedback to make this tutorial more useful.

• 3

## 1 Comment

I've not forgotten! Things should slow down at work in January, then I'll start over from the beginning (I've forgotten everything already).

##### Link to comment

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.