-
Notifications
You must be signed in to change notification settings - Fork 24
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
Conversation
…ce between 2 points. This makers it easier to read and it is also more computationally efficient.
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(); |
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(); |
@Dream23322, the modified script no longer uses 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 In your modified script, you are comparing the difference between |
Any updates? |
No response. |
…ce between 2 points.
This makers it easier to read and it is also more computationally efficient.