Pathological floating point problems: Difference between revisions
m
→{{header|EasyLang}}
(Added XPL0 example.) |
|||
(5 intermediate revisions by 4 users not shown) | |||
Line 1,486:
Bank balance after 25 years: $0,04
Rump f(77617, 33096): -0,827396059946821</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
numfmt 10 0
proc task1 . .
print "--- Task 1 pathologic ---"
vpp = 2
vp = -4
for i = 3 to 100
v = 111 - 1130 / vp + 3000 / (vp * vpp)
if i <= 8 or i = 20 or i = 30 or i = 50 or i = 100
print i & ": " & v
.
vpp = vp
vp = v
.
.
task1
print ""
#
proc task2 . .
print "--- Task 2 pathologic ---"
e = 2.718281828459045
bal = e - 1
for i = 1 to 25
bal = bal * i - 1
.
print "Balance after 25 years: $" & bal
.
task2
print ""
#
proc mul f . bal bal$[] .
for i = len bal$[] downto 1
dig = number bal$[i]
h = dig * f + c
bal$[i] = h mod 10
c = h div 10
.
bal += c
.
proc task2ok . .
print "--- Task 2 OK ---"
e$ = "2.7182818284590452353602874713526624977572470"
bal = number substr e$ 1 1
bal$[] = strchars substr e$ 3 999
bal -= 1
for i = 1 to 25
mul i bal bal$[]
bal -= 1
.
print "Balance after 25 years: $" & bal & "." & substr strjoin bal$[] 1 16
.
task2ok
</syntaxhighlight>
{{out}}
<pre>
--- Task 1 pathologic ---
3: 18.5000000000
4: 9.3783783784
5: 7.8011527378
6: 7.1544144810
7: 6.8067847369
8: 6.5926327687
20: 98.3495031222
30: 100.0000000000
50: 100
100: 100
--- Task 2 pathologic ---
Balance after 25 years: $-2242373258.5701580048
--- Task 2 OK ---
Balance after 25 years: $0.0399387296732302
</pre>
=={{header|Excel}}==
Line 1,980 ⟶ 2,055:
=={{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,879:
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,593:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./fmt" for Fmt
var LIMIT = 100
|