2

I am trying to translate a BASIC program. It has been many decades since I did any BASIC programming. :)

I am having a problem with 2 lines of code:

360 D=D+((X(I)-X(J))^2+(Y(I)-Y(J))^2+(Z(I)-Z(J))^2)^(-1/2)

510 F=((X(I)-X(J))^2+(Y(I)-Y(J))^2+(Z(I)-Z(J))^2)^(3/2)

x(i) etc form (x,y,z) coordinates; so line 360 is - I think - calculating the distance between 2 points; that would work if ^(-1/2) = square-root.

Line 510 is very similar, but the ^(3/2) has me stumped. Is it sensible to raise a number to the power of 1.5?

I remember that ^2 means square (aka raise to the power 2). So, can someone please tell me what ^(-1/2) and ^(3/2) mean in BASIC!?

Thanks for you help. Steve.

  • I know nothing about this program except the lines quoted by the OP and the assumption that it's decades old, but just from that I already know that it's an awesome program. I can imagine a 1980's version of myself hunched over the C64 cranking out lines like this to make a game sprite move along an unpredicable path and feeling very smug when it worked. – Spudley Mar 19 '19 at 11:09

4 Answers4

4

Is it sensible to raise a number to the power of 1.5?

Yes. There is an ordinary arithmetical meaning to raising any number to any power. In this case it is equivalent to cubing then square-rooting (or square-rooting then cubing, as multiplication is commutative).

^(-1/2) = square-root.

Not quite. Raising a number to a negative power is equivalent to finding the reciprocal of having raised it to a positive power, so that is a "1 divided by square root X" operation

Caleth
  • 52,200
  • 2
  • 44
  • 75
0

In BASIC the following is true:

360 D = D + ((X(I) - X(J)) ^ 2 + (Y(I) - Y(J)) ^ 2 + (Z(I) - Z(J)) ^ 2) ^ (-1 / 2)
361 D = D + 1 / SQR((X(I) - X(J)) ^ 2 + (Y(I) - Y(J)) ^ 2 + (Z(I) - Z(J)) ^ 2)

510 F = ((X(I) - X(J)) ^ 2 + (Y(I) - Y(J)) ^ 2 + (Z(I) - Z(J)) ^ 2) ^ (3 / 2)
511 F = SQR(((X(I) - X(J)) ^ 2 + (Y(I) - Y(J)) ^ 2 + (Z(I) - Z(J)) ^ 2) ^ 3)
eoredson
  • 1,167
  • 2
  • 14
  • 29
  • 1
    Thanks! What is the significance of the `-1` in `^ (-1 / 2)`; also is `sqr(...)` the same as `(...) ^2`? – Steve Peter Mar 20 '19 at 07:42
  • 1
    Those so happen to be two pairs of (equivalent) assignments. Imperative languages are bad at *describing* equivalences – Caleth Mar 20 '19 at 08:29
  • The **SQR(x)** is the same as **X^(1/2)** – eoredson Apr 07 '19 at 04:23
  • The statement **x = x ^ -1** is the same as **x = 1 / x** – eoredson Apr 07 '19 at 04:24
  • Don't confuse **SQR(X)** which is "square root" with square which is **X^2** – eoredson Apr 07 '19 at 04:40
  • 1
    Gotya! :) so my lines are not calculating a distance! And could be written as: `dx = x(i)-x(j) dy = y(i)-y(j) dz = z(i)-z(j) D = D + 1 / sqr( dx^2 + dy^2 + dz^2 ) F = sqr( (dx^2 + dy^2 + dz^2 ) ^ 3)` Sorry for the insertions but I don't think I can have newlines appear in comments. Many thanks eoredson! – Steve Peter Apr 08 '19 at 06:35
0

These functions should report arrays of equal values:

DIM x(10) AS INTEGER, y(10) AS INTEGER, z(10) AS INTEGER
FOR x = 1 TO 10
    x(x) = x: y(x) = x: z(x) = x
NEXT
I = INT(RND * 10 + 1): J = INT(RND * 10 + 1)

D1 = ((x(I) - x(J)) ^ 2 + (y(I) - y(J)) ^ 2 + (z(I) - z(J)) ^ 2) ^ (-1 / 2)
D2 = 1 / SQR((x(I) - x(J)) ^ 2 + (y(I) - y(J)) ^ 2 + (z(I) - z(J)) ^ 2)

F1 = ((x(I) - x(J)) ^ 2 + (y(I) - y(J)) ^ 2 + (z(I) - z(J)) ^ 2) ^ (3 / 2)
F2 = SQR(((x(I) - x(J)) ^ 2 + (y(I) - y(J)) ^ 2 + (z(I) - z(J)) ^ 2) ^ 3)

IF D1 = D2 THEN PRINT "values equal to:"; D1
IF F1 = F2 THEN PRINT "values equal to:"; F1
END
eoredson
  • 1,167
  • 2
  • 14
  • 29
0

Three-dimensional coordinate systems from google:

The distance formula states that the distance between two points in xyz-space is 
the square root of the sum of the squares of the differences between corresponding 
coordinates. That is, given P1 = (x1,y1,z1) and P2 = (x2,y2,z2), the distance 
between P1 and P2 is given by d(P1,P2) = sqr((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2)

So, apparently, you can get the distance between 2 xyz points..

eoredson
  • 1,167
  • 2
  • 14
  • 29