Post Reply 
(42S, Free42, DM42) Solving for HMS values
10-08-2020, 12:44 PM
Post: #1
(42S, Free42, DM42) Solving for HMS values
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?

Given
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
so
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):

Code:
 LBL "FW"
 MVAR "WT"
 MVAR "LH"
 MVAR "LB"
 MVAR "C"
 MVAR "LO"
 RCL "LH"
 RCL "WT"
 HMS+
 RCL "LB"
 HMS+
 0.4
 HMS+
 RCL "C"
 HMS-
 RCL "LO"
 HMS-
 END

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:

Code:
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
12 PGMSLV "FW"

13▸LBL 10 @ -- Menu loop --
14 VARMENU "FW"
15 STOP
16 ASTO ST L @ set up solver
17 CLX
18 STO IND ST L
19 SOLVE IND ST L
20 2ᴇ31 @ round result to the nearest minute, Free42 specific
21 HMS+
22 LASTX
23 -
24 ASTO ST L
25 STO IND ST L @ store in variable you solved for
26 ├"=" @ show solver message
27 ARCL IND ST L
28 AVIEW
29 CLA
30 ARCL ST L
31 GTO 10

32▸LBL 00 @ -- solver equation --
33 RCL "FF"
34 ENTER
35 HMS+
36 RCL "C"
37 HMS-
38 LASTX
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
In:
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)
XEQ "HMSLV"

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

Code:
00 { 64-Byte Prgm }
01▸LBL "HMSLV"
02 →HR
03 LSTO "R"
04 LSTO "P"
05 ASTO "P"
06 PGMSLV IND "P"
07▸LBL 10
08 VARMENU IND "P"
09 STOP
10 ASTO ST L @ set up solver
11 CLX
12 STO IND ST L
13 SOLVE IND ST L
14 →HR @ round to a multiple of R
15 RCL÷ "R"
16 FP
17 RCL+ ST L
18 IP
19 RCL× "R"
20 →HMS
21 ASTO ST L
22 STO IND ST L @ store in variable you solved for
23 ├"=" @ show solver message
24 ARCL IND ST L
25 AVIEW
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,
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
10-08-2020, 10:06 PM
Post: #2
RE: (42S, Free42, DM42) Solving for HMS values
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 ?

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
10-09-2020, 03:21 AM
Post: #3
RE: (42S, Free42, DM42) Solving for HMS values
Because I want the program to return the result? And I round it to the nearest minute as well.
Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
10-15-2020, 07:44 AM
Post: #4
RE: (42S, Free42, DM42) Solving for HMS values
(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.
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
10-15-2020, 12:40 PM
Post: #5
RE: (42S, Free42, DM42) Solving for HMS values
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.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
10-15-2020, 01:13 PM
Post: #6
RE: (42S, Free42, DM42) Solving for HMS values
(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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: