08-10-2017, 05:29 AM
Can one of you please help me figure out how to modify some code so I can do a temperature conversion?
I started this project by adding units to epp's excellent CNV program.
I am using this as an educational exercise for myself.
http://www.hpmuseum.org/forum/thread-5018.html
Everything is working except the temperature conversion.
The problem is that if the first unit selected is Fahrenheit or Celsius, line 38 fails because those units contain a degree symbol ' ° ' ...
So, at line 38, I have:
x = 25
uFr = 1_°F (or 1_°C)
x := x * uFr;
(25 * 1_°F)
which gives Error: Invalid input
If I leave out the degree symbol, I am getting "Error: Inconsistent Units" which I believe is because the calculator uses the degree symbol for Fahrenheit or Celsius units internally.
I have added the modified code below which only has the temperature conversion for review convenience.
Thanks for the help! I am really trying to learn some basic Prime programming.
I started this project by adding units to epp's excellent CNV program.
I am using this as an educational exercise for myself.
http://www.hpmuseum.org/forum/thread-5018.html
Everything is working except the temperature conversion.
The problem is that if the first unit selected is Fahrenheit or Celsius, line 38 fails because those units contain a degree symbol ' ° ' ...
So, at line 38, I have:
x = 25
uFr = 1_°F (or 1_°C)
x := x * uFr;
(25 * 1_°F)
which gives Error: Invalid input
If I leave out the degree symbol, I am getting "Error: Inconsistent Units" which I believe is because the calculator uses the degree symbol for Fahrenheit or Celsius units internally.
I have added the modified code below which only has the temperature conversion for review convenience.
Thanks for the help! I am really trying to learn some basic Prime programming.
Code:
#pragma mode(separator(.,;) integer(h32))
cFr = {1};
cTo = {1};
iPr;
EXPORT Units(x)
begin
local unit = {
{"°C", "°F", "K", "Rankine"}
};
local prog = {
"Temperature"
};
local t = type(x);
local x0 = x;
local uFr, uTo, n;
if (t <> 0) and (t <> 9) then msgbox("Input must be REAL or UNIT");
return x0;
end;
choose(iPr, "convert", prog);
if (iPr == 0) then return x0;
end;
if (t == 0) then
n := cFr[iPr];
choose(n, "From", unit[iPr]);
if (n == 0) then return x0;
end;
uFr := expr("1_" + unit[iPr,n]);
x := x * uFr; *** this is line 38 ***
cFr[iPr] := n;
end;
n := cTo[iPr];
choose(n, "To", unit[iPr]);
if (n == 0) then return x0;
end;
uTo := expr("1_" + unit[iPr,n]);
x := convert(x, uTo);
cTo[iPr] := n;
if (t == 0) then x := x; // x := x / uTo; commenting this retains the units in the output
else x;
end;
end;