Skip to content

fix: Handle NaN and infinity #2535

@aleksanderkatan

Description

@aleksanderkatan

Can be written in plain JS, cannot appear during shader compilation in WGSL.
Furthermore, they are later appended with 'f'/'u'/'i', which can result in accidentally compiling code.

    const fn = () => {
      'use gpu';
      const NaNf = 0;
      const b = d.f32(NaN);
    };

    expect(tgpu.resolve([fn])).toMatchInlineSnapshot(`
      "fn fn_1() {
        const NaNf = 0;
        const b = NaNf;
      }"
    `);

Solution 1 (sane):
throw when encountered

Solution 2 (insane):
Replace NaN and infinity by calls to the following:

const divide = (a: number, b: number) => {
  'use gpu';
  return a / b;
};

const getNaN = () => {
  'use gpu';
  return divide(0, 0);
};

const getInfinity = () => {
  'use gpu';
  return divide(1, 0);
};

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions