How to resolve the algorithm Pathological floating point problems step by step in the REXX programming language
How to resolve the algorithm Pathological floating point problems step by step in the REXX programming language
Table of Contents
Problem Statement
Most programmers are familiar with the inexactness of floating point calculations in a binary processor. The classic example being: In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding. There are pathological problems however, where seemingly simple, straight-forward calculations are extremely sensitive to even tiny amounts of imprecision. This task's purpose is to show how your language deals with such classes of problems.
A sequence that seems to converge to a wrong limit. Consider the sequence:
As n grows larger, the series should converge to 6 but small amounts of error will cause it to approach 100.
Display the values of the sequence where n = 3, 4, 5, 6, 7, 8, 20, 30, 50 & 100 to at least 16 decimal places.
The Chaotic Bank Society is offering a new investment account to their customers. You first deposit $e - 1 where e is 2.7182818... the base of natural logarithms. After each year, your account balance will be multiplied by the number of years that have passed, and $1 in service charges will be removed. So ...
What will your balance be after 25 years?
Siegfried Rump's example. Consider the following function, designed by Siegfried Rump in 1988.
Demonstrate how to solve at least one of the first two problems, or both, and the third if you're feeling particularly jaunty.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pathological floating point problems step by step in the REXX programming language
Source code in the rexx programming language
/*REXX pgm (pathological FP problem): a sequence that might converge to a wrong limit. */
parse arg digs show . /*obtain optional arguments from the CL*/
if digs=='' | digs=="," then digs= 150 /*Not specified? Then use the default.*/
if show=='' | show=="," then show= 20 /* " " " " " " */
numeric digits digs /*have REXX use "digs" decimal digits. */
#= 2 4 5 6 7 8 9 20 30 50 100 /*the indices to display value of V.n */
fin= word(#, words(#) ) /*find the last (largest) index number.*/
w= length(fin) /* " " length (in dec digs) of FIN.*/
v.1= 2 /*the value of the first V element. */
v.2=-4 /* " " " " second " " */
do n=3 to fin; nm1= n-1; nm2= n-2 /*compute some values of the V elements*/
v.n= 111 - 1130/v.nm1 + 3000/(v.nm1*v.nm2) /* " a value of a " element.*/
/*display digs past the dec. point───┐ */
if wordpos(n, #)\==0 then say 'v.'left(n, w) "=" format(v.n, , show)
end /*n*/ /*stick a fork in it, we're all done. */
/*REXX pgm (pathological FP problem): the chaotic bank society offering a new investment*/
e=2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713,
||8217852516642742746639193200305992181741359662904357290033429526059563073813232862794,
||3490763233829880753195251019011573834187930702154089149934884167509244761460668082264,
||8001684774118537423454424371075390777449920695517027618386062613313845830007520449338
d = length(e) - length(.) /*subtract one for the decimal point. */
parse arg digs show y . /*obtain optional arguments from the CL*/
if digs=='' | digs=="," then digs= d /*Not specified? Then use the default.*/
if show=='' | show=="," then show= 20 /* " " " " " " */
if y=='' | y=="," then y= 25 /* " " " " " " */
numeric digits digs /*have REXX use "digs" decimal digits. */
$= e - 1 /*subtract $1 from e, that's da deposit*/
/* [↑] value of newly opened account. */
do n=1 for y /*compute the value of the account/year*/
$= $*n - 1 /* " " " " " account now.*/
end /*n*/
@@@= 'With ' d " decimal digits, the balance after " y ' years is: '
say @@@ '$'format($, , show) / 1 /*stick a fork in it, we're all done. */
/*REXX pgm (pathological FP problem): the Siegfried Rump's example (problem dated 1988).*/
parse arg digs show . /*obtain optional arguments from the CL*/
if digs=='' | digs=="," then digs=150 /*Not specified? Then use the default.*/
if show=='' | show=="," then show= 20 /* " " " " " " */
numeric digits digs /*have REXX use "digs" decimal digits. */
a= 77617.0 /*initialize A to it's defined value.*/
b= 33096.0 /* " B " " " " */
/*display SHOW digits past the dec. pt.*/
say 'f(a,b)=' format( f(a,b), , show) /*display result from the F function.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
f: procedure; parse arg a,b; return 333.75* b**6 + a**2 * (11* a**2* b**2 - b**6,
- 121*b**4 - 2) + 5.5*b**8 + a / (2*b)
You may also check:How to resolve the algorithm Same fringe step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Logical operations step by step in the SkookumScript programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the 11l programming language
You may also check:How to resolve the algorithm Elliptic curve arithmetic step by step in the Java programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the Frink programming language