Skip to content

runtime.wrap doesn't work correctly for fractional numbers #1562

Open
@blerner

Description

@blerner

Consider the following expression (on current CPO): image-pinhole-x(point-polygon([list: point(0, 0), point(2.5, 0), point(32, 0)], "solid", "red")). It will evaluate to 11.5 -- but it's a broken 11.5. It's not an instance of Roughnum, but it is a raw JS number, so it renders as a raw JS number, and doesn't render as a fractional quantity. The problem seems to be here, in runtime.js:

    function wrap(v) {
      if(jsnums.isPyretNumber(v)) { return makeNumberBig(v); }
      else if(typeof v === "number") { return makeNumber(v); }
      ...
    }

The definition of jsnums.isPyretNumber then says

  var isPyretNumber = function(thing) {
    return (typeof(thing) === 'number'
            || (thing instanceof Rational ||
                thing instanceof Roughnum ||
                thing instanceof BigInteger));
  };

and makeNumberBig says

    function makeNumberBig(n) {
      return n;
    }

We use wrap all over the image library, and probably elsewhere too, so it seems quite odd to me that wrap doesn't actually wrap numbers correctly... I'm not sure exactly what the right fix should be, though, since I don't think wrap has enough information to know whether to make an exact number or a roughnum. And I don't know what exactly we ought to do in the image library, short of using jsnums.* everywhere instead of raw JS arithmetic, to ensure that we create rationals when we can do so... It seems reasonable that our image library has to discretize its answers to the pixel grid, so things like "what's the pinhole location?" ought to be a rational at worst, if not also forced to an integer on the pixel grid as well...

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions