(12C) Check if two numbers is to the power of other
09-06-2018, 11:00 AM (This post was last modified: 09-06-2018 01:45 PM by Gamo.)
Post: #1
 Gamo Senior Member Posts: 623 Joined: Dec 2016
(12C) Check if two numbers is to the power of other
Program to check if the given two numbers is to the power of other.

Procedure:

n1 [ENTER] n2 [R/S] or n2 [ENTER] n1 [R/S]

Display 0 // Two numbers is not the power of the other
Display 1 // Two numbers is the power of the other

When display show 1 user can recheck for base integer, power integer and X^Y result

Display 1
Rv // Base Integer
Rv // Power Interger

Example:

Is base 5 power to 625 ?

5 [ENTER] 625 [R/S] // Number input can switch around 625 [ENTER] 5 [R/S]

1 Display // True
Rv Display 5 // Base Number
Rv Display 4 // Power Number
Rv Display 625 // X^Y result

----------------------------------------------------------------------------------------

Is base 101 power to 104060401 ?

101 [ENTER] 104060401 [R/S]

1 Display // True
Rv Display 101 // Base Number
Rv Display 4 // Power Number
Rv Display 104060401 // X^Y result

----------------------------------------------------------------------------------------

Is base 2 power to 4099 ?

4099 [ENTER] 2 [R/S]

0 Display // Two numbers not a power to other

----------------------------------------------------------------------------------------

Program:
Code:
 01 X≤Y 02 X<>Y 03 X<>Y 04 STO 1 05 X<>Y 06 STO 2 07 CLx 08 STO 3 09  1 10 STO+3 11 RCL 3 12 RCL 1 13 X<>Y 14 Y^X 15 STO 4 16 RCL 2 17 X≤Y 18 GTO 20 19 GTO 09 20 RCL 4 21 RCL 2 22  - 23 X=0 24 GTO 27 25  0 26 GTO 00 27 RCL 2 28 RCL 3 29 RCL 1 30  1 31 GTO 00

Gamo
09-06-2018, 07:07 PM (This post was last modified: 09-06-2018 07:08 PM by stored.)
Post: #2
 stored Junior Member Posts: 15 Joined: Sep 2016
RE: (12C) Check if two numbers is to the power of other
Interesting.
Perhaps it is more practical to use logarithmic formula:
if a=b^n where a, b, n -- integers, a,b>0, then n=ln(a)/ln(b) -- also must be integer number.
But the problem of this approach is a big rounding errors in some cases, so it is necessary to make a direct verification.
09-06-2018, 07:59 PM
Post: #3
 Albert Chan Senior Member Posts: 1,242 Joined: Jul 2018
RE: (12C) Check if two numbers is to the power of other
Hi, stored.

No log is needed. Just fill the stack

Example: 104060401 vs 101

101 Enter Enter Enter x x x

--> 101^4 thus match 104060401
09-07-2018, 03:09 AM
Post: #4
 Gamo Senior Member Posts: 623 Joined: Dec 2016
RE: (12C) Check if two numbers is to the power of other
This program work ok on HP-12C but so far not work on HP-12C Platinum for Android but HP-12C Platinum on PC work fine.

What wrong with the Android version?

Thank You

Gamo
09-07-2018, 07:35 AM (This post was last modified: 09-07-2018 07:36 AM by Dieter.)
Post: #5
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-07-2018 03:09 AM)Gamo Wrote:  This program work ok on HP-12C but so far not work on HP-12C Platinum for Android but HP-12C Platinum on PC work fine.

What wrong with the Android version?

What's wrong with the program?
What exactly does not work on the Android version?
What is your input and what is the output?

You can check this yourself: enter two numbers and use the SST key to step through the program, one line after another. Where exactly does the Android program deviate from the others? Where exactly is the point where the program behaves differently?

Dieter
09-07-2018, 09:04 AM (This post was last modified: 09-07-2018 09:07 AM by Gamo.)
Post: #6
 Gamo Senior Member Posts: 623 Joined: Dec 2016
RE: (12C) Check if two numbers is to the power of other
Sorry my bad forgot to explain of the problem.
For 12C Platinum on Android I put exactly the same program steps with step numbers like 09 for 12C and 009 for 12C Platinum.
Program run but always return 0 display eventhough
it is true and all the store registers result are correct answer.

Example:
Is 5 to the power of 25

5 ENTER 25 R/S

Display 0 // suppose to be display 1

R1 is 5
R3 is 2
R2 is 25

