This question already has answers here:
Calculation error with pow operator
(4 answers)
Closed 6 years ago.
from math import pow
assert pow(-3, 2) == 9
assert -3 ** 2 == -9
why are the two above assertions valid?
in regular math, when a negative numbered is powered to 2, it becomes positive. which one of these is equal to the regular math I know?
is ignoring negative value the only difference between these two methods?
Its because of the order in which the operations are performed. In the first case, pow(-3,2) takes as inputs a -3 as first input and a 2 as the second input. In the second case, the ** has precedence over the -, so the order in which the operations are executed is
Calculate 3**2
Change the sign of the result
This leads to the result being -9.
Because python calculate the minus after it calculate the power.
In [2]: -3**2
Out[2]: -9
In [3]: (-3)**2
Out[3]: 9
Related
This question already has answers here:
The tilde operator in Python
(9 answers)
Closed last year.
I was learning about bitwise operators and I learnt that complement of 0 is 1 and 1 is 0. But when I tried using ~0 on IDLE, it printed -1 and when i typed ~1 it gave -2..
-1 is 0-1 => 00..00-00...01 = 1..11
So, as long as you consider some finite width (width is the size of integer or binary form you are using,4,8 ....),it is true that:
00..00 =~11..11
then, the following also be true:
~0=-1
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 5 years ago.
I have the following program
def F_inf(a,b):
x1=a.numerator/a.denominator
x2=b.numerator/b.denominator
if x1<x2:
print "a<b"
elif x1>x2:
print "a>b"
else: print "a=b"
a=Fraction(10,4)
b=Fraction(10,4)
F_inf(a, b)
When I execute it,x1 receive just the integer value of the fraction, for exemple if I have to compute 2/4 x1 is equal to 0 not 0.5.
What should I do ?
Thanks
It sounds like you're using Python2. The best solution would be to switch to Python 3 (not just because of the division but because "Python 2.x is legacy, Python 3.x is the present and future of the language").
Other than that you have a couple of choices.
from __future__ import division
# include ^ as the first line in your file to use float division by default
or
a = 1
b = 2
c = a / (1.0*b) # multiplying by 1.0 forces the right side of the division to be a float
#c == 0.5 here
This question already has answers here:
The tilde operator in Python
(9 answers)
Closed last month.
Is there a function that takes a number with binary numeral a, and does the NOT?
(For example, the function's value at 18 [binary 10010] would be 13
[binary 01101].) I thought this was what the tilde operator (~) did, but it only adds a minus sign to 18, which is two's complement of that, instead of getting 13.
As mentioned in the comments ~ is the bitwise NOT.
If you want a 5 bit unsigned bitwise NOT you can use an XOR with a mask:
>>> n = 0b10010 # 18
>>> m = 0b11111
>>> n ^ m
13
This question already has answers here:
What does the suffix e+number mean in python at the end of a float?
(2 answers)
What is the meaning of number 1e5?
(5 answers)
Closed 6 years ago.
I notice that there is such an expression "1e-5" in Python(probably in other languages also)
What is the name of this notation?
what does it denote in math?
What does 'e' mean? It's the first time I see a character helps to denote some value, are there other characters also help do so?
Why should use this way instead of some other python math operation like pow() etc.
It is scientific notation. It means 1 × 10−5. In other words, 0.00001.
10 ** -5, i.e. 10 to the power of negative 5, 1 divided by 10 to the power of 5, or 0.00001.
It means 10 to the power of -5 times 1
This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10