user12572: I have written an expression to get the distance between two points, but it does not work and I haven't a clue why. It's pretty simple:
function findDistance(xa, xb, ya, yb)
{
    return Math.sqrt( (xa - xb)^2 + (ya - yb)^2 );
}
findDistance(682, 617, 310, 342);

The error I get is, "Error at line 0... invalid numeric result (divide by zero?)."
I am so confused by this. It seems so straight forward, so I am not even sure what to ask. Can anyone see what is wrong with this?

EDIT: I am trying to narrow down the issue. Even using the following expression is not working:
Math.sqrt( (682-617)^2 + (310-342)^2 );

Narrowing down further, this finally worked:
Math.sqrt( 5249 );

I also isolated and confirmed that X^2 is the correct way to square something, so what gives?

user12572: I figured it out. The syntax and/or order of operation could not handle squaring two numbers before adding them, so I added parenthesis to isolate each number and square them BEFORE adding them together.
function findDistance(xa, xb, ya, yb)
{
    return Math.sqrt( ((xa - xb)^2) + ((ya - yb)^2) );
}
findDistance(682, 617, 310, 342);


user1950: Hate to spoil all your good work, but it's a lot easier to use the built-in length() function:
length(pointa, pointb)

e.g. 
length(thisComp.layer(1).position, thisComp.layer(2).position)

or
length([682, 617], [310, 342]); //raw vector values as an array
length(thisComp.layer("foo").anchorPoint, [310, 342]); //a spatial property and an array
length([123,456,789], [10,9,8]); //works in 3D too. 

It's in vector math in the expressions drop-down panel.


12572: <p>I have written an expression to get the distance between two points, but it does not work and I haven't a clue why. It's pretty simple:</p>

<pre class="lang-js prettyprint-override"><code>function findDistance(xa, xb, ya, yb)
{
    return Math.sqrt( (xa - xb)^2 + (ya - yb)^2 );
}
findDistance(682, 617, 310, 342);
</code></pre>

<p>The error I get is, "Error at line 0... invalid numeric result (divide by zero?)."</p>

<p>I am so confused by this. It seems so straight forward, so I am not even sure what to ask. Can anyone see what is wrong with this?</p>

<hr>

<p>EDIT: I am trying to narrow down the issue. Even using the following expression is not working:</p>

<pre class="lang-js prettyprint-override"><code>Math.sqrt( (682-617)^2 + (310-342)^2 );
</code></pre>

<p>Narrowing down further, this finally worked:</p>

<pre class="lang-js prettyprint-override"><code>Math.sqrt( 5249 );
</code></pre>

<p>I also isolated and confirmed that X^2 is the correct way to square something, so what gives?</p>
 12572: <p>I figured it out. The syntax and/or order of operation could not handle squaring two numbers before adding them, so I added parenthesis to isolate each number and square them BEFORE adding them together.</p>

<pre class="lang-js prettyprint-override"><code>function findDistance(xa, xb, ya, yb)
{
    return Math.sqrt( ((xa - xb)^2) + ((ya - yb)^2) );
}
findDistance(682, 617, 310, 342);
</code></pre>
 1950: <p>Hate to spoil all your good work, but it's a lot easier to use the built-in <code>length()</code> function:</p>

<pre><code>length(pointa, pointb)
</code></pre>

<p>e.g. </p>

<pre><code>length(thisComp.layer(1).position, thisComp.layer(2).position)
</code></pre>

<p>or</p>

<pre><code>length([682, 617], [310, 342]); //raw vector values as an array
length(thisComp.layer("foo").anchorPoint, [310, 342]); //a spatial property and an array
length([123,456,789], [10,9,8]); //works in 3D too. 
</code></pre>

<p>It's in <strong>vector math</strong> in the expressions drop-down panel.
<a href="https://i.stack.imgur.com/chgPx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/chgPx.png" alt="enter image description here"></a></p>
