Post Reply 
Temperature conversion problem. Please help!
08-10-2017, 05:29 AM
Post: #1
Temperature conversion problem. Please help!
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.

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;
Find all posts by this user
Quote this message in a reply
08-10-2017, 07:27 AM (This post was last modified: 08-10-2017 07:31 AM by StephenG1CMZ.)
Post: #2
RE: Temperature conversion problem. Please help!
If I recall correctly, there are two techniques that can be used to strip out units.

Mathematically, if one divides 3_m by 1_m the result is 3 - a dimensionless number without any units. However, in the case of temperatures I am not certain that would work without first converting to Kelvin, because of the differing 0's.

Alternatively, str(x) will yield a string like "3_m" and one can then strip out the underscore and unit from the string, leaving just "3".

I have implemented temperature and other unit conversions In my Z_UNITS program.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
08-10-2017, 07:35 AM (This post was last modified: 08-10-2017 07:36 AM by Didier Lachieze.)
Post: #3
RE: Temperature conversion problem. Please help!
°C and °F temperature units have some restrictrictions as explained here.

Your program works if I replace the lines 37 & 38:

uFr := expr("1_" + unit[iPr,n]);
x := x * uFr;


by:

x := expr(x + "_" + unit[iPr,n]);
Find all posts by this user
Quote this message in a reply
08-12-2017, 01:10 AM
Post: #4
RE: Temperature conversion problem. Please help!
(08-10-2017 07:35 AM)Didier Lachieze Wrote:  °C and °F temperature units have some restrictrictions as explained here.

Your program works if I replace the lines 37 & 38:

uFr := expr("1_" + unit[iPr,n]);
x := x * uFr;


by:

x := expr(x + "_" + unit[iPr,n]);

Thanks for the ideas, I'm learning a lot here!

I decided to go with Didier's solution. It's basically a string concationation to replace the need to multiply the units. Brilliant!

Thanks,
Jim
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)