Pathological floating point problems: Difference between revisions
Pathological floating point problems (view source)
Revision as of 11:16, 24 January 2024
, 4 months ago→{{header|Wren}}: Minor tidy
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Wren}}: Minor tidy) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1,980:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Pathological_floating_point_problems}}
'''Solution'''
'''Case 1. Introduction example'''
[[File:Fōrmulæ - Pathological floating point problems 01.png]]
[[File:Fōrmulæ - Pathological floating point problems 02.png]]
'''Case 2. A sequence that seems to converge to a wrong limit'''
[[File:Fōrmulæ - Pathological floating point problems 03.png]]
[[File:Fōrmulæ - Pathological floating point problems 04.png]]
'''Case 3. The Chaotic Bank Society'''
[[File:Fōrmulæ - Pathological floating point problems 05.png]]
[[File:Fōrmulæ - Pathological floating point problems 06.png]]
'''Case 4. Siegfried Rump's example'''
[[File:Fōrmulæ - Pathological floating point problems 07.png]]
[[File:Fōrmulæ - Pathological floating point problems 08.png]]
=={{header|Go}}==
Line 2,782 ⟶ 2,804:
f(77617.0, 33096.0) is -0.8273960599468214
</pre>
=={{header|M2000 Interpreter}}==
From n=26 we get wrong numbers (not shown here). For Task 2 only Decimal can show a good result, although has less accuracy.
<syntaxhighlight lang="m2000 interpreter">
module Pathological_floating_point_problems{
decimal vn[3]
vn[1]=2
vn[2]=-4
for i=3 to 100
vn[i]=111@-1130@/vn[i-1]+3000@/(vn[i-1]*vn[i-2])
next
n=list:=3,4,5,6,7,8,20,25
k=each(n)
while k
report "n = "+eval$(k)+chr$(9)+(vn[eval(k)])
end while
}
print "Task 1"
Pathological_floating_point_problems
print
print "Task 2"
module Chaotic_Bank_Society {
decimal Balance=2.7182818284590452353602874713@-1@
string frmt="year {0::-2} Balance:{1}"
for i=1 to 25
Balance=Balance*i-1@
rem print format$(frmt, i, Balance)
next i
Print "Starting balance: $e-1"
Print "Balance = (Balance * year) - 1 for 25 years"
print "Balance after 25 years: $"+Balance
}
Chaotic_Bank_Society
</syntaxhighlight>
{{out}}
<pre>
Task 1
n = 3 18.5
n = 4 9.378378378378378378378378378
n = 5 7.80115273775216138328530259
n = 6 7.154414480975249353527890606
n = 7 6.806784736923632983941755925
n = 8 6.592632768704438392741992887
n = 20 6.04355210719488789087813234
n = 25 6.01330401514055310928530167
Task 2
Starting balance: $e-1
Balance = (Balance * year) - 1 for 25 years
Balance after 25 years: $0.0391218706091111022592
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 4,442 ⟶ 4,518:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./fmt" for Fmt
var LIMIT = 100
Line 4,582 ⟶ 4,658:
f(77617.0, 33096.0) is -0.8273960599468214
</pre>
=={{header|XPL0}}==
This shows the results from the IEEE 754 double precision (64-bit) FPU
built into the Raspberry Pi 4. Identical results were obtained from
EXPL-32 on an Intel Inspiron. A Duron 850 gave identical results for the
first task, but the second task diverged to positive values after 17
years. The Duron also gave a large positive value for the third task.
<syntaxhighlight lang "XPL0">func real F(A, B);
real A, B;
return 333.75*Pow(B,6.) +
A*A*(11.*A*A*B*B - Pow(B,6.) - 121.*Pow(B,4.) - 2.) +
5.5*Pow(B,8.) + A/(2.*B);
real V1, V2, V3, Bal;
int N, Year;
[V1:= 2.; V2:= -4.; \task 1
for N:= 3 to 100 do
[V3:= 111. - 1130./V2 + 3000./(V1*V2);
case N of
3,4,5,6,7,8,20,30,50,100:
[Format(3, 0); RlOut(0, float(N));
Format(5, 16); RlOut(0, V3); CrLf(0);
]
other [];
V1:= V2; V2:= V3;
];
CrLf(0); \task 2
Bal:= Exp(1.) - 1.;
for Year:= 1 to 25 do
[Bal:= Bal*float(Year) - 1.;
Format(2, 0); RlOut(0, float(Year));
Format(12, 16); RlOut(0, Bal); CrLf(0);
];
CrLf(0); \task 3
RlOut(0, F(77617., 33096.));
CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
3 18.5000000000000000
4 9.3783783783783800
5 7.8011527377521700
6 7.1544144809753300
7 6.8067847369248100
8 6.5926327687217900
20 98.3495031221654000
30 99.9999999999989000
50 100.0000000000000000
100 100.0000000000000000
1 0.7182818284590450
2 0.4365636569180900
3 0.3096909707542710
4 0.2387638830170820
5 0.1938194150854110
6 0.1629164905124650
7 0.1404154335872580
8 0.1233234686980610
9 0.1099112182825480
10 0.0991121828254790
11 0.0902340110802700
12 0.0828081329632370
13 0.0765057285220790
14 0.0710801993091080
15 0.0662029896366220
16 0.0592478341859530
17 0.0072131811612050
18 -0.8701627390983050
19 -17.5330920428678000
20 -351.6618408573560000
21 -7385.8986580044700000
22 -162490.7704760980000000
23 -3737288.7209502600000000
24 -89694930.3028063000000000
25 -2242373258.5701600000000000
-1180591620717410000000.0000000000000000
</pre>
|