Trabb Pardo–Knuth algorithm: Difference between revisions

Line 289:
2.0 : 41.41421356237309
1.0 : 6.0</pre>
 
=={{header|ASIC}}==
{{works with|ASIC|5.0}} Compile with the option ''Decimal Math'' (<code>DEC</code> in command line).
Notes:
* ASIC does not have the fuction that calculates a square root. Thus, the program uses the added subprogram <code>CALCSQRT:</code>.
* ASIC does not scroll a text screen, but it wraps the text. Thus, the program clears the screen after the user enters numbers.
<lang>
REM Trabb Pardo-Knuth algorithm
REM Used "magic numbers" because of strict specification of the algorithm.
DIM S@(10)
PRINT "Enter 11 numbers."
FOR I = 0 TO 10
I1= I + 1
PRINT I1;
PRINT " => ";
INPUT TMP@
S@(I) = TMP@
NEXT I
PRINT
REM Reverse
HALFIMAX = 10 / 2
FOR I = 0 TO HALFIMAX
IREV = 10 - I
TMP@ = S@(I)
S@(I) = S@(IREV)
S@(IREV) = TMP@
NEXT I
REM Results
REM Leading spaces in printed numbers are removed
CLS
FOR I = 0 TO 10
PRINT "f(";
STMP$ = STR$(S@(I))
STMP$ = LTRIM$(STMP$)
PRINT STMP$;
PRINT ") = ";
N@ = S@(I)
GOSUB CALCF:
R@ = F@
IF R@ > 400 THEN
PRINT "overflow"
ELSE
STMP$ = STR$(R@)
STMP$ = LTRIM$(STMP$)
PRINT STMP$
ENDIF
NEXT I
END
 
CALCF:
REM Calculates f(N@)
REM Result in F@
X@ = ABS(N@)
GOSUB CALCSQRT:
F@ = 5.0 * N@
F@ = F@ * N@
F@ = F@ * N@
F@ = F@ + SQRT@
RETURN
 
CALCSQRT:
REM Calculates approximate (+- 0.00001) square root of X@ for X@ >= 0 (bisection method)
REM Result in SQRT@
A@ = 0.0
IF X@ >= 1.0 THEN
B@ = X@
ELSE
B@ = 1.0
ENDIF
L@ = B@ - A@
L@ = ABS(L@)
WHILE L@ > 0.00001
MIDDLE@ = A@ + B@
MIDDLE@ = MIDDLE@ / 2
MIDDLETO2@ = MIDDLE@ * MIDDLE@
IF MIDDLETO2@ < X@ THEN
A@ = MIDDLE@
ELSE
B@ = MIDDLE@
ENDIF
L@ = B@ - A@
L@ = ABS(L@)
WEND
SQRT@ = MIDDLE@
RETURN
</lang>
{{out}}
Enter the data.
<pre>
Enter 11 numbers.
1 => ?-5
2 => ?-3
3 => ?-2
4 => ?-1
5 => ?0
6 => ?1
7 => ?2
8 => ?3
9 => ?4
10 => ?5
11 => ?6
</pre>
Results.
<pre>
f(6.00000) = overflow
f(5.00000) = overflow
f(4.00000) = 321.99999
f(3.00000) = 136.73206
f(2.00000) = 41.41422
f(1.00000) = 5.99999
f(0.00000) = 0.00001
f(-1.00000) = -4.00001
f(-2.00000) = -38.58578
f(-3.00000) = -133.26794
f(-5.00000) = -622.76393
</pre>
 
=={{header|AutoIt}}==
Anonymous user