Script Example: Centimeters to Feet and Inches

Home, Up: Scripts

 

This is actually the problem that gave me the inspiration for accumulation mode in Hypatia: when you want to convert body height in cm to feet and inches, you need two results, not just one.

There is a rounding issue that looks a bit cumbersome to deal with, but is easily solved with an IF ... condition.

Let's call our script cm2feet, and create it with EDIT cm2feet.

Obviously the script has to begin with prompting for the centimeters, convert them to meters by dividing them by 100, and then use Hypatia's unit conversion operator :FT to convert them to feet:

 

PROMPT $cm enter centimeters to convert to feet and inches:

$feet = $cm 100 / :FT

 

Now we have feet as a floating point value -- multiply the fractional part with 12 and round it to integer (zero digits after the decimal point) and we have the inches, take the integer part of $feet and we have the feet.

 

$inch = $feet FRAC 12 * 0 ROUND

$feet = $feet INT

 

The problem is that after rounding the inches to integers we can end up with, for instance, 5 feet 12 inches.

To deal with that case, if we get 12 inches we increase feet by one, and set inches to 0.

 

IF $inch 12 EQUAL THEN $feet = $feet 1 +

ALSO: $inch = 0

 

Now Hypatia knows the answer, but we still have to make it share it with us.

If we only want to see the result, we can display the values of our two variables with SHOW $feet $inch.

If we want the two numbers written to the result file hy, though (for instance so that we can copy them to the clipbord with COPY), we can use the & command with the variables $feet and $inch to clear hy and write the values of the variables to hy. After this, we can look at the results with the command HY.

So, this would be the full script:

 

PROMPT $cm enter centimeters to convert to feet and inches:

$feet = $cm 100 / :FT

$inch = $feet FRAC 12 * 0 ROUND

$feet = $feet INT

IF $inch 12 EQUAL THEN $feet = $feet 1 +

ALSO: $inch = 0

& $feet $inch

HY

 

When we have saved this script as a file named cm2feet, we can now convert 176cm to feet and inches:

 

? _cm2feet

  Enter centimeters to convert to feet and inches:

? $cm = 176

Result file hy cleared, data written to hy

  5 9

 

The command HY in the last line of our script shows the content of hy, 5 9 -- this is the result we were looking for.

With the command COPY you can copy it to the Windows clipboard.

Note that there is no calculation line in this script, therefore the value of $ does not get changed -- this is why it does not get displayed at the end of the script (only at the end of a loop it would be displayed anyway).

 

We will return to this example, too, in the context of loops.

 

Home, Up: Scripts, Prev: Script Example: Body Mass Index, Next Chapter: Loops