Remark: 12C Platinum on PC work fine.

Gamo
09-07-2018, 01:42 PM
Post: #7
 rprosperi Senior Member Posts: 4,461 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-07-2018 09:04 AM)Gamo Wrote:  Sorry my bad forgot to explain of the problem.
For 12C Platinum on Android I put exactly the same program steps with step numbers like 09 for 12C and 009 for 12C Platinum.
Program run but always return 0 display eventhough
it is true and all the store registers result are correct answer.

Example:
Is 5 to the power of 25

5 ENTER 25 R/S

Display 0 // suppose to be display 1

R1 is 5
R3 is 2
R2 is 25

Remark: 12C Platinum on PC work fine.

Gamo

I can confirm the program as listed in the OP does not produce correct results on HP's HP-12CP Android emulator. I have not tried it on a real 12CP or any other emualtor. Yet.

In the example just above (5,25) the resulting registers (on Android) are:

R1: 5
R2: 25
R3: 3

I don't have time right now to SST through on a real machine side-by-side with the emulator, but will try to do so later today.

Meanwhile, if you have time to look at it Dieter, perhaps the incorrect R3 value above is a hint for where the error lies?

It looks like Gamo found a real bug in the Android emulator. Well done Gamo.

--Bob Prosperi
09-07-2018, 02:06 PM (This post was last modified: 09-07-2018 02:08 PM by Albert Chan.)
Post: #8
 Albert Chan Senior Member Posts: 1,242 Joined: Jul 2018
RE: (12C) Check if two numbers is to the power of other
The algorithm assumed Y^X always return integer if X and Y are integers.
(Not just displayed as integer, but the internal precision digits too)

That might not be true.

Might be better to use regular multiply to build Y^X
09-07-2018, 02:40 PM (This post was last modified: 09-08-2018 05:14 AM by Gamo.)
Post: #9
 Gamo Senior Member Posts: 623 Joined: Dec 2016
RE: (12C) Check if two numbers is to the power of other
I try to check the program line by line using SST as Dieter mention.
The example problem is 5 to what power is to 25.

When Y^X loop to 25 at line 017 it keep looping to line 009 one more time to 125 and exit.

This suppose to exit exactly on 25 since X is less than or equal to Y conditional test.

This program work normally on regular 12C

Thank you
Gamo
09-07-2018, 03:21 PM (This post was last modified: 09-07-2018 03:22 PM by rprosperi.)
Post: #10
 rprosperi Senior Member Posts: 4,461 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
Update: Found the problem, and it's weird. (Gamo posted while writing this, seems he's found the same thing)

Using input: 5 [Enter] 25 [R/S] (or in this case using SST):

On the 2nd pass through steps 9-17, upon hitting the test in line 17, the stack is:

T: 25
Z: 1
Y: 25
X: 25

On a real 12CP, the test (X<=Y) is true, so execution continues on line 18.

On the Android 12CP, the test (X<=Y) fails, and execution continues on line 19 <==== [ERROR].

First thought was perhaps it really tests (X<Y) only and ignores the [=] part, but a simple test program reveals this is not the case:

Code:
001 X<=Y 002 GTO 005 003 0 004 GTO 006 005 1 006 GTO 000

works right every time.

So, somehow there is an internal issue causing the [X<=Y] test to produce improper results upon multiple passes of a program loop.

A few more test cases however show the problem does not always occur:

3, 9 => 0 [ERROR]
5, 625 => 0 [ERROR]
4,16 => 0 [ERROR]

but

2, 4 => 1 [Correct]

Could someone else with the Android emulator please verify these results before reporting it to HP?

12CP Emulator version: 1.7.1, Mar 23, 2017

--Bob Prosperi
09-07-2018, 08:49 PM
Post: #11
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-07-2018 02:40 PM)Gamo Wrote:  When Y^X loop to 25 at line 017 it keep looping to line 009 one more time to 125 and exit. This suppose to exit exactly on 25 since X is less than or equal to Y conditional test.

(09-07-2018 03:21 PM)rprosperi Wrote:  On the 2nd pass through steps 9-17, upon hitting the test in line 17, the stack is:

T: 25
Z: 1
Y: 25
X: 25

On a real 12CP, the test (X<=Y) is true, so execution continues on line 18.

On the Android 12CP, the test (X<=Y) fails, and execution continues on line 19 <==== [ERROR].

OK. Gamo and Bob: please repeat this case (5 and 25) once again and SST through the program until you get to the X≤Y? test in line 17. When this test fails and the next SST continues with the GTO 09 command, press [–] to see the difference between X and Y. Is it zero or something else?

