Jump to content
IGNORED

Learning fbForth 2.0 - Lesson 3a: Better Temperature Conversion Words


RSS Bot
 Share

Recommended Posts

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

Spoiler
( 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 :

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

 

 

: TF>TC

Spoiler
( 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.


http://atariage.com/forums/blog/613/entry-12238-lesson-3a-better-temperature-conversion-words/
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...