(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
|
|||
|
|||
(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 Rv // X^Y Answer 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:
Gamo |
|||
09-06-2018, 07:07 PM
(This post was last modified: 09-06-2018 07:08 PM by stored.)
Post: #2
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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'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
|
|||
|
|||
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 Correct answer in registers 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
|
|||
|
|||
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. 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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 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
|
|||
|
|||
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: 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
|
|||
|
|||
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
|
|||
|
|||
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 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
|
|||
|
|||
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:
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
|
|||
|
|||
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 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
|
|||
|
|||
RE: (12C) Check if two numbers is to the power of other | |||
09-08-2018, 04:44 PM
Post: #17
|
|||
|
|||
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? 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
|
|||
|
|||
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: 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 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 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: 5 Guest(s)