Dieter
09-07-2018, 09:28 PM (This post was last modified: 09-07-2018 09:44 PM by rprosperi.)
Post: #12
 rprosperi Senior Member Posts: 4,461 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-07-2018 08:49 PM)Dieter Wrote:  OK. Gamo and Bob: please repeat this case (5 and 25) once again and SST through the program until you get to the X≤Y? test in line 17. When this test fails and the next SST continues with the GTO 09 command, press [–] to see the difference between X and Y. Is it zero or something else?

Actually I thought of that earlier and SST-ed through in FIX-9 mode and confirmed both values were 25.00000000, and it appeared to not be a round-off or loss pf precision issue.

BUT, your idea is clearly better as it actually performs the test under question and revealed the truth, the difference is -6.000000 -E13. (on the emulator)

Update 1: Forgot to mention that this difference is indeed 0.00000000 on the real 12CP.

So, I gather the Y^X in step 014 is the suspicious candidate.

Update 2: Doh! Yes, it's trivial to confirm on the emulator

[5] [Enter] [2] [Y^X] [25] [-] => -6.000000 -E13

Thanks Dieter.

--Bob Prosperi
09-07-2018, 09:36 PM
Post: #13
 Albert Chan Senior Member Posts: 1,242 Joined: Jul 2018
RE: (12C) Check if two numbers is to the power of other
(09-07-2018 08:49 PM)Dieter Wrote:  OK. Gamo and Bob: please repeat this case (5 and 25) once again and SST through the program until
you get to the X≤Y? test in line 17. When this test fails and the next SST continues with the GTO 09 command,
press [–] to see the difference between X and Y. Is it zero or something else?

Just the fact that it failed X≤Y test is enough to deduce X>Y
This is the same as the Microsoft calc bug: sqrt(4) - 2 = -8.1648 ... E-39
Sqrt(X) were using formula Exp(1/2 * ln(X))

For the same reason, android 12C Y^X may be the same way, Exp(X* ln(Y))
Technically, it is not a bug, since 5 ^ 2 = 25.00000000 is indeed 10 digits accurate.

If exact value of Y^X is required (small positive integer X), just build it with multiply (see post 8)
09-08-2018, 05:02 AM (This post was last modified: 09-08-2018 10:23 AM by Gamo.)
Post: #14
 Gamo Senior Member Posts: 623 Joined: Dec 2016
RE: (12C) Check if two numbers is to the power of other
Thanks to:
Albert Chan
Dieter
Bob
stored

The Y^X function is kind of complicated when use repeatedly in program.
I remember that Dieter mention this many time that when possible use ENTER [x]
is more efficient and Albert Chan also recommended to use ENTER [x] as well.

Remark: Real HP-12C and HP-12C for Android edition is working with [X^Y] loop in program.

Here is the update version specifically for the HP-12C (Platinum) Android edition.

Program:
Code:
 001 X≤Y 002 X<>Y 003 X<>Y 004 STO 1 005 X<>Y 006 STO 2 007 CLx 008 STO 3 009 STO 4 010 RCL 1 011 STO 4 012   1  013 STO 3  014   1  015 STO+3  016 RCL 4    017 RCL 1  018   x  019 STO 4  020 RCL 2  021 X≤Y 022 GTO 024  023 GTO 014  024 RCL 4    025 RCL 2  026   -  027 X=0    028 GTO 031  029   0 030 GTO 00    031 RCL 2    032 RCL 3  033 RCL 1  034   1  035 GTO 00

Now program work flawlessly when use [ENTER] [x] instead of [X^Y]

Remark:

When doing the test for example

5 [ENTER] 2 [X^Y] display 25 then input 2 [R/S] display 0 // This is because of the X^Y issue discussed here

Must input directly as 5 [ENTER] 25 [R/S] display 1

Gamo
09-08-2018, 07:28 AM
Post: #15
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
A bit shorter and with additional stack diagrams:
Code:
001 X≤Y     ; b     a 002 X<>Y    ; a     b 003 STO 2   ; a 004 X<>Y    ; b     a 005 STO 1   ; b 006 STO 4   ; p = b 007 1       ; 1 008 STO 3   ; n = 1 009 1       ; 1 010 STO+3   ; n = n + 1 011 RCL 4   ; p 012 RCL 1   ; b     p 013   x     ; p x b 014 STO 4   ; p = p x b 015 RCL 2   ; a     p 016 X≤Y     ; a ≤ p ? 017 GTO 019 ; done 018 GTO 009 ; loop 019   -     ; p - a 020 X=0     ; p = a ? 021 GTO 024 ; true 022   0     ; 0 023 GTO 000 ; return 0 024 RCL 2   ; a 025 RCL 3   ; n     a 026 RCL 1   ; b     n     a 027 1       ; 1     b     n     a 028 GTO 000 ; return 1

