Approx Equals Vectors

Benchmark created on


Setup

const a = {x: 1, y: 2, z: 3, w: 4}
const b = {x: 1, y: 2, z: 3, w: 4}
const c = {x: 1, y: 2, z: 3, w: 5}
const d = {x: 6, y: 7, z: 8, w: 9}
const epsilon = 0.0001;

function distance(p1, p2)
{
	const dx = p1.x - p2.x;
	const dy = p1.y - p2.y;
	const dz = p1.z - p2.z;
	const dw = p1.w - p2.w;
	return Math.sqrt(dx ** 2 + dy ** 2 + dz ** 2 + dw ** 2);
}
function approxEqualsLength(p1, p2)
{
	return distance(p1, p2) < epsilon;
}

function approxEqualsAbs(p1, p2)
{
	return Math.abs(p1.x - p2.x) < epsilon &&
		Math.abs(p1.y - p2.y) < epsilon &&
		Math.abs(p1.z - p2.z) < epsilon &&
		Math.abs(p1.w - p2.w) < epsilon;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Approx Equals Length Same Value
const result1 = approxEqualsLength(a, b);
ready
Approx Equals Length Similar Value
const result2 = approxEqualsLength(a, c);
ready
Approx Equals Length Different Value
const result3 = approxEqualsLength(a, d);
ready
Approx Equals Abs Same Value
const result4 = approxEqualsAbs(a, b);
ready
Approx Equals Abs Similar Value
const result5 = approxEqualsAbs(a, c);
ready
Approx Equals Abs Different Value
const result6 = approxEqualsAbs(a, d);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.