HP Forums
(42S, Free42, DM42) Solving for HMS values - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (42S, Free42, DM42) Solving for HMS values (/thread-15695.html)

(42S, Free42, DM42) Solving for HMS values - Werner - 10-08-2020 12:44 PM

Solving for HMS values may introduce wrong values.

An example.
Suppose your daily commute counts as working hours, except for the first 20 minutes each way.
Say your commute is 1 hour, each way, and you have a lunch break of half an hour.
Your daily working time is set to 7:36.
If you leave home at 7:15, when would you then be allowed to leave the office, to ensure a full day's work?

LH: the time you Leave Home in the morning
C: the duration of your Commute, one-way
LB: Lunch Break
WT: your daily Working Time
when would you be allowed to leave the office (LO)?

The moment you arrive back home is:

LH + 20m + WT + LB + 20m
LO = LH + WT + LB + 40m - C

if your commute is less than 20 minutes, it is

LO = LH + WT + LB + C

Try a solver program (for now only for C>20m):

 RCL "C"

SOLVE "FW", enter
WT = 7.36
LH = 7.15
LB = 0.30
C = 1

solve for LO with guesses 0 and 16 and get 14.6060 - which ought to be 15.0100.
(the guesses are not really necessary but force the result)
This value is equivalent to 15.01, as you can see when you do 0 HMS+.
There is however no way to tell the solver to only use valid H.MS values.
So we'll have to correct the solved value. Since we may also run into accuracy problems (eg 14.60599999..)
we will build a front-end to the solver, and round the value obtained to the nearest minute.
This time we include the case where C is less than 20m as well, as follows:

If C<20 Then add C Else add (40 - C) End
If C<(40-C) Then add C Else add (40 - C) End

or add MIN(40-C,C)

Let's make the Forfait of 20m a variable as well (FF).
Since FF and WT are variables that won't change often, we'll put them on the next page, using a dummy " " variable:

00 { 114-Byte Prgm }
01▸LBL "FW"
02 MVAR "LH"
03 MVAR "LB"
04 MVAR "C"
05 MVAR "LO"
06 MVAR " " @ fill up the page
07 MVAR " "
08 MVAR "FF" @ rarely used variables on 2nd page
09 MVAR "WT"
10 FS? 45 @ Solver running flag
11 GTO 00

13▸LBL 10 @ -- Menu loop --
16 ASTO ST L @ set up solver
17 CLX
20 2ᴇ31 @ round result to the nearest minute, Free42 specific
21 HMS+
23 -
25 STO IND ST L @ store in variable you solved for
26 ├"=" @ show solver message
29 CLA
31 GTO 10

32▸LBL 00 @ -- solver equation --
33 RCL "FF"
35 HMS+
36 RCL "C"
37 HMS-
39 X>Y?
40 X<>Y
41 RCL "LH"
42 HMS+
43 RCL "WT"
44 HMS+
45 RCL "LB"
46 HMS+
47 RCL "LO"
48 HMS-
49 END

Now, you can observe the difference:
if you use the SOLVER, with the original example, you get 14.606
If you do XEQ "FW" instead, the interface is the same (apart from inputting the guesses), but you get 15.01

In general, if you have a HMS solver program (FW here), we can do
A: "FW"
X: 0.01 @ round to multiple of (in HH.MMSS style so if you want to round to a quarter, use 0.15)

(Of course, for the 42S, change the LSTO statements into STO's)

00 { 64-Byte Prgm }
02 →HR
03 LSTO "R"
04 LSTO "P"
05 ASTO "P"
07▸LBL 10
10 ASTO ST L @ set up solver
11 CLX
14 →HR @ round to a multiple of R
15 RCL÷ "R"
16 FP
17 RCL+ ST L
18 IP
19 RCL× "R"
20 →HMS
22 STO IND ST L @ store in variable you solved for
23 ├"=" @ show solver message
26 CLA
27 ARCL "P"
28 GTO 10
29 END

Problem: when you EXIT the VARMENU, the local variables R and P still exist. But when the MENU is shown, they should exist. Dilemma.

Hope you like it,

RE: (42S, Free42, DM42) Solving for HMS values - rprosperi - 10-08-2020 10:06 PM

Interesting real-world issue.

Why not simply use the clever trick you point out at the beginning (0 HMS+ to normalize) applied to the result? Seems like a lot less thrashing, no ?

RE: (42S, Free42, DM42) Solving for HMS values - Werner - 10-09-2020 03:21 AM

Because I want the program to return the result? And I round it to the nearest minute as well.
Cheers, Werner

RE: (42S, Free42, DM42) Solving for HMS values - Werner - 10-15-2020 07:44 AM

(10-08-2020 10:06 PM)rprosperi Wrote:  Seems like a lot less thrashing, no ?

I wrote the above routine for my daughter-in-law-to-be who knows nothing about calculators in general or HPs in particular. So it had to be as user-friendly as possible.
I thought it might interest people here, because it shows the use of a number of techniques:
  • calling the solver from within a program
  • the use of flag 45 to do so with a single alpha label
  • rounding HMS values
  • the use of VARMENU
  • putting rarely used variables in the second page of the variable menu
Alas, it turned out to be a lot of thrashing instead.

RE: (42S, Free42, DM42) Solving for HMS values - rprosperi - 10-15-2020 12:40 PM

Thanks for explaining. Indeed it's a small who's who of many of the 42's unique and interesting commands, demonstrating them well in a relatively small, focused application. I especially like the statement "SOLVE IND ST L", which combines some of these nicely.

I find there is always something to learn in your posts and programs - keep it up please.

RE: (42S, Free42, DM42) Solving for HMS values - Albert Chan - 10-15-2020 01:13 PM

(10-08-2020 12:44 PM)Werner Wrote:  Solving for HMS values may introduce wrong values...
solve for LO with guesses 0 and 16 and get 14.6060 - which ought to be 15.0100.

Luckily, this is "only" a display bug

For HMS calculations, Casio shines, since it knows sexagesimal.
Sharp calculator is even better, with less keystrokes:

7 [DMS] 36 - [DMS] 40 * 2 = 6°16' // work-related time, less commute allowances
+ 1 [DMS] 30 = 7°46'                    // + non-work time, commute (1 way) + lunch break
+ 7 [DMS] 15 = 15°01'                  // leaving office at 3:01 pm