Post Reply 
CONVERT and variables for arguments
10-15-2015, 06:16 PM (This post was last modified: 10-26-2015 09:35 AM by epp.)
Post: #1
CONVERT and variables for arguments
Too bad CONVERT doesn't support variables for arguments. Anyone have a code for a conversion utility?

Code:

EXPORT C2,C1;
EXPORT TEST()
BEGIN
    LOCAL y,lst,a1,a2;
    CHOOSE(C2, "Convert from", "mm","cm","m","km","inches","feet","yards","miles");
    CHOOSE(C1, "Convert to",   "mm","cm","m","km","inches","feet","yards","miles");
    lst := {"mm","cm","m","km","inch","ft","yd","mile"};
    a1 := "1_"+lst[C1];
    a2: = "1_"+lst[C2];
    INPUT(y);
    y*CONVERT(a2,a1);
END;

Update 26 Oct 2015:
My first PRIME program and, over the last week, it has evolved to this solution.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-15-2015, 07:05 PM
Post: #2
RE: CONVERT and variables for arguments
You can experiment with CAS.EVAL("something") and cat(var1,var2,var3...)

My website: erwin.ried.cl
Visit this user's website Find all posts by this user
Quote this message in a reply
10-15-2015, 07:08 PM
Post: #3
RE: CONVERT and variables for arguments
Code:

EXPORT C2,C1;
EXPORT TEST()
BEGIN
    LOCAL y,lst,a1,a2,t;
    CHOOSE(C2, "Convert from", "mm","cm","m","km","inches","feet","yards","miles");
    CHOOSE(C1, "Convert to",   "mm","cm","m","km","inches","feet","yards","miles");
//    lst := {"mm","cm","m","km","inch","ft","yd","mile"};
    lst := { 1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile };
    a1 := lst[C1];
    a2 := lst[C2];
    INPUT(
      { 
        { y, [2] } 
      }
    );
   t:="simplify(" + y + "*" + CONVERT(a2,a1) + ")";
   CAS(t);
END;

Is this what you meant?

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
10-15-2015, 07:46 PM
Post: #4
RE: CONVERT and variables for arguments
Sure is! Thank you, Han. It was my first program and you should see me smile!
Visit this user's website Find all posts by this user
Quote this message in a reply
10-15-2015, 09:25 PM
Post: #5
RE: CONVERT and variables for arguments
I found it gave a syntax error on 1 inch. Fixed by changing to 1 in. Both with underscores.
Works good apart from inch conversions.

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
10-15-2015, 09:31 PM (This post was last modified: 10-15-2015 09:33 PM by Han.)
Post: #6
RE: CONVERT and variables for arguments
You might also find this post useful:

http://www.hpmuseum.org/forum/thread-2913-page-2.html

