Post Reply 
CNV: A Function to Convert Units
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
Post Reply 


Messages In This Thread
CNV: A Function to Convert Units - epp - 10-27-2015, 08:34 PM
RE: CNV: A Function to Convert Units - Skyblues - 07-22-2017 10:28 PM



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