Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you do this:</p> <pre><code>m = (y1-y2) / (x1-x2) </code></pre> <p>You get a float (a floating point representation of a real number), not an int (an integer).</p> <p>Then, because <code>m</code> is a float, <code>b</code> too is a float. Then, when you call <code>str</code> on a float, you get a decimal point.</p> <p>The right way to fix this depends on what you actually want to do.</p> <p>If you really want to deal only with integers, you can use integer division:</p> <pre><code>m = (y1-y2) // (x1-x2) </code></pre> <p>That means if, say, <code>x1, x2, y1, y2 = 4, 2, 2, 1</code> you'll end up with <code>b = 2</code> (<code>2 - 0 * 4</code>). I suspect that isn't what you want; you actually want to multiply that <code>4</code> by <code>1/2</code>, and then round the result afterward.</p> <p>In that case, you can do the conversion the same place you round:</p> <pre><code>m = int(round(m, 0)) b = int(round(b, 0)) </code></pre> <p>Note that your existing calls to <code>round</code> didn't actually do anything; <code>round(m, 0)</code> doesn't modify <code>m</code>, it returns a new value that's the rounded version of <code>m</code>, which you have to assign back to <code>m</code> (or somewhere else).</p> <p>One more thing to consider: You probably really do want literally one half, not "the closest floating point representation to 0.5".</p> <p>You probably don't actually care about the difference—unless you're using quite large integers, the rounding errors won't ever be visible. But if you <em>do</em> care, you may want to consider using a third-party exact-fraction module (e.g., <code>clnum</code>), or possibly the standard library <code>decimal</code>. This would mean you have to be more explicit, so I wouldn't do this unless you expect it to be necessary. But if it is necessary, it would look something like this:</p> <pre><code>m = decimal.Decimal(y1-y2) / (x1-x2) b = y1 - m * x1 m = int(m.to_integral(rounding=decimal.ROUND_HALF_UP)) b = int(b.to_integral(rounding=decimal.ROUND_HALF_UP)) </code></pre> <p>Finally, you can always keep the numbers around as <code>float</code>s (or <code>Decimal</code>s, etc.), never rounding or converting them, and force them to print as integers anyway:</p> <pre><code>print("Y = {:.0g}X - {:.0g}.".format(m, b)) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload