Post Reply 
CNV: A Function to Convert Units
10-27-2015, 08:34 PM (This post was last modified: 04-24-2017 09:59 PM by epp.)
Post: #1
CNV: A Function to Convert Units
A program to convert units for length, volume, time, or weight. After installing CNV click on the Toolbox, select the USER tab, and choose CNV. The calculator may be in Textbook or RPN modes. Under Textbook specify the input as a function parameter:

CNV(5)

With RPN enter the number, press ENTER, then invoke CNV:

5 ENTER CNV

You will be prompted to indicate whether the conversion is for Length, Volume, Time, or Weight. Then indicate the units you are converting from and to. If units are specified on input then you will only be prompted to enter output units and the results will include units. The program remembers your selections for input and output units for the next time.

CNV(1) --> 2.54
CNV(1_inch) --> 2.54_cm

Code:

#pragma mode(separator(.,;) integer(h32))

// when changing menus update the following: cFr, cTo, unit, prog

cFr = {1,1,1,1};
cTo = {1,1,1,1};
iPr;

EXPORT CNV(x)
begin
  local unit = {
    {"mm", "cm", "m", "km", "inch", "ft", "yd", "mile"},
    {"ml", "l", "tsp", "tbsp", "cu", "qt", "ozfl", "inch^3", "ft^3"},
    {"s", "min", "h", "d", "yr"},
    {"g", "kg", "oz", "lb"}
  };
  local prog = {
    "Length",
    "Volume",
    "Time",
    "Weight"
  };

  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;
    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 / uTo; else x; end;
end;
Visit this user's website Find all posts by this user
Quote this message in a reply
10-30-2015, 08:58 AM
Post: #2
RE: CNV: A Function to Convert Units
I have also found it best to deal with temperature conversion myself rather than using CONVERT - and I use the Android emulator rather than actual hardware. Nice to see your program in the library.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
07-17-2017, 05:53 AM
Post: #3
RE: CNV: A Function to Convert Units
Nice!
Find all posts by this user
Quote this message in a reply
07-22-2017, 06:15 AM (This post was last modified: 07-22-2017 06:24 AM by Skyblues.)
Post: #4
RE: CNV: A Function to Convert Units
I'm starting to play around with programming the Prime. I thought I'd start with an existing program that I like.

I was going to add an area conversion to this program. Can someone point me in the right direction? I looked over the code and tried a few things but nothing is working.

I thought this may be a good educational exercise for me.

Thanks
Find all posts by this user
Quote this message in a reply
07-22-2017, 10:28 PM (This post was last modified: 07-23-2017 12:37 AM by Skyblues.)
Post: #5
RE: CNV: A Function to Convert Units
This is my first attempt to expand on epp's "CNV: A Function to Convert Units" program.

All the unit conversions seem to be working except Speed.
The only units that work in Speed are kph, mph and knot; the rest are creating "Error: Syntax Error".

It doesn't seem to like anything with a "/" in the name and I can't find the problem.

So I guess the question is, how do I get the code to accept a name with a "/" in it?
I've been reading through the code to try to figure why m/s (for example) isn't being accepted although it's a valid unit in the Prime.

Is there an IDE one can use to step through the code?

Edit:
In case anyone's interested, I discovered that anything with a "/" in it, must be enclosed in parentheses. I've tried out most of the unit conversions and they work. If anyone finds an error, please let me know.

I edited my original code with my update. I find it very useful. Thanks for the original code, epp!

Code:
#pragma mode(separator(.,;) integer(h32))

// when changing menus update the following: cFr, cTo, unit, prog

cFr = {1,1,1,1,1,1,1,1,1,1,1,1,1};
cTo = {1,1,1,1,1,1,1,1,1,1,1,1,1};
iPr;

