Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>I can read parts of them (multiplication, division, decimals, sigma, variables) but I have troubles when going off to implement them in code. </code></pre> <p>Well, the crucial thing I guess is that you need to be able to break down the formula you're interested into its component pieces, and if you can understand how to code those pieces individually, you are in a position to then glue them back together in the form of code.</p> <p>Let us take as an example the Manhattan distance between two points in two-dimensional space with a fixed <code>(x, y)</code> coordinate system. You want to write a function that will take two points and give you the Manhattan distance between those points. Let's forgo the use of object-oriented concepts here and just assume that you have four input variables:</p> <pre><code>x1, the x-coordinate of the first point y1, the y-coordinate of the first point x2, the x-coordinate of the second point y2, the y-coordinate of the second point </code></pre> <p>So our function will be like</p> <pre><code>function mdistance (x1, y1, x2, y2) { ??? } </code></pre> <p>What should the insides of the function (the function body) look like? Now we inspect the mathematical formula that we want to rewrite as code. Wikipedia's version (under "Formal description") treats the case of arbitrary dimensions - we can do this too, but for now we're considering the 2-dimensional case only. So their <code>n</code> is 2 as far as we are concerned, and we want to compute <code>|x1 - x2| + |y1 - y2|</code>. That is the result of losing the sigma notation in favour of an expression describing a sum with two summands. But we still haven't worked out how <code>|a - b|</code> can be expressed in computer code.</p> <p>So now the function could look like</p> <pre><code>function mdistance (x1, y1, x2, y2) { return bars(x1, x2) + bars(y1, y2); } </code></pre> <p>And that, as far as it goes, is fine, because we've isolated the thing we don't yet know how to do as another function, called <code>bars()</code>. Once we define <code>bars()</code>, the function <code>mdistance()</code> will work just fine, assuming of course that our definition for <code>bars()</code> is sensible. So the problem is just to define <code>bars()</code>. By breaking the problem into its component parts, we've made our job easier, because we just have to make each part work - which is simpler than making everything work at once.</p> <p>So how should <code>bars()</code> be defined? Well, <code>|a - b|</code> just expresses the idea "the absolute value of <code>a - b</code>". PHP has a built-in function for the absolute value of a real number; it is <code>abs()</code>. So we define <code>bars()</code> like this:</p> <pre><code>function bars (a, b) { return abs(a - b); } </code></pre> <p>Now our function <code>mdistance()</code> will work just the way we want.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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