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

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

Image of the Page - 15 -

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

Text of the Page - 15 -

1.5 MoreBasicConcepts 15 In [1]: 1/4 Out [1]: 0 In [2]: 1.0/4 Out [2]: 0.25 Wesee two alternativeways ofwriting it, but only the last way ofwriting it gave thecorrect (i.e., expected) result!Why? WithPythonversion2, thefirst alternativegiveswhat is called integer division, i.e., all decimals in the answer are disregarded, so the result is rounded down to the nearest integer. To avoid it, we may introduce an explicit decimal point in either thenumerator, thedenominator, or in both. If youarenew toprogramming, this is certainly strangebehavior. However, youwill find the same feature inmany programminglanguages,notonlyPython,butactuallyall languageswithsignificant inheritance fromC. If your numerator or denominator is a variable, say you have 1/x, youmaywrite1/float(x) tobeonsafegrounds. Python version 3 implementsmathematical real number division by / and re- quires theoperator// for integerdivision(// is alsoavailable inPythonversion2). Although Python version 3 eliminates the problemswith unintended integer divi- sion, it is important toknowabout integer divisionwhendoingcomputing inmost other languages. 1.5.5 FormattingTextandNumbers Resultsfromscientificcomputationsareoftentobereportedasamixtureoftextand numbers.Usually,wewant tocontrolhownumbersareformatted. Forexample,we maywanttowrite1/3as0.33or3.3333e-01(3:3333 10 1). Theprintcommand is thekeytool towriteout textandnumberswith fullcontrolof theformatting. The firstargumenttoprint isastringwithaparticularsyntax tospecify theformatting, the so-calledprintf syntax. (Thepeculiar namestems from theprintf function in theprogramminglanguageCwhere the syntaxwasfirst introduced.) Suppose we have a real number 12.89643, an integer 42, and a text ’some message’ thatwewant towriteout in the following twoalternativeways: real=12.896, integer=42, string=some message real=1.290e+01, integer= 42, string=some message Therealnumberisfirstwritten indecimalnotationwith threedecimals, as12.896, but afterwards in scientific notation as 1.290e+01. The integer is first written as compactly as possible, while on the second line, 42 is formatted in a text field of widthequal tofivecharacters. Thefollowingprogram,formatted_print.py,appliestheprintfsyntaxtocon- trol the formattingdisplayedabove:
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