Skip to content
TheChatArchiveThe Chat Archive
Mistralmagistral-medium-2506· Mistral AIHugging Face LMArena Human Preference 140K

make me a java function that calculates the distance between two coordinates

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-flash-lite-preview-06-17-thinking’s answer.
  1. User

    make me a java function that calculates the distance between two coordinates

  2. Mistral Assistant

    Here's a Java function that calculates the distance between two coordinates using the Euclidean distance formula. The function takes four parameters representing the x and y coordinates of the two points and returns the distance as a double. The implementation uses Math.hypot for numerical stability.

    public class DistanceCalculator {
        public static double calculateDistance(double x1, double y1, double x2, double y2) {
            double dx = x2 - x1;
            double dy = y2 - y1;
            return Math.hypot(dx, dy);
        }
    
        public static void main(String[] args) {
            double distance = calculateDistance(1, 2, 4, 6);
            System.out.println(distance); // Output: 5.0
        }
    }
    

    This function works by calculating the differences in the x and y coordinates, then using the Math.hypot method to compute the hypotenuse of the right triangle formed by these differences, which gives the Euclidean distance between the two points. The use of Math.hypot ensures that the calculation is numerically stable and avoids potential overflow or underflow issues.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.