Post Reply 
(12C) Check if two numbers is to the power of other
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:
...
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (12C) Check if two numbers is to the power of other - Dieter - 09-09-2018 04:24 PM



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