Division(//) in python - python

I can't understand the output of this code.
print(1//.2)
print(10//2)
The output of 1st is 4.0
and output of 2nd is 5

To clarify this, the // operator will give you the integer part of the result. So:
10 // 2 = 5
11 // 2 = 5
12 // 2 = 6
To get the rest you can use the modulo operator %:
10 % 2 = 0
11 % 2 = 1
12 % 2 = 0
Now, when you specify 0.2, this is taken as a floating point value. These are not 100% accurate in the way the system stores them, meaning the exact value of this is likely something like 0.2000000000000000001.
This means that when you do:
1 // 0.2
What you are really doing is:
1 // 0.2000000000000001
Which if course evaluates to 4. If you then try the modulo operator, you will see you get a value less than 0.2. When I ran this I saw:
>>> 1 // 0.2
4.0
>>> 1 % 0.2
0.19999999999999996

Related

How to measure similarity of inner observation variation without considering actual values?

I am sure that this has been done before but I am unsure of how to even phrase the question for google and have been racking my brain for a few hours now, but I can explain it with an example. Imagine you have the data below.
observation #
m1
m2
m3
m4
m5
m6
1
T
L
T
L
T
L
2
A
R
A
R
A
A
3
B
C
B
C
B
C
4
K
K
K
A
L
K
5
P
P
P
R
L
P
I want to generate some sort of similarity metric between observations that relates to the variation across the m1-6 variables. The actual values in the cells shouldn't matter at all.
Considering the table above, for example observations 1 and 3 are exactly the same as they vary the same across the m's (TLTLTL & BCBCBC). 1 & 3 are very similar to 2, and observations 4 and 5 are the same but not similar to 1-3.
I would like an output that captures all these relationships for example . . .
observation #
1
2
3
4
5
1
1
0.8
1
0.1
0.1
2
0.8
1
0.8
0.2
0.2
3
1
0.8
1
0.1
0.1
4
0.1
0.2
0.1
1
1
5
0.1
0.2
0.1
1
1
A few notes - each cell can have more than just 1 letter but again the actual contents of each cell don't matter - just the variation across the m's within each observation compared to other observations. Is their a name for what I am trying to do here? Also I only know python & R so if you provide any code please have it in those (python preferred).
It is driving me crazy that I can't figure this out. Thanks in advance for any help :)

How to find 2 to the power of an insanely big number modulo 10^9

I have an excessively big number (1500+) digits and I need to find 2 ** that number modulo 1_000_000_000 so I wrote this python:
n = 1
return_value = 2
while n < To_the_power_of:
return_value *= 2
return_value = return_value % 1_000_000_000
n += 1
This returns the correct value for smaller values, but takes too long for bigger values.
If the number is modulo 10 then you get this pattern which could be used.
2 ** 1 modulo 10 = 2
2 ** 2 modulo 10 = 4
2 ** 3 modulo 10 = 8
2 ** 4 modulo 10 = 6
2 ** 5 modulo 10 = 2
2 ** 6 modulo 10 = 4
2 ** 7 modulo 10 = 8
2 ** 8 modulo 10 = 6
I'm hoping that a similar pattern could be used to answer the original problem.
You already know that the sequence will repeat. You found the cycle of 4 for mod 10; now, just find it for a billion:
mod_billion = set()
pow_2 = 2
billion = 10**9
while pow_2 not in mod_billion:
mod_billion.add(pow_2)
pow_2 *= 2
if pow_2 > billion:
pow_2 -= billion
print (pow_2, len(mod_billion))
Three seconds later, we get:
512 1562508
Thus, this sequence repeats every 1562508 items. To find your value for your given power:
cycle = 1562508
small_power = big_power % cycle
result = (2 ** small_power) % billion
Your code makes about 10 ** 1500 iterations, which is indeed insanely long. A useful general technique is exponentiation by squaring, which will give you the result in about 4500 iterations.
If you want to follow the path of the #Prune's answer, you should go along the lines of Fermat's Little Theorem, specifically the Euler's generalization. phi(1_000_000_000) is easy to compute, because 10 ** 9 = (2 ** 9) * (5 ** 9), the product of 2 powers of primes.

Need integer output from equation using complex numbers in python

I have some equation for a type of step function which I obtained with wolfram alpha:
a_n = 1/8 (2 n+(-1)^n-(1+i) (-i)^n-(1-i) i^n+9)
Using in wolfram with any positive integer will yield me a positive integer result however when I try the following in python
import numpy as np
n = 5
i = complex(0,1)
a = (1/8)*((2*n)+(np.power(-1,n))-(1+i)*(np.power(-i,n))-(1-i)*(np.power(i,n))+9)
I'm always stuck with some real + imaginary part. I need to be able to obtain an integer output for a for use in other equations.
Maybe you want int(a.real) at the end.
Also be aware that by default 1/8 will be evaluated as 0 in Python 2.x
(1+i) (-i)^n+(1-i) i^n
is two times the real part of (1-i) i^n which is, for instance
2*cos(pi/2*n)-2*cos(pi/2*(n+1))
or as values
n 0 1 2 3 4 5 6 7 8
expression 2 2 -2 -2 2 2 -2 -2 2
this is subtracted from the alternating sequence to give
n 0 1 2 3 4 5 6 7 8
(-1)^n-expr -1 -3 3 1 -1 -3 3 1 -1
periodically with period 4
This can be computed avoiding all powers and saveguarding for negative n as
3-2*(((n+2) mod 4 +4) mod 4)
adding 2n+9 to complete the expression gives
12+2*n-2*(((n+2) mod 4 +4) mod 4)
which is indeed divisible by 8, so
a = 1+(n+2-(((n+2) % 4 +4) % 4) )/4
now if one considers that this just reduces (n+2) to the next lower multiple of 4, this is equivalent to the simplified
a = 1 + (n+2)/4
using integer division.

Any way so integers are classed as integers with no .0 and floats classed as floats?

So. In my program I have a part where I check whether the result of a division sum is an integer or not. For example, 6 / 3 = 2 (True) or 7 / 3 = 1.66 (False). The problem is that when I do a division like 6 / 3, the result that should be an integer is classed as a float because it comes out as 2.0 instead of 2. Is there any way so that decimal/float answers are classed as floats with a decimal point, and integer answers are classed as an integer? (The number without the .0 at the end)
I have this now:
6 / 3 = 2.0 (float)
7 / 3 = 1.66 (float)
I want this:
6 / 3 = 2 (integer)
7 / 3 = 1.66 (float)
Just use float.is_integer().
For example, as expressed by OP:
>>> num1 = 6 / 3 # 2.0
>>> num1.is_integer()
True
>>> num2 = 7 / 3 # 2.33
>>> num2.is_integer()
False
No need for anything complex here- and implementing this into your function should be easy.
Does 7 / 3 * 3 = 7?
return ((n1 // n2 * n2) == n1)

Arithmetic in python, can't figure out simple basic operations

I am doing some very basic excersizes in python. I'm using the hard way to learn python book, which in excersize 3 have a expression I should understand.
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6, is coming out as 7.
For me, the answer is 6.
6 - 5 + 0 - 1 / 4 + 6
1 - 1 / 4 + 6
6.
This is clearly wrong, but can anyone help me with priorty in mathematics etcetera? I seem to have forgotten everything if it's not inside a parenthesis!
EDIT: Thank you very much for the response. I've clearly learned something about the very basic stuff, which I think is important before moving on! My order of operations was definately way off!
4 % 2 = 0 because the remainder of 4 / 2 is 0
1 / 4 is also 0 because it is doing integer division and .25 is floored to 0.
If we look at the rules for Operator precedence in Python we can see that:
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
is being treated as:
3 + 2 + 1 - 5 + (4 % 2) - (1 / 4) + 6
(For the arithmetic operators this is the same order as the standard order of operations in mathematics.)
Now 4 % 2 is 0 since the remainder when dividing 4 by 2 is 0. 1 / 4 is also 0 as Python will return a value of the same type as the operands and 0.25 when "floored" is 0.
So I think your mistake is applying the / to the whole expression to the left. In fact as / has a higher precedence than - the division evaluated is 1 / 4.
See the table of operator precedences in Python.
http://docs.python.org/reference/expressions.html#summary
1/4 rounds down (or floors) to 0 since 1 and 4 are integers.
Therefore 1-0+6 = 7
You seem to think that python should evaluate everything to the left of a / operator before dividing by the first token to the right. That would be an odd evaluation order by any measure.
As always with programming: if you have a complex expression with infix operators, use brackets to force the correct order.
In Python3.x it'd be 6.75 b/c 1/4 = 0.25 (true division). However, in Python 2.x 1/4 = 0 (convert to the most generic type of the arguments used, that is int in given case). Therefore, if it'd be 1. / 4 or 1 / 4. then in Python2.x you'd get 0.25 and the result would be 6.75
Priority is for division and multiplication, if you don't use brackets!
In this way: 1-1/4+6=7 because 1/4, working with integer, is = 0
operands are integer, so 1/4 integer dividing = 0
Rather than mathematics you should have a look at the operators:
a%b is a modular b, which in case of a=4 and b=2 yields 0 (ok, thanks mathematics ;) )
1/4: here it's important to take into account that 1 and 4 are integers. So the result will also be an integer, i.e. 0.25 becomes 0.
That's why you end up with
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
= 3 + 2 + 1 - 5 + 0 - 0 + 6
= 7
For operators in python have a look at the python docs.
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
The order of precedence for this is multiplication and division evaluated left to right and then addition and subtraction. So..
3 + 2 + 1 - 5 + (4 % 2) - (1 / 4) + 6
The ones inside the parentheses are evaluated first because of order of operations. Because you only have integers present (and not a float which would allow the value 0.25), 1/4 will floor to 0 because of python.
3 + 2 + 1 - 5 + 0 - 0 + 6
Afterwards, when you evaluate from left to right for addition/subtraction, you are left with:
6 - 5 + 6 => 1 + 6 => 7
Remember that division and multiplication are evaluated before addition and subtraction. For more information, Order of Operations - Wikipedia

Categories