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

Seite - 45 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 45 -

Bild der Seite - 45 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 45 -

2.2 Variables,ObjectsandExpressions 45 2.2.7 Division—QuotientandRemainder It is sometimes desirable to pick out the quotient and remainder of a division a b , for integer or floating point numbersa and b. In Python, we can do this with the operators// (integer division)6 and% (modulo), respectively. They have the same precedenceas*, and theiruse maybe exemplifiedby In [1]: 11//2 Out[1]: 5 In [2]: 11%2 Out[2]: 1 Note that thesignof the remainder follows thesignof thedenominator, i.e., In [1]: -11%2 Out[1]: 1 In [2]: 11%-2 Out[2]: -1 2.2.8 UsingParentheses Notethatparenthesesareoftenveryimportanttogrouppartsofexpressionstogether in the intended way. Let us consider some variablexwith the value 4 and assume that you want to divide 1.0 byx + 1. We know the answer is 0.2, but the way we present the task toPython is critical, as shownby the followingexample. In [1]: x = 4 In [2]: 1.0/x+1 Out[2]: 1.25 In [3]: 1.0/(x+1) Out[3]: 0.2 In the first try,wesee that 1.0 isdividedbyx (i.e., 4), giving0.25,which is then added to 1. Python did not understand that our complete denominator wasx+1. In our second try, we used parentheses to “group” the denominator, and we got what wewanted. 2.2.9 Round-OffErrors Since most numbers can be represented only approximately on the computer, this gives rise to what is called roundingerrors.We may illustrate this if we take a look 6 Note that in Python 2.x, the operator/gives integer division, unless either the numerator and/or thedenominator is afloat.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
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