This allows you to turn the [Shift] [Space] key sequence to bring up a custom units menu (read further into the thread to see how to allow pressing [Esc] to just print an underscore character.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
10-16-2015, 01:14 PM (This post was last modified: 10-17-2015 01:55 PM by epp.)
Post: #7
RE: CONVERT and variables for arguments
Here's the final version. I've also done Volume and Weight conversions using this as a template.
Code:

EXPORT D2,D1;
EXPORT Distance()
BEGIN
    LOCAL x,lst,a1,a2,t;
    INPUT({{ x, [2], {45,20,3} }},"","Convert ");
    IF string(x,1) == "0" THEN RETURN; END;
    CHOOSE(D2, "Units", "mm","cm","m","km","inches","feet","yards","miles");
    CHOOSE(D1, "To",    "mm","cm","m","km","inches","feet","yards","miles");
    lst := { 1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile };
    a2 := lst[D2];
    a1 := lst[D1];
    t := "simplify(" + x + "*" + CONVERT(a2,a1) + ")";
    CAS(t);
END;
Visit this user's website Find all posts by this user
Quote this message in a reply
10-17-2015, 01:40 PM
Post: #8
RE: CONVERT and variables for arguments
"mm","cm","m","km","inches","feet","yards","miles" can go in a list variable so you don't need to duplicate them

My website: erwin.ried.cl
Visit this user's website Find all posts by this user
Quote this message in a reply
10-17-2015, 02:13 PM (This post was last modified: 10-17-2015 02:15 PM by epp.)
Post: #9
RE: CONVERT and variables for arguments
(10-17-2015 01:40 PM)eried Wrote:  "mm","cm","m","km","inches","feet","yards","miles" can go in a list variable so you don't need to duplicate them

Thanks. Somehow I knew it wasn't final.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-17-2015, 10:03 PM
Post: #10
RE: CONVERT and variables for arguments
Added the list for the menu, called expr instead of CAS (is CAS a better alternative here?), and simplified by removing "simplify".

Code:

EXPORT CD2,CD1;
EXPORT Distance()
BEGIN
  LOCAL x,menu,unit,a1,a2,t;
  INPUT({{ x,[2],{45,20,3} }},"","Convert ");
  IF string(x,1) == "0" THEN RETURN; END;
  unit := { 1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile };
  menu := { "mm","cm","m","km","inches","feet","yards","miles" };
  CHOOSE(CD2, "Units", menu);
  CHOOSE(CD1, "To", menu);
  a2 := unit[CD2];
  a1 := unit[CD1];
  t := x + "*" + CONVERT(a2,a1);
  expr(t);
END;
Visit this user's website Find all posts by this user
Quote this message in a reply
10-18-2015, 01:19 AM
Post: #11
RE: CONVERT and variables for arguments
(10-17-2015 10:03 PM)epp Wrote:  Added the list for the menu, called expr instead of CAS (is CAS a better alternative here?), and simplified by removing "simplify".

Code:

EXPORT CD2,CD1;
EXPORT Distance()
BEGIN
  LOCAL x,menu,unit,a1,a2,t;
  INPUT({{ x,[2],{45,20,3} }},"","Convert ");
  IF string(x,1) == "0" THEN RETURN; END;
  unit := { 1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile };
  menu := { "mm","cm","m","km","inches","feet","yards","miles" };
  CHOOSE(CD2, "Units", menu);
  CHOOSE(CD1, "To", menu);
  a2 := unit[CD2];
  a1 := unit[CD1];
  t := x + "*" + CONVERT(a2,a1);
  expr(t);
END;

You don't want to use EXPR() -- you want CAS() because it will allow for undefined variables in addition to defined variables and numerical values for x. EXPR() expects everything to be resolvable and will return an error if there is a reference that cannot be resolved. On the other hand, if you know that you will never want to create something like t_m where t is an undefined variable, then stick with EXPR().

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
10-18-2015, 05:52 AM (This post was last modified: 10-18-2015 03:45 PM by epp.)
Post: #12
RE: CONVERT and variables for arguments
Ahh, I only intended to have numeric input. My fault for not mentioning this. Well, with this in mind things got a bit simpler. The reason my original code didn't work was because I was concatenating to create the 1_ object rather than simply specifying the object directly in the list. This created a string containing the of the name of the object and that wouldn't work unless it was evaluated via expr or cas.

In summary, CONVERT does accept variables for arguments but those variables must contain an appropriate object. I wish to thank both Han and Eried for their patience and assistance.

Code:

EXPORT CDFrom,CDTo;
EXPORT Distance()
BEGIN
  LOCAL x,menu,unit;
  INPUT({{ x,[0],{45,20,3} }},"","Convert ");
  IF x == 0 THEN RETURN; END;
  unit := { 1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile };
  menu := { "mm","cm","m","km","inches","feet","yards","miles" };
  CHOOSE(CDFrom, "Units", menu);
  CHOOSE(CDTo, "To", menu);
  x*CONVERT(unit[CDFrom],unit[CDTo]);
END;
Visit this user's website Find all posts by this user
Quote this message in a reply
10-19-2015, 01:18 PM
Post: #13
RE: CONVERT and variables for arguments
(10-17-2015 01:40 PM)eried Wrote:  "mm","cm","m","km","inches","feet","yards","miles" can go in a list variable so you don't need to duplicate them

One option to avoid typing in duplicate lists is to assign "menu :=units", which saves having to type in all the units in the menu. You end up having units like " 1 mm" instead of "millimetres" - that's not pretty, but on the other hand its workable with less typing AND there is no risk of the two lists getting out of step, which could otherwise risk turn your "miles" into mm (if the two lists end up in different sequences). The disadvantage is you then have to go online to discover that an "lyr" is actually a light year.

If you do keep the printable names as well as the units, if the lists can be changed to:
Mm,"millimetres"
Yds,"yards" //keep adjacent

They are less likely to go wrong than if you have
Mm,rods,chains,furlongs,yds
"Mm"',chains"," rods","furlong","yards"

It may also be worth pointing out that Han's technique does not extend to Temperatures, because of the differing zeros, in case anyone is tempted to do that.

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
10-21-2015, 09:26 AM (This post was last modified: 10-21-2015 11:36 AM by StephenG1CMZ.)
Post: #14
RE: CONVERT and variables for arguments
I have now incorporated this capability into my ZIPP program.
I have made some usability changes.
I changed the sequence to prompt for from units first...
What's the point in typing 12 for 12cm before you have checked the program does cm: it might only do mm and m. This also means the units can be included in the input value prompt.
Doing it the other way round, it was easy to tap the wrong units and get no warning, particularly since my screen is smaller than recommended.
I have noticed that some HP units give an inconsistent units error - I wonder whether IFERR would trap these cases? (update: yes it can)
Hope you find this program useful (there are some units not included).

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
10-21-2015, 10:14 AM (This post was last modified: 10-21-2015 11:10 AM by epp.)
Post: #15
RE: CONVERT and variables for arguments
I like your suggestions Stephen. I often have better luck pressing the number keys to make a selection.

Code:

EXPORT CDFrom,CDTo;
EXPORT Distance()
BEGIN
  LOCAL x,menu,unit;
  unit := {1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile};
  menu := {"mm", "cm", "m", "km", "inches", "feet", "yards", "miles"};
  CHOOSE(CDFrom, "From", menu);
  CHOOSE(CDTo, "To", menu);
  INPUT({ {x,[0],{45,20,3}} }, "Convert", menu[CDFrom] + " ");
  x*CONVERT(unit[CDFrom], unit[CDTo]);
END;
Visit this user's website Find all posts by this user
Quote this message in a reply
10-21-2015, 11:26 AM
Post: #16
RE: CONVERT and variables for arguments
(10-21-2015 10:14 AM)epp Wrote:  I like your suggestions Stephen.

Code:

EXPORT CDFrom,CDTo;
EXPORT Distance()
BEGIN
  LOCAL x,menu,unit;
  unit := {1_mm, 1_cm, 1_m, 1_km, 1_inch, 1_ft, 1_yd, 1_mile};
  menu := {"mm", "cm", "m", "km", "inches", "feet", "yards", "miles"};
  CHOOSE(CDFrom, "From", menu);
  CHOOSE(CDTo, "To", menu);
  INPUT({ {x,[0],{45,20,3}} }, "Convert", menu[CDFrom] + " ");
  x*CONVERT(unit[CDFrom], unit[CDTo]);
END;

My own preference is to have the INPUT value between the two units...
It seems confusing to me to select " to m" and then start typing in a value in mm rather than m (where mm is the from unit). As a user, I'm sure I would sometimes type in the value using "to units" instead by mistake. And the from and to screens are so similar its not always obvious that you have moved from from to to. Having the sequence " from, value, to" seems much clearer to me. Compare with the sequence in ZIPP and let me know what you think.

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
10-21-2015, 01:14 PM
Post: #17
RE: CONVERT and variables for arguments
What is ZIPP?
Visit this user's website Find all posts by this user
Quote this message in a reply
10-21-2015, 01:23 PM
Post: #18
RE: CONVERT and variables for arguments
(10-21-2015 01:14 PM)epp Wrote:  What is ZIPP?

My collection of practical programs, now including unit conversions. I have just placed it in the HP Prime software library.

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
10-21-2015, 03:06 PM (This post was last modified: 10-21-2015 03:14 PM by epp.)
Post: #19
RE: CONVERT and variables for arguments
Ah, I see I'm re-inventing the wheel. I suspected as such since this is one of the things on older calculators that's missing from the PRIME. Either way works well.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-21-2015, 03:50 PM
Post: #20
RE: CONVERT and variables for arguments
(10-21-2015 03:06 PM)epp Wrote:  Ah, I see I'm re-inventing the wheel. I suspected as such since this is one of the things on older calculators that's missing from the PRIME. Either way works well.

Not at all. Its thanks to your and Han's contributions on this thread that I got started on the Convert utility in my program...apart from me adding a few usability tweaks, most of the effort was typing in all those units - given that the hard part of sorting out how to do a conversion was already solved here. Thanks to both of you.

It would be nice if a list of those units were available from HP (accessible from the code).
I know I haven't included them all.

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
Post Reply 




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