Post Reply 
Casio fx-3800P "Programming"
03-10-2020, 11:02 PM
Post: #15
RE: Casio fx-3800P "Programming"
(03-10-2020 01:33 PM)Csaba Tizedes Wrote:  Today evening (CET) I will go back with a little program for my 50F, which can be run on any similar unit.

Estimating real numbers with fractions
======================================

Let K is a (real) number and estimate it with the fraction A/B, where A and B are relative primes and ABS(A/B-K)<=E, where E is a small number. The algorithm is a simple brute-force-counting algorithm: Let A=0 and B=1. If ABS(A/B-K)<=E, the program stops. If A/B-K<0 then A=A+1, else B=B+1.


The above in BASIC:
-------------------
10 D=A/B-K
20 IF E>=ABS(D) THEN PRINT A,B:END
30 IF D<0 THEN A=A+1:GOTO 10
40 B=B+1:GOTO10


On blind programmable CASIOs:
-----------------------------

Code:
P1:
-----------
01:  Kout4
     Min
     Kout5
     M-      // M=K-E
-----------
05:  1
     Kin+1   // A++
-----------
07:  Kout1
     ÷
     Kout2
     =
     Kin3    // let H=A/B
     x<=M    // H<K-E ?
-----------
13:  1
     Kin-1   // A--
     Kin+2   // B++
-----------
16:  Kout5
     M+
     M+      // let M=K+E
     Kout3
     -
     MR
     =
     x>0     // H>K+E ?
-----------
24:  1
     Kin+1
26:  Kin-2   // end of the program
-----------
     alphaA  // only for display the numerator and
     alphaB  // denominator
-----------

Variables:
----------
K1: A (numerator)
K2: B (denominator)
K3: H (=A/B)
K4: K (the number)
K5: E (eps = absolute error of estimation; can be calculated as percent of K for relative error)
M: for decisions


For type in: (let K=PI)
------------
1 Kin1 Kin2
PI Kin4 ×0.001 = Kin5


For running:
------------
0 Kin1 1 Kin2 P1


The results:
------------
Tested on CASIO fx-50F
Code:

+--------------+-----+-----+----------------------+---------------------------+-------------+
|   E(rel) (%) |   A |   B |   Running time (sec) |   Number of steps (A+B-1) |   Steps/sec |
+--------------+-----+-----+----------------------+---------------------------+-------------+
|      1       |  19 |   6 |                 12.9 |                        24 |        1.86 |
|      0.1     |  22 |   7 |                 14.9 |                        28 |        1.88 |
|      0.01    | 289 |  92 |                189.6 |                       380 |        2.00 |
|      0.001   | 355 | 113 |                232.2 |                       467 |        2.01 |
+--------------+-----+-----+----------------------+---------------------------+-------------+

Short explanation:
------------------
In 05~12 steps the A is increased until H>K-E.
If we are in K+/-E "good" range (tested in step 23) the program ends...
...or A is too high and H>K+E, in this case we must to increase the B.
We can increase B in the following way: first decrease A in step 14 because in steps 05~12 the A will be increased during the next cycle and the original A will be present and increase B in step 15.
Important: the H is not changed when A and B changed, because H is required in step 23, but when the program finished, must to correct A and B in step 24~26.


nJoy! Big Grin

Csaba
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Casio fx-3800P "Programming" - Csaba Tizedes - 03-10-2020 11:02 PM
RE: Casio fx-3800P "Programming" - xerxes - 03-11-2020, 12:06 AM



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