Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Seite - 15 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 15 -

Bild der Seite - 15 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 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:
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
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
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python