EXPORT Units(x)
begin
  local unit = {
    {"m", "cm", "mm", "km", "yd", "ft", "inch", "mile"},
    {"m^2", "cm^2", "km^2", "yd^2", "ft^2", "inch^2", "mile^2", "acre"},
    {"m^3", "cm^3", "l", "ml", "yd^3", "ft^3", "inch^3", "galUS", "ozfl", "qt", "liqpt", "cu", "tbsp", "tsp"},
    {"yr", "d", "h", "min", "s"},
    {"(m/s)", "(cm/s)", "(ft/s)", "kph", "mph", "knot", "(rad/s)", "(tr/min)", "(tr/s)"},
    {"kg", "g", "lb", "oz", "ozt", "grain", "ct"},
    {"(m/s^2)", "(ft/s^2)", "grav",  "(rad/s^2)"},
    {"(kg*m/s^2)", "N", "dyn", "lbf", "kip", "gf", "pdl"},
    {"(kg*m^2/s^2)", "J", "Wh", "ft*lbf", "kcal", "cal", "Btu", "erg", "thermUS"},
    {"(kg*m^2/s^3)", "W", "MW", "hp", "ft*lbf/s"},
    {"(kg/(m*s^2))", "Pa", "bar", "atm", "psi", "torr", "mmHg", "inHg", "inH2O"},
    {"°C", "°F", "K", "Rankine"},
    {"rad", "deg", "grad", "arcmin", "arcs", "tr"}

  };
  local prog = {
    "Length",
    "Area",
    "Volume",
    "Time",
    "Speed",
    "Mass",
    "Acceleration",
    "Force",
    "Energy",
    "Power",
    "Pressure",
    "Temperature",
    "Angle"
  };

  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;
    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 / uTo; else x; end;
end;
Find all posts by this user
Quote this message in a reply
08-12-2017, 02:24 AM
Post: #6
RE: CNV: A Function to Convert Units
I'm posting my final version of epp's expanded "CNV: A Function to Convert Units"

I made one change based on some advise I got from Didier. Now, the temperature conversion works correctly.

Code:
#pragma mode(separator(.,;) integer(h32))

// when changing menus update the following: cFr, cTo, unit, prog

cFr = {1,1,1,1,1,1,1,1,1,1,1,1,1};
cTo = {1,1,1,1,1,1,1,1,1,1,1,1,1};
iPr;

EXPORT Units(x)
begin
  local unit = {
    {"m", "cm", "mm", "km", "yd", "ft", "inch", "mile", "nmi"},
    {"m^2", "cm^2", "km^2", "yd^2", "ft^2", "inch^2", "mile^2", "acre"},
    {"m^3", "cm^3", "l", "ml", "yd^3", "ft^3", "inch^3", "galUS", "ozfl", "qt", "liqpt", "cu", "tbsp", "tsp"},
    {"(m/s)", "(cm/s)", "(ft/s)", "kph", "mph", "knot", "(rad/s)", "(tr/min)", "(tr/s)"},
    {"tonUS", "lb", "oz", "kg", "g", "ozt", "grain", "ct", "mol"},
    {"rad", "deg", "grad", "arcmin", "arcs", "tr"},
    {"(m/s^2)", "(ft/s^2)", "grav",  "(rad/s^2)"},
    {"(kg*m/s^2)", "N", "dyn", "lbf", "kip", "gf", "pdl"},
    {"(kg*m^2/s^2)", "J", "Wh", "kWh", "eV", "ft*lbf", "kcal", "cal", "Btu", "erg", "thermUS"},
    {"(kg*m^2/s^3)", "W", "MW", "hp", "ft*lbf/s"},
    {"yr", "d", "h", "min", "s"},
    {"(kg/(m*s^2))", "Pa", "bar", "atm", "psi", "torr", "mmHg", "inHg", "inH2O"},
    {"°C", "°F", "K", "Rankine"}

  };
  local prog = {
    "Length",
    "Area",
    "Volume",
    "Velocity",
    "Mass",
    "Angle",
    "Acceleration",
    "Force",
    "Energy",
    "Power",
    "Time",
    "Pressure",
    "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;

    x := expr(x + "_" + unit[iPr,n]);
    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;  this removes the units
  else x; 
  end;
end;
Find all posts by this user
Quote this message in a reply
Post Reply 




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