Equations, units and things.. (plus42)

General discussion about calculators, SwissMicros or otherwise
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Thomas Okken wrote:
Wed Aug 17, 2022 1:07 am
It's actually a rather straightforward algorithm
I actually used my plus42 to convert using modesty units from Thomas Okken to everyone else ... :lol:
Thomas Okken wrote:
Wed Aug 17, 2022 1:07 am
use the CONVERT function
Thanks yes, that makes sense.

I might look into how a program + solver could do an optional range. Though I have to admit, that I often struggle to get my head round programming these devices and all my code looks like spaghetti. I'm glad to see you seem to have added if and for functions into the device so maybe that will help!

This might a be crazy idea, but have you ever pondered adding some ability to allow python or other code to run on the plus42? Maybe opening up plus42 to an API, allowing modules to be attached. I imagine it'd probably need the whole of plus42 re-compiled for adding in programs in a different language, and I guess that doesn't really work with interpreted languages. Maybe it would only work with fortran?
SN:09075
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

PS, sorry to be yet another person to say 'have you ever thought of doing this...' :D I promise this isn't a request.
SN:09075
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Equations, units and things.. (plus42)

Post by Thomas Okken »

dm319 wrote:
Wed Aug 17, 2022 4:10 pm
This might a be crazy idea, but have you ever pondered adding some ability to allow python or other code to run on the plus42?
I have, actually... RPN with the big stack and local variables is a pretty easy target for code generation, and the thought has occurred to me that I could support RPL by compiling it to RPN. The equation language is handled that way as well.

It seems like a very niche use case, though. I don't think I could justify spending the time on that.
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Thomas Okken wrote:
Wed Aug 17, 2022 6:05 pm
It seems like a very niche use case, though.
If I needed to write a new program on my free42/plus42, and I had the choice of RPN keystroke or a language with structural elements like if, for, switch, named functions (with arguments), code blocks, etc, I know what I'd choose! I get that RPN keystroke makes sense for programming when you only have your calculator to hand, but I'm rarely that far from my laptop these days.
SN:09075
User avatar
RJvM
Posts: 276
Joined: Wed Jun 02, 2021 9:21 am
Location: Gelderland, Netherlands

Re: Equations, units and things.. (plus42)

Post by RJvM »

If your laptop is always near... this begs the question why bother with a calculator at all...

Although personally, I greatly prefer calculators for ad hoc calculations and for on the go computing power.

A Python to RPN-Keystroke programming conversion tool might be powerful, but this kind of code generators (almost) always produce unreadable code, or at least code you should not subsequently modify manually.

This would be contrary to the way you would use a calculator, imho.
Robbert Jan, MSEE, RPN user since 1976 and a collector for many years I now own all the important ones: HP-35, 45, 55, 65, 97, 19, 21, 25, 34, 10-16, 41, 42, 71, 48, 50, Prime, DM41, DM42, WP43, C47, R47; Project 47 team member https://47calc.com
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

RJvM wrote:
Fri Aug 19, 2022 5:49 pm
If your laptop is always near... this begs the question why bother with a calculator at all...
As I wrote that comment, I felt this reply was coming! I have a few counter reasons:

1. A calculator has an interface specifically designed for quick calculations

And many people choose to use a 'calculator' on a computer, phone, calculator. For me, personally, I find the RPN way of calculating things really intuitive and useful for checks and quick calculations. Just because you have a full laptop, doesn't mean that all your calculations will be done in excel, R, python, google, etc.. We still like to do quick calculations using an interface designed for that.

2. Laptops are cumbersome, even if near by:

I would pick my phone running free42/plus42 over my laptop running the same. I have a numpad on the phone, it doesn't sit on top of everything on my desk. My work computer doesn't run free42, so I'd need my personal laptop to hand. My line of work is clinical, so I'm often in a consulting room, or on a ward, it's always easier to use a small portable device for calculations.

3. You write a program infrequently:

