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>

<ol>
<li>Because the <code>y_1</code> and <code>y_2</code> lines are computing specific values, not defining functions.  Plain Python does not have a built-in concept of symbolic equations.  (Although you can implement symbolic equations various ways.)</li>
<li>Because binary floating-point, as used in Python, cannot exactly represent 0.0001 (base 10).  Therefore, the step is rounded, so your steps are not exactly ten-thousandths.  The Python <code>print</code> statement does not round, absent specific instructions to do so, so you get exactly the value the system is using, even though that's not quite the value you asked for.</li>
<li>Same reason: Since the steps are not exactly ten-thousandths, the point at which the functions are close enough to test as equal under rounding is not exactly at -1.</li>
</ol>

python numpy