Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Math.hypot instead of "manually" calculating the Euclidean distan… #64

Open
wants to merge 1 commit into
base: rewrite
Choose a base branch
from

Conversation

Dream23322
Copy link

…ce between 2 points.

This makers it easier to read and it is also more computationally efficient.

…ce between 2 points.

This makers it easier to read and it is also more computationally efficient.
@Visual1mpact
Copy link
Owner

Visual1mpact commented Dec 16, 2024

https://onecompiler.com/javascript/4337nuh8r

Take a moment to look at this test doing 100,000 iterations.

// Function to calculate distance using Math.sqrt + Math.pow
function distanceWithSqrtPow(pos1, pos2) {
    return Math.sqrt(
        Math.pow(pos1.x - pos2.x, 2) +
        Math.pow(pos1.y - pos2.y, 2) +
        Math.pow(pos1.z - pos2.z, 2)
    );
}

// Function to calculate distance using Math.hypot
function distanceWithHypot(pos1, pos2) {
    return Math.hypot(
        pos1.x - pos2.x,
        pos1.y - pos2.y,
        pos1.z - pos2.z
    );
}

// Generate random Minecraft coordinates with floating-point values
function generateRandomCoordinates() {
    return {
        x: (Math.random() * 30000000) - 15000000, // World bounds for X and Z
        y: Math.random() * 256,                  // Height range (0–256)
        z: (Math.random() * 30000000) - 15000000 // World bounds for X and Z
    };
}

// Test speed
function testSpeed(iterations) {
    const pos1 = generateRandomCoordinates();
    const pos2 = generateRandomCoordinates();

    console.time("Math.sqrt + Math.pow");
    for (let i = 0; i < iterations; i++) {
        distanceWithSqrtPow(pos1, pos2);
    }
    console.timeEnd("Math.sqrt + Math.pow");

    console.time("Math.hypot");
    for (let i = 0; i < iterations; i++) {
        distanceWithHypot(pos1, pos2);
    }
    console.timeEnd("Math.hypot");
}

// Test accuracy
function testAccuracy() {
    const pos1 = generateRandomCoordinates();
    const pos2 = generateRandomCoordinates();

    const resultSqrtPow = distanceWithSqrtPow(pos1, pos2);
    const resultHypot = distanceWithHypot(pos1, pos2);
    const difference = Math.abs(resultSqrtPow - resultHypot);

    // Calculate the difference as a percentage of the result
    const percentageDifference = (difference / resultSqrtPow) * 100;

    console.log("Coordinates:");
    console.log("  Point 1:", pos1);
    console.log("  Point 2:", pos2);
    console.log("Results:");
    console.log("  Math.sqrt + Math.pow:", resultSqrtPow);
    console.log("  Math.hypot:", resultHypot);
    console.log("Difference:");
    console.log(`  Absolute: ${difference}`);
    console.log(`  Percentage of Result: ${percentageDifference.toFixed(20)}%`);

    // Provide interpretation based on the percentage
    if (percentageDifference < 0.000001) {
        console.log("  Difference is negligible.");
    } else if (percentageDifference < 0.001) {
        console.log("  Difference is very small.");
    } else {
        console.log("  Difference is noticeable but unlikely to matter.");
    }
}

// Run tests
const iterations = 100000; // Number of iterations for speed test
console.log("Testing speed...");
testSpeed(iterations);

console.log("\nTesting accuracy...");
testAccuracy();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants