CONVERT and variables for arguments
10-24-2015, 09:35 PM (This post was last modified: 10-27-2015 09:19 PM by epp.)
Post: #24
 epp Junior Member Posts: 23 Joined: Oct 2015
RE: CONVERT and variables for arguments
By storing information in lists the code is shorter and modification is easier. The function, CNV(X), converts between units where X can be a real (18) or a unit (18_inch). Distance, Volume, and Weight have been implemented. Works under HOME and CAS.

RPN:
18 ENTER
CNV

TEXTBOOK:
CNV(18)

If the original number you are converting from is in units the answer will be in terms of units. For example, when converting 18 inches to feet we have

CNV(18_inch) -> 1.5_ft
CNV(18) -> 1.5

The following code can easily be modified to include other data types.

Code:
 #pragma mode(separator(.,;) integer(h32)) // when changing menus update the following: cFr, cTo, unit, prog cFr = {1,1,1}; cTo = {1,1,1}; iPr; EXPORT CNV(x) begin   local unit = {     {"mm", "cm", "m", "km", "inch", "ft", "yd", "mile"},     {"ml", "l", "tsp", "tbsp", "qt", "inch^3", "ft^3"},     {"g", "kg", "oz", "lb"}   };   local prog = {     "Distance",     "Volume",     "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 x; 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;

To place CNV on the command line choose

Toolbox > User > CNV > CNV

If you have previously visited this menu then choose Toolbox and keep pressing ENTER. Previous choices are saved by both the calculator and the CNV program.

Alternatively you can bind a key to insert "CNV" and then choose

Shift > Help > KEY

where KEY is the one you bind to CNV. The following code binds the Units key to CNV.

Code:
 KEY K_Templ() BEGIN RETURN "CNV"; END;

On a small screen selecting menu items while running CNV can be a challenge. Instead use the rocker to navigate to the selection or use the keyboard and choose a number that corresponds to the selection. Implementing temperature conversions (Fahrenheit/Celsius) fail under firmware 8151 due to the lack of arithmetic support.
 « Next Oldest | Next Newest »

 Messages In This Thread CONVERT and variables for arguments - epp - 10-15-2015, 06:16 PM RE: CONVERT and variables for arguments - eried - 10-15-2015, 07:05 PM RE: CONVERT and variables for arguments - Han - 10-15-2015, 07:08 PM RE: CONVERT and variables for arguments - epp - 10-15-2015, 07:46 PM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-15-2015, 09:25 PM RE: CONVERT and variables for arguments - Han - 10-15-2015, 09:31 PM RE: CONVERT and variables for arguments - epp - 10-16-2015, 01:14 PM RE: CONVERT and variables for arguments - eried - 10-17-2015, 01:40 PM RE: CONVERT and variables for arguments - epp - 10-17-2015, 02:13 PM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-19-2015, 01:18 PM RE: CONVERT and variables for arguments - epp - 10-17-2015, 10:03 PM RE: CONVERT and variables for arguments - Han - 10-18-2015, 01:19 AM RE: CONVERT and variables for arguments - epp - 10-18-2015, 05:52 AM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-21-2015, 09:26 AM RE: CONVERT and variables for arguments - epp - 10-21-2015, 10:14 AM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-21-2015, 11:26 AM RE: CONVERT and variables for arguments - epp - 10-21-2015, 01:14 PM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-21-2015, 01:23 PM RE: CONVERT and variables for arguments - epp - 10-21-2015, 03:06 PM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-21-2015, 03:50 PM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-21-2015, 06:59 PM Conversions Implemented as a Function - epp - 10-22-2015, 11:35 AM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-22-2015, 11:12 PM RE: CONVERT and variables for arguments - epp - 10-24-2015 09:35 PM RE: CONVERT and variables for arguments - StephenG1CMZ - 10-30-2015, 08:26 AM

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