avajs/ava

Zero != -Zero in is / deepEqual

seanmay opened this issue · 0 comments

Apologies if this is ridiculous; I realize that 0 and -0 are two distinct values, when it comes to textual representations and binary encodings, however from the perspective of mathematical operations in, say, linear algebra, they are equivalent.

Goal

I am trying to test an unorthodox project, with some bespoke math libraries. Converting CSG volumes in a very particular format (Quake 1 .MAP format and Half-Life 1 .MAP format, specifically) to triangle-mesh, among other things.

Reproduction

I have some Objects with Static Methods Vec3.add(v1, v2, vecOutput) rather than v1.add(v2) for instance, so there are no private state surprises, etc.

Given something like the following:

test("Triangle#clockwiseNormal", (_) => {
  const expected = Vec3.of(0, 0, 1);

  const p1 = Vec3.of(-1, 0, 0);
  const p2 = Vec3.of(1, 0, 0);
  const p3 = Vec3.of(0, -1, 0);
  const output = Vec3.of(0, 0, 0);

  const result = Triangle.clockwiseNormal(p1, p2, p3, output);
  return _.deepEqual(result, expected);
});

I get an error stating that [0, -0, 1] != [0, 0, 1] because

-  0
+ -0

Running a simpler test test("0 is 0", (_) => _.is(-0, 0)) produces the same error.

Expectation

test("0 is 0", (_) => _.assert(_.is(-0, 0)))

test("0s are 0s", (_) => _.assert(_.deepEqual([-0, -0, -0], [0, 0, 0])))

I can manually craft each and every expected value to incorporate -0 in all components, but when it comes time to validate even a trivial Quake map, converted to a mesh suitable for uploading to WebGPU shaders, or the like, that's going to be a lot of manual effort.

Version

ava@latest ("^5.2.0") added yesterday

Config

Default config (no config files; no package.json changes).

Runtime

package.json: { "scripts": { "dev:test": "ava --watch" } }

shell: npm run dev:test