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

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

Image of the Page - 45 -

Image of the Page - 45 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
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