I've heard that Python does not have any upper limit with integers. So I wanted to give a try:
a = 1e100
b = 1
c = a + b + a
c - 2 * a
> 0.0
Unfortunately I realized that writing 1e2 returns a float while 100 returns an int.
I've then tested with long('1' + '0' * 100) which works.
a = long('1' + '0' * 100)
b = 1
c = a + b + a
c - 2 * a
> 1L
Is this solution the only way to affect a Googol to a variable?
Subsequent question:
How to avoid confusion between floating point and fixed point during computations?
You can get a Googol like so:
10**100
I dont really understand your question but i think you are asking is there only this way to manipulate googol variable .
i just tried this on my python idle and got this
>>> a = 10 ** 100
>>> b = 1
>>> c = a + b + a
>>> c - 2 *a
1
>>>
You could use the power operator:
base**times
so 123 googols would be 123*10**100
Related
I have an expression which contains terms like expr = a**m * b**n where a,b are symbols and m,n are integers. I want to use expr.subs(a**i * b**j, 0) to set the expression to zero if a**i * b**j is a factor of a**m * b**n (here i,j are integers too). This works in some cases, but not when only one of the symbols is used for the substitution:
>>> from sympy import symbols
>>> a, b = symbols('a b')
>>> (a**2 * b**3).subs(a**2 * b**2, 0) # (1) works
0
>>> (a**2 * b**3).subs(a * b**2, 0) # (2) works
0
>>> (a**2 * b**3).subs(b**2, 0) # (3) does not work
a**2*b**3
>>> (a**3).subs(a**2, 0) # (4) does not work
a**3
>>> (a**4).subs(a**2, 0) # (5) works
0
I would like this substitution to work in all of the above cases and logically it should (since for example a**2 * b**3 is equivalent to a**2 * b * (b**2) and thus the substitution (b**2, 0) should yield 0). However, if using a single symbol in the substitution, it seems to work only for those cases where the expression contains the subs-term raised to a power; for example:
>>> (a**2 * b**4).subs(b**2, 0) # (6) works
0
>>> (a**2 * b**4).subs(b**3, 0) # (7) does not work
a**2*b**4
This behavior seems a little odd since it works for b**1 and also when artificially augmenting both, the actual expression and the subs-term, with some other symbol:
>>> (a**2 * b**4).subs(b, 0) # (8) works
0
>>> c = symbols('c')
>>> (c * (a**2 * b**4)).subs(c * (b**3), 0) # (9) works
0
While I could use this last observation (augmenting expressions) as a workaround, it doesn't seem neither clear nor efficient.
Hence, is there another way to make the substitution work for all cases where the subs-term is contained in the actual expression?
>>> sympy.__version__
'1.10.1'
To check if something is a factor of a product you can use extract_multiplicatively:
>>> expr = a**3*b**3*c
>>> 0 if expr.extract_multiplicatively(a*b**2) else expr
0
You could also use the rem function to see if the remainder is 0:
>>> rem(expr, a*b**2)
0
If your expressions occur within a larger expression, then these could be incorporated into a replace call, expr.replace(...). If you are trying to ignore high-order terms in an expression you can just make a function to add the order of all symbol-based powers in a term:
>>> expr = expand((x + 1)**3*(y + 1)**2)
>>> def order(m):
... return sum([degree(m, x) for x in m.free_symbols])
...
>>> expr.replace(lambda x: not x.is_Add,
... lambda x: 0 if order(x)>2 else x)
3*x**2 + 6*x*y + 3*x + y**2 + 2*y + 1
works, because a2 = 0 is 0 * b3 what leads to 0
a = 0 means a**2 = 0 -> 0
b2 = 0 but b2 is not in b**3, so it does not work
same issue as 3
a4 is in a2 so 0
same as 5 but with b´s
same issue as 3
b is in b**4
c = c so it comes to 0 because only symbols get replaced
Everything works like intended
Is it possible to assign < to a variable, let's say b ?
For example:
b = < .
Edit:
My plan is the following:
a < b < c < d .
I want add the "compares" to a permutation. I know that I can add a b c d to a permutation but I have to change the < too. Please believe me that this is important for my plan. I have to find all possibilities.
You can do it like so
b = '<'
c = '+'
When you want to use it, you have to use the eval function.
eval('5' + b + '10') # '5<10'
>>> True
eval('5' + c + '10') # '5+10'
>>> 15
I started learning python and while i was checking some of the examples about shortcut operators I ran into this one
a = 6
b = 3
a /= 2 * b
print(a)
which print 1.0 as the result which i'm sure is wrong because it's supposed to be the simplified version of:
a = a / 2 * b
which gives 9.0 as the result and I think makes more sense acording to the order
a /= <anything here> computes all of the <anything here> first, and then does the division and assignment. So in your example, it is equivalent to a = a / (2 * b) (note parentheses).
Think about how the order of operations works:
a /= c
Is equivalent to
a = a / c
In a similar line,
a /= 2 * b
Is tantamount to
a = a / (2 * b)
Which equates to 6 / (2 * 3) which indeed equals 1.
a /= 2 * b
is in other words a = a / ( 2 * b)
This
a /= 2 * b
corresponds to
a = a / (2 * b)
and not to
a / 2 * b
I read that if I have the following expression:
variable = variable op expression
It can be simplified and shown as follows:
variable op= expression
For example: i = i + 2 * j --> i += 2 * j
However, I have used the previous example in a = a / 2 * b --> a /= 2 * b
Let a=6 and b=3. Using both values in a=a/2*b, the output is 9. However, in the case of a /= 2* b, the output is 1
This is because you are changing the order of operations.
a = a / 2 * b is interpreted as "Divide a by 2, then multiply by b, then store the result in a", or a = (a / 2) * b.
a /= 2 * b is interpreted as "Multiply 2 by b, then divide a by the result, and store the final result in a", or a = a / (2 * b).
I hope this helps.
I have to compare two numbers and write greater, then smaller, but I don't know how to get smaller one. I currently have this code:
a = int(input())
b = int(input())
c = round(((a + b) + abs(a - b)) / 2)
x = "should be smaller one"
print("{0}\n{1}".format(c, x))
smaller is : c = round(((a + b) - abs(a - b)) / 2)
If I right understand you:
In [1]: a , b = 3, 4
In [2]: smaller = a if a < b else b
In [3]: smaller
Out[3]: 3
Here you can replace < to other operator