Skip to main content

AI vs Pythagoras

I needed a function to check if the point belongs to a line between two other points. Doable with a bit of good old Pythagoras. Boring, so I thought I’d save three minutes and ask ChatGPT instead. I had a feeling of things to come, but John Lennon suggested “giving peace a chance,” so I gave it a chance.

Computer Says No

I’m logging in to https://chat.openai.com, enter the query, the clock is ticking, one minute already spent. Finally, the powerful NVIDIA-powered hyper-intelligent multidimensional Supreme Being gives us the answer:

Here's a JavaScript function that checks if a point belongs to a line defined by two other points:

Brace for it.

function pointBelongsToLine(point, linePoint1, linePoint2) {
  // Calculate the slope (m) of the line
  const m = (linePoint2.y - linePoint1.y) / (linePoint2.x - linePoint1.x);

  // Calculate the y-intercept (b) using one of the points
  const b = linePoint1.y - m * linePoint1.x;

  // Use the equation of the line to check if the point belongs to the line
  return point.y === m * point.x + b;
}

Hmm. Really?

A starfish with 500 neurons can see from a distance of 100m that the code returns NaN when the line is vertical. Even at my age I have more than 500 functioning neurons, so I can see it too. Division by zero, darling. In C# this would simply crash the application. Maybe we should be more specific about that? I asked to fix it for vertical lines too, by complaining:

The above doesn't work for vertical lines.

The humiliated supreme being attempts flattery:

You're right; my previous solution assumes that the line is not vertical.

Sure. Just admit you simply don’t get it, my dear, don’t you ;-) Meanwhile, we got an improved version:

function pointBelongsToLine(point, linePoint1, linePoint2) {
  // Check if the line is vertical
  if (linePoint1.x === linePoint2.x) {
    return point.x === linePoint1.x;
  }

  // Calculate the slope (m) of the line
  const m = (linePoint2.y - linePoint1.y) / (linePoint2.x - linePoint1.x);

  // Calculate the y-intercept (b) using one of the points
  const b = linePoint1.y - m * linePoint1.x;

  // Use the equation of the line to check if the point belongs to the line
  return point.y === m * point.x + b;
}

Nice try. How about it checking the following conditions?

point { x: 15, y: 0 }
linePoint1 { x: 0, y: 0 }
linePoint2 { x: 10, y: 0 }

which represents the following situation:

Oh where could that point possibly be?

The function merrily says that the point belongs. Really? To what?

It certainly belongs to some infinite line passing through the specified points. But the point is certainly not between the line ends! It seems like I should be even more specific. Asking further:

I need to check whether the point is between the two points making the line.

and the result promptly follows:

function pointIsBetweenPoints(point, linePoint1, linePoint2) {
  // Check if the point's x-coordinate is between the x-coordinates of the two endpoints
  const isXBetween = (point.x >= Math.min(linePoint1.x, linePoint2.x)) &&
                    (point.x <= Math.max(linePoint1.x, linePoint2.x));

  // Check if the point's y-coordinate is between the y-coordinates of the two endpoints
  const isYBetween = (point.y >= Math.min(linePoint1.y, linePoint2.y)) &&
                    (point.y <= Math.max(linePoint1.y, linePoint2.y));

  // Return true if both conditions are met, indicating that the point is between the two points
  return isXBetween && isYBetween;
}

That’s… suspicious. Just a simple comparison? No line equations nor Pythagoras? Let’s check with diagonal line from 0,0 to 10,10.

  • Point (3,3) - check!
  • Point (5,5) - check!
  • Right. I can see it. Point (5,6) - check! While it should not.

The new algorithm checks whether the point is inside a rectangle defined by the two points, and not on a line joining them. Sigh.

This is where my adventures with ChatGPT ended today. After spending 15 minutes observing its labours, then writing this little comment, I finally wrote my own code. Rusty, boring, and made not by the NVIDIA V100’s 21 billion transistors but by my own chaotic brain, but it does survive a whole series of unit tests.

Humor aside, time for conclusions: you have to be specific to the point of paranoid when using LLM to generate code. Prepare for repeated attempts, each result needs to be carefully scrutinized, preferably with unit tests. Just as if you do it with your own code or anyone else’s. Because in the end, the code generated by LLM is nothing more than a probabilistic rehash of someone else’s code. Otherwise …

As I write this, bazillions of developers are happily copying and pasting LLM creations directly into their code, often unverified and untested, while their managers are drooling over the next bonus due to productivity growth that is “simply insane” (in the words of the gurus of artificial intelligence).

I wouldn’t say we’re doomed as species, but we’re certainly doomed to live in the world run by sh*tty software for the foreseeable future ;-)