Pathological floating point problems: Difference between revisions

Content deleted Content added
Dinosaur (talk | contribs)
Added Algol 68
Line 154:
18 100.000
... 100.000
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
In Algol 68G, we can specify the precision of LONG LONG REAL values
<lang algol68>BEGIN
# task 1 #
BEGIN
PR precision 32 PR
print( ( " 32 digit REAL numbers", newline ) );
[ 1 : 100 ]LONG LONG REAL v;
v[ 1 ] := 2;
v[ 2 ] := -4;
FOR n FROM 3 TO UPB v DO v[ n ] := 111 - ( 1130 / v[ n - 1 ] ) + ( 3000 / ( v[ n - 1 ] * v[ n - 2 ] ) ) OD;
FOR n FROM 3 TO 8 DO print( ( "n = ", whole( n, 3 ), " ", fixed( v[ n ], -22, 16 ), newline ) ) OD;
FOR n FROM 20 BY 10 TO 50 DO print( ( "n = ", whole( n, 3 ), " ", fixed( v[ n ], -22, 16 ), newline ) ) OD;
print( ( "n = 100 ", fixed( v[ 100 ], -22, 16 ), newline ) )
END;
BEGIN
PR precision 120 PR
print( ( "120 digit REAL numbers", newline ) );
[ 1 : 100 ]LONG LONG REAL v;
v[ 1 ] := 2;
v[ 2 ] := -4;
FOR n FROM 3 TO UPB v DO v[ n ] := 111 - ( 1130 / v[ n - 1 ] ) + ( 3000 / ( v[ n - 1 ] * v[ n - 2 ] ) ) OD;
print( ( "n = 100 ", fixed( v[ 100 ], -22, 16 ), newline ) )
END;
print( ( newline ) );
# task 2 #
BEGIN
print( ( "single precision REAL numbers...", newline ) );
REAL chaotic balance := exp( 1 ) - 1;
print( ( "initial chaotic balance: ", fixed( chaotic balance, -22, 16 ), newline ) );
FOR i FROM 1 TO 25 DO ( chaotic balance *:= i ) -:= 1 OD;
print( ( "25 year chaotic balance: ", fixed( chaotic balance, -22, 16 ), newline ) )
END;
BEGIN
print( ( "double precision REAL numbers...", newline ) );
LONG REAL chaotic balance := long exp( 1 ) - 1;
print( ( "initial chaotic balance: ", fixed( chaotic balance, -22, 16 ), newline ) );
FOR i FROM 1 TO 25 DO ( chaotic balance *:= i ) -:= 1 OD;
print( ( "25 year chaotic balance: ", fixed( chaotic balance, -22, 16 ), newline ) )
END;
BEGIN
PR precision 32 PR
print( ( " 32 digit REAL numbers...", newline ) );
LONG LONG REAL chaotic balance := long long exp( 1 ) - 1;
print( ( "initial chaotic balance: ", fixed( chaotic balance, -22, 16 ), newline ) );
FOR i FROM 1 TO 25 DO ( chaotic balance *:= i ) -:= 1 OD;
print( ( "25 year chaotic balance: ", fixed( chaotic balance, -22, 16 ), newline ) )
END
END</lang>
{{out}}
<pre>
32 digit REAL numbers
n = +3 18.5000000000000000
n = +4 9.3783783783783784
n = +5 7.8011527377521614
n = +6 7.1544144809752494
n = +7 6.8067847369236330
n = +8 6.5926327687044384
n = +20 6.0435521101892689
n = +30 6.0067860930262429
n = +40 -2.9367486132065552
n = +50 100.0000000006552004
n = 100 100.0000000000000000
120 digit REAL numbers
n = 100 100.0000000000000000
 
single precision REAL numbers...
initial chaotic balance: 1.7182818284590400
25 year chaotic balance: -2242373258.5701500000
double precision REAL numbers...
initial chaotic balance: 1.7182818284590452
25 year chaotic balance: 0.0406729916134442
32 digit REAL numbers...
initial chaotic balance: 1.7182818284590452
25 year chaotic balance: 0.0399387296732302
</pre>