what is the order of mathematical calculations in python? [duplicate] - python

This question already has answers here:
How do order of operations go on Python?
(5 answers)
Closed 2 years ago.
I tried to calculate the output of this problem in python: 4+6/2 and it was 7 then i reversed them as 6+4/2 and answer was 8. now whats the difference, and why this occurs? what is divided first?

Python uses PEMDAS, as do most languages. Division comes before addition, so it would be calculated as 4+(6/2)=4+3=7, and 6+(4/2)=8. This can be confirmed with any calculator.
PEMDAS is the standard order of operations:
P- Parentheses first
E- Exponents second
M/D- Multiplication or Division third (If there are multiple
multiplication or division signs in a row, then operate first to last)
A/S- Addition or Subtraction fourth (If there are multiple addition or
subtraction signs in a row, then operate first to last)

Related

Sum() returns bad value when used in a list of numbers with many decimals [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 4 months ago.
a = [0.0021, 0.0087]
s = sum(a)
print(s)
Outcome: 0.010799999999999999
When executing the program above, the result is complex and eronated.
After performing multiple tests, including:
a = 0.0021
b = 0.0087
The result is the same. I tried different combinations of numbers and it seems that only these 2 have such an odd outcome.
I would say that this is floating point arithmetics error. Or I think there were some performance improvements done to math operations in Python which cause this, you can look more into it if you want by searching for PyNumber_Add and BINARY_ADD operation

Issue with euclidean division in Python [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 12 months ago.
First of all, sorry if this has been already answered, I have been searching for a while and did not find anything.
In short, I'm trying to create a programe to automate some tasks I have. In one, I have a loop on days, between a start date and an end date. For each iteration, I'm trying to get the lag in months between the current date and the start date. So I tried to use the euclidean division, which seemed appropriate.
Bottom line is, it works for almost all dates except a few, for instance:
import datetime as dt
start=dt.date(2022, 7, 1)
end=dt.date(2023, 7, 1)
average_days=365/12
lag1=(end-start).days/average_days
lag2=(end-start).days//average_days
For these dates in particular, lag1 gives me 12 while lag2 gives me 11. Where does this difference come from exactly, and how can I work around this issue to continue using the euclidean division ?
Thank you
This is answered here:
Dividing a Number by a Decimal Using the Integer Division Operator
In short, 365/12 is a float and is not exact after a certain number of digits. And could be a tiny bit larger than what you would expect. So when you divide 365 by this number it will be 11.99999... And // is floor division which truncates the decimals, resulting in 11.

How does Python3 compare floats when using `set`? [duplicate]

This question already has answers here:
Set of floating point numbers
(3 answers)
What is the best way to compare floats for almost-equality in Python?
(18 answers)
Checking for NaN presence in a container
(2 answers)
Closed 1 year ago.
The set function in Python relies on the existence of a robust equality operator existing on the elements given to set.
Suppose we had a list of floating point numbers, xs, and we called set(xs). How will set compare the elements of xs? Will it check to see if two elements are within machine epsilon, or will it perform bitwise equality?

How to run calculations [duplicate]

This question already has answers here:
How do I do exponentiation in python? [duplicate]
(3 answers)
Closed 2 years ago.
I am new to programming and just started a course and stuck on a demo section where I have to take a math calculation and program it in Python. When I program it do you follow BEDMAS order of figuring out or is there a logical order. The syntax is correct but I keep being told it is not right
See the equation below to be programmed into python. What are the rules for telling Python to calculate something correctly where 5 is the exponent
Use Python to calculate (((1+2)∗3)/4)5
Exponentiation in Python is done with the ** operator.
For instance:
2**8
is 256.
In Python to calculate exponent you use ** operator.
Similar to multiplication but double asterisk.
(((1+2)∗3)/4)**5
This will give you 32.
That is because you are using values which are all integers, so 9 / 4 will be 2, not 2.25.
Python has operator precedence that follows algebraic rules.

Python issue with an arithmetic operation [duplicate]

This question already has answers here:
Why does Python return 0 for simple division calculation?
(6 answers)
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 4 years ago.
I have a line of Python code that doesn't work the way it should (at least to the best of my knowledge). Following is the code line:
print 'progress: {}%'.format((i/len(e_numbers))*100)
The value of i starts from 0 and goes up to length of e_numbers which is a list, while the length of e_numbers is around 17000. But the code always prints Progress: 0%.
Any idea why?
In Python 2, using / to divide two integers performs integer division by default, rounding the result down to the nearest integer value. Thus, as long as i is between 0 and len(e_numbers), i/len(e_numbers) is going to be 0. There are two possible solutions:
Cast one or both operands to a float before dividing, e.g., float(i)/len(e_numbers)
Put from __future__ import division at the top of your file so that / always produces a float.

Categories