Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 71 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 71 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 71 -

Image of the Page - 71 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python