If I need a program on my calculator, I am happy to find the time to write it. That time period might cover several weeks. During that time, I will be able to find some time where I can use a laptop to write a program. In contrast when I need a calculation, I usually need it right there and then. A quick tally up of costs for items, or splitting costs etc.. I work in a clinical setting, so I often need to calculate body surface area, BMI, sense-check drug doses, or more complicated medical calculators for predicting risks of treatments etc..
SN:09075
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

RJvM wrote:
Fri Aug 19, 2022 5:49 pm
A Python to RPN-Keystroke programming conversion tool might be powerful, but this kind of code generators (almost) always produce unreadable code, or at least code you should not subsequently modify manually.
Hang on... you are already using unreadable code for any function on the calculator (Ln, Cos, Sin etc..) as well as any key-stroke program that was written by someone else or by yourself more than 24 hours ago. Once your program gets more than 2 GOTOs, I find it very hard to see what is going on!

The appeal of being able to use a modern structured programming languages is that you can clearly see the code, with comments, indentation, curly brackets to identify code blocks that run etc..

I wasn't suggesting a code-generator, rather some kind of interface from free42/plus42 to source code written in a numerical programming language. I guess that could be as source code for interpreted languages, or compiled binaries if not. In my imagination, it could take 0 to 4 arguments from the stack, manipulate them in their language, and return the result to the stack or alpha register. Programming not being my main job, I really have no idea how this code could run. I guess the 'calculator' would need to be able to run python/similar, or the compiled binaries.
SN:09075
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Equations, units and things.. (plus42)

Post by Thomas Okken »

Well, compiling Python to RPN has been done already:

viewtopic.php?f=15&t=495

And that was before the big stack and local variables, so it could be done even better now.

Building this into Plus42 would be a bit of a job, though, and it wouldn't surprise me if the UI were to be a bigger task than the compiler!

(The non-readability of generated RPN code shouldn't be a concern. The code that is generated by PARSE in Plus42 isn't tremendously readable either, but that's not the point... If you don't use TRACE or SST to execute it, you'll never see it.)
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Thomas Okken wrote:
Fri Aug 19, 2022 7:13 pm
Well, compiling Python to RPN has been done already:
Oh, this is news to me! I think I might be confusing everyone by not being clear - it isn't that I'd want to write python code on the calculator. Quite happy to write it on a laptop or similar. But what would be nice would then be to just drop it onto the phone/calculator, and have it appear as a program!

A python to RPN converter on a desktop machine would be fine - no need to change free42/plus42! I will have a play with that, though my very very initial look on the website I'm slightly concerned I can't see the source code / license.
SN:09075
User avatar
dm319
Posts: 144
Joined: Thu Aug 03, 2017 12:21 pm
Location: Birmingham, UK

Re: Equations, units and things.. (plus42)

Post by dm319 »

Had a bit of fun with the converter.

I used some code for this maths problem https://projecteuler.net/problem=44. Basically can you find the smallest pair of pentagonal numbers where their sum and difference are also pentagonal numbers.

This is my python code that went into pyrpn:

Code: Select all

def p_demo():
  result = False
  i = 1

  while not result:
      j = 1
      while j < i:
          result = isspecial(i,j)
          if result:
              print("P%d(%d)\tP%d(%d)\n") % (i, gen_pent(i), j, gen_pent(j))
              break
          j += 1
      i += 1

  print("end")

def gen_pent(n):
    p = n*(3*n-1)/2
    return(p)

def ispentagonal(p):
    a = 3.0/2.0
    b = -(1.0/2.0)
    c = -(p)

    x = (-b + SQRT(b*b-4*a*c)) / (2*a)

    return(x == IP(x))

def isspecial(x,y):
    a = gen_pent(x)
    b = gen_pent(y)

    diff = ABS(a - b)

    difftest = ispentagonal(diff)
    sumtest = ispentagonal(a+b)

    return(difftest and sumtest)
This wasn't the pure python code, some things had to change, such as int() -> IP(), abs() -> ABS(), math.sqrt() -> SQRT(). But it did generate this:

Code: Select all

LBL "p_demo"
0
STO 00
1
STO 01
LBL 00
RCL 00
XEQ 70
XEQ 87
X≠0?
GTO 01
GTO 02
LBL 01
1
STO 02
LBL 03
RCL 02
RCL 01
XEQ 79
X≠0?
GTO 04
GTO 05
LBL 04
RCL 01
RCL 02
XEQ C
STO 00
RCL 00
X≠0?
GTO 06
GTO 07
LBL 06
AVIEW
RCL 01
RCL 01
XEQ A
RCL 02
RCL 02
XEQ A
MOD
GTO 05
LBL 07
1
STO+ 02
GTO 03
LBL 05
1
STO+ 01
GTO 00
LBL 02
"end"
AVIEW
RTN
LBL A
STO 03
RDN
RCL 03
3
RCL 03
*
1
-
*
2
/
STO 04
RCL 04
RTN
LBL B
STO 05
RDN
3.0
2.0
/
STO 06
-1.0
2.0
/
STO 07
RCL 05
CHS
STO 08
RCL 07
CHS
RCL 07
RCL 07
*
4
RCL 06
*
RCL 08
*
-
SQRT
+
2
RCL 06
*
/
STO 09
RCL 09
RCL 09
IP
XEQ 72
RTN
LBL C
XEQ 65
STO 10
RDN
STO 11
RDN
RCL 10
XEQ A
STO 12
RCL 11
XEQ A
STO 13
RCL 12
RCL 13
-
ABS
STO 14
RCL 14
XEQ B
STO 15
RCL 12
RCL 13
+
XEQ B
STO 16
RCL 15
RCL 16
XEQ 63
AND
RTN
LBL 50
"-Utility Funcs-"
RTN
LBL 63
XEQ 70
X<>Y
XEQ 70
X<>Y
RTN
LBL 65
X<>Y
RTN
LBL 70
CF 00
X≠0?
SF 00
RDN
FS? 00
1
FC? 00
0
RTN
LBL 72
CF 00
X=Y?
SF 00
XEQ 61
RTN
LBL 79
CF 00
X>Y?
SF 00
XEQ 61
RTN
LBL 87
X≠0?
SF 00
RDN
FS? 00
0
FC? 00
1
RTN
LBL 61
RDN
RDN
FS? 00
1
FC? 00
0
RTN
Which looks as hard to follow as my own HP-42 code :D :

Code: Select all

00 { 141-Byte Prgm }
01 LBL "PENT"
02 2
03 STO 01
04 1
05 STO 00
06 LBL A
07 RCL 01
08 RCL 00
09 XEQ "P"
10 STO 03
11 X<>Y
12 XEQ "P"
13 STO 04
14 +
15 XEQ "ISPENT"
16 ENTER
17 IP
18 X≠Y?
19 GTO C
20 RCL 04
21 RCL 03
22 -
23 XEQ "ISPENT"
24 ENTER
25 IP
26 X≠Y?
27 GTO C
28 XEQ "P"
29 CLA
30 AIP
31 AVIEW
32 RTN
33 LBL B
34 RCL 01
35 STO 00
36 1
37 +
38 STO 01
39 GTO A
40 LBL C
41 RCL 00
42 1
43 -
44 STO 00
45 X≠0?
46 GTO A
47 GTO B
48 LBL "P"
49 ENTER
50 ENTER
51 3
52 x
53 1
54 -
55 x
56 2
57 ÷
58 RTN
59 LBL "ISPENT"
60 +/-
61 6
62 x
63 0.25
64 X<>Y
65 -
66 SQRT
67 0.5
68 +
69 3
70 ÷
71 RTN
72 .END.
That was fun. Though I'll have to check if the compiled code actually gives me the right answer.

NB: A far more elegant python solution using set() wouldn't run as set() isn't implemented.
SN:09075
Post Reply