If you start with p = b you should set n = 1 since p = bn.
Now you don't have to adjust n at the end.

Cheers
Thomas
09-08-2018, 08:00 AM
Post: #16
 Thomas Klemm Senior Member Posts: 1,447 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-08-2018 07:28 AM)Thomas Klemm Wrote:  If you start with p = b you should set n = 1 since p = bn.

What happens if you enter the same number twice?

5
ENTER
R/S

0

That's probably not what you want.
You should rather start with p = 1 and set n = 0.

Kind regards
Thomas
09-08-2018, 04:44 PM
Post: #17
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-08-2018 08:00 AM)Thomas Klemm Wrote:  You should rather start with p = 1 and set n = 0.

Here is a modified program.
This time R4 is not required.

Code:
01 X≤Y? 02 X<>Y 03 STO 2 04 X<>Y 05 STO 1 06 CLX 07 STO 3 08 1 09 RCL 2 10 X≤Y? 11 GTO 19 12 1 13 STO+3 14 R↓ 15 R↓ 16 RCL 1 17 x 18 GTO 09 19 - 20 X=0? 21 GTO 24 22 CLX 23 GTO 00 24 RCL 2 25 RCL 3 26 RCL 1 27 1 28 GTO 00

5 [ENTER] 625 [R/S]  =>  1    [R↓] 5 [R↓] 4 [R↓] 625
5 [ENTER] 600 [R/S]  =>  0
5 [ENTER] 5 [R/S]  =>  1   [R↓] 5 [R↓] 1 [R↓] 5

Dieter
09-09-2018, 04:24 PM (This post was last modified: 09-09-2018 05:11 PM by Dieter.)
Post: #18
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: (12C) Check if two numbers is to the power of other
(09-06-2018 07:07 PM)stored Wrote:  Perhaps it is more practical to use logarithmic formula:
...
But the problem of this approach is a big rounding errors in some cases, so it is necessary to make a direct verification.

Since the 12CP emulator has roundoff errors anyway, maybe the following approach will work, both on an original 12C and the mis-behaving emulator. The idea is: round the result of ln(n2)/ln(n1) to the nearest integer, then calculate n1^this and check if the result matches n2. As far as I got it, even the 12CP emulator returns powers correctly when rounded to 10 digits. So this should work:

Code:
01 f 9 02 X≤Y? 03 X<>Y 04 RND 05 STO 2 06 LN 07 X<>Y 08 RND 09 STO 1 10 LN 11 ÷ 12 , 13 5 14 + 15 INTG 16 STO 3 17 RCL 1 18 RCL 3 19 Y^X 20 RND 21 RCL 2 22 - 23 X=0? 24 GTO 27 25 CLX 26 GTO 00 27 RCL 2 28 RCL 3 29 RCL 1 30 1 31 GTO 00

Maybe someone can try this.

The program sets and exits with FIX 9 mode. If you want to reset this to FIX 2 (or anything else) insert the respective command after the RND in line 20 and before the X=0? test that follows.

EDIT:
Here's an even better version. It returns additional information even if n2 is not an integer power of n1. The stack then holds the closest approximation. Try it and see what you get, e.g. for 5 and 600.

Code:
01 f 9 02 X≤Y? 03 X<>Y 04 RND 05 STO 2 06 LN 07 X<>Y 08 RND 09 STO 1 10 LN 11 ÷ 12 , 13 5 14 + 15 INTG 16 STO 3 17 RCL 2 18 RCL 1 19 RCL 3 20 Y^X 21 RND 22 - 23 X=0? 24 GTO 27 25 0 26 GTO 28 27 1 28 LstX 29 RCL 3 30 RCL 1 31 R↓ 32 R↓ 33 R↓ 34 f 0 35 GTO 00

5 [ENTER] 600 [R/S] => 0 [R↓] 5 [R↓] 4 [R↓] 625
2 [ENTER] 512 [R/S] => 1 [R↓] 2 [R↓] 9 [R↓] 512

Dieter
 « Next Oldest | Next Newest »

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