How to change priority in math order(asterisk)
<p>I want users to input math formula in my system. How can convert case1 formula to case2 formula using Python? In another word, I would like to change math order specifically for double asterisks.</p>

<pre><code>#case1
3*2**3**2*5
&gt;&gt;&gt;7680

#case2
3*(2**3)**2*5
&gt;&gt;&gt;960
</code></pre>

<p>If users are supposed to enter formulas into your program, I would suggest keeping it as is. The reason is that exponentiation in mathematics is <a href="https://en.wikipedia.org/wiki/Operator_associativity" rel="nofollow">right-associative</a>, meaning the execution goes from the top level down. For example: <code>a**b**c = a**(b**c)</code>, by convention.</p>

<p>There are some programs that use bottom-up resolution of the stacked exponentiation -- MS Excel and LibreOffice are some of them, however, it is against the regular convention, and always confused the hell out of me.</p>

<p>If you would like to override this behavior, and still be mathematically correct, you have to use brackets.</p>

<p>You can always declare your own power method that would resolve the way you want it -- something like <code>numpy.pow()</code>. You could overload the built-in, but that's too much hastle.</p>

<p><a href="https://en.wikipedia.org/wiki/Order_of_operations#Special_cases" rel="nofollow">Read this</a></p>

python python-2.7 numpy math