This is actually the problem that made me come up with accumulation mode in Hypatia: when you want to convert body height in cm to feet and inches, you need two results, not just one.
In addition, there the problem involves a rounding issue that would otherwise be 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 by 100, and then convert meters to feet with Hypatia's unit conversion operator :FT:
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.
One way would be by entering SHOW $feet $inch -- this would display the values of the two variables -- but we want a proper result, two numbers written to the result file hy.
Only calculations give results (assigning values to variables does not) -- simply entering a variable counts as a calculation (as does entering a number, or a constant).
But, within a script results do not get written to hy except in accumulation mode -- so, we not only have to use accumulation mode to add the second result (the inches) after the first one, we also have to use accumulation mode for the first result (the feet). And this means we have to clear the result file hy before we write anything to hy.
The command & in the first line here clears hy, the next two lines add the values of $feet and $inches to the empty result file, and then the command HY displays the content of hy:
&
$feet &
$inch &
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
5 9
= 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.
But what does = 9 underneath our result mean? When a script contains calculation lines, then the value of $ (that is, the most recent calculation result) is always shown at the end of the script, whether we need it or not -- in our case, the last calculation was $inch.
We could insert the line $cm before HY, then this would be the last calculation result and the number underneath our result 5 9 would be the original centimeters (and because we wouldn't use accumulation mode here, and it's a script, this value would not be written to hy):
? $cm = 176
Result file hy cleared
5 9
= 176
We will return to this example, too, in the context of loops.
Home, Up: Scripts, Prev: Script Example: Body Mass Index, Next Chapter: Loops