Seite - 71 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 71 -
Text der Seite - 71 -
3.4 Testing 71
Theseare twoequations for twounknownsC and r.Wecaneasily eliminateC by
dividing theequationsbyeachother. Thensolving forr gives
ri 1 D ln.Ei=Ei 1/
ln.ni=ni 1/ : (3.24)
We have introduced a subscript i 1 in r since the estimated value for r varies
with i. Hopefully, ri 1 approaches the correct convergence rate as the number of
intervals increasesand i !q.
3.4.3 FinitePrecisionofFloating-PointNumbers
The test procedures above lead to comparisonof numbers for checking that calcu-
lationswere correct. Suchcomparison ismorecomplicated thanwhat anewcomer
might think. Supposewehaveacalculationa + bandwant tocheckthat theresult
iswhatweexpect.Westartwith1+2:
>>> a = 1; b = 2; expected = 3
>>> a + b == expected
True
Thenweproceedwith0.1+0.2:
>>> a = 0.1; b = 0.2; expected = 0.3
>>> a + b == expected
False
Sowhyis0:1C0:2¤0:3?Thereason is that realnumberscannot ingeneralbe
exactly representedon a computer. Theymust insteadbe approximatedbyafloat-
ing-pointnumber3 that canonlystoreafinite amountof information,usuallyabout
17digitsofa realnumber. Letusprint0.1,0.2,0.1+0.2,and0.3with17decimals:
>>> print ’%.17f\n%.17f\n%.17f\n%.17f’ % (0.1, 0.2, 0.1 + 0.2, 0.3)
0.10000000000000001
0.20000000000000001
0.30000000000000004
0.29999999999999999
Wesee that all of the numbers have an inaccurate digit in the 17th decimal place.
Because 0.1C 0.2 evaluates to 0.30000000000000004 and 0.3 is represented as
0.29999999999999999, these twonumbersarenot equal. Ingeneral, real numbers
inPythonhave (atmost)16correctdecimals.
When we compute with real numbers, these numbers are inaccurately repre-
sented on the computer, and arithmetic operations with inaccurate numbers lead
to small rounding errors in the final results. Depending on the type of numerical
algorithm, the roundingerrorsmayormaynotaccumulate.
3https://en.wikipedia.org/wiki/Floating_point
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik