Unexpected number of decimal places and Syntactical Query
<p>I'm trying to find the intersection between the curves $ y= x^2+3x+2 $ and $ y=x^2+2x+1$. For this, I have written the following python program:</p>

<pre><code>from numpy import *
import numpy as np
for x in np.arange(-100, 100, 0.0001):
    y_1=x**2+3*x+2
    y_2=x**2+2*x+1
    if round(y_1, 5)==round(y_2,5):
        print x
print 'end'
</code></pre>

<p>The console displays:</p>

<pre><code>-0.999999996714
end
</code></pre>

<p>I have three questions. </p>

<p>1) Why must I include <code>y_1=x**2+3*x+2</code> and <code>y_2=x**2+2*x+1</code> in the for statement? Why can I not simply include them after the line <code>from numpy import*</code>?</p>

<p>2) Why is the output to 12 decimal places when I have specified the step in <code>np.arange</code> to be 4 decimal places?</p>

<p>3) Why is -1.0000 not outputted?</p>

<p>Please go easy on me, I'm just starting to use python and thought I would try and solve some simultaneous equations with it.</p>

<p>Thanks,</p>

<p>Jack</p>

<p>1) First you have (probably) redundant import statements:</p>

<pre><code>from numpy import *
import numpy as np
</code></pre>

<p>The first statement imports the <code>__all__</code> variable from the package the second statement imports the numpy package then aliases it as <code>np</code>. The normal convention is to import numpy as <code>np</code>, so I would delete your first line and keep the second. </p>

<p>Now to more clearly answer your question, you need to include your equations in the for loop because <code>x</code> is representing each element in the <code>np.array</code> using the <code>for</code> loop.</p>

<p>2 and 3) The value is probably being interpreted as a float in your equations. The rounding error is inherent to how python (and most programing languages) interpret fractions. <a href="https://docs.python.org/3/tutorial/floatingpoint.html" rel="nofollow">See more here</a>.  </p>

python numpy