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

Closed
wants to merge 1 commit into 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();

@Dream23322
Copy link
Author

Take a look at this

https://onecompiler.com/javascript/433pqyfkz

// 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.sqrt((pos1.x - pos2.x) ** 2 + (pos1.y - pos2.y) ** 2 + (pos1.z - pos2.z) ** 2);
}

// 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();

@Visual1mpact
Copy link
Owner

@Dream23322, the modified script no longer uses Math.hypot, which was originally included in the commit and the changed file:

return Math.hypot(pos1.x - pos2.x, pos1.y - pos2.y, pos1.z - pos2.z);

You modified the script I provided, which originally showed a performance comparison between using Math.hypot and Math.pow for calculating the distance using the Euclidean method.

In your modified script, you are comparing the difference between Math.pow and the exponentiation operator (**) when calculating the Euclidean distance. If your intent is to update your PR to reflect that specific change, then you'll need to adjust the script accordingly, as this change no longer involves Math.hypot.

@Visual1mpact
Copy link
Owner

Any updates?

@Visual1mpact
Copy link
Owner

No response.

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