Skip to content

Precision error in interpolateZoom #2575

Closed

Description

@cie

Hello,

For some input, interpolateZoom gives NaNs, because of precision problems.

http://jsfiddle.net/m68axcy3/

As far as I understand, the root of the problem is that the corner-case formula is only used when the xy coordinates of the inputs are exactly the same, but there occurs a rounding problem in Math.log(Math.sqrt(b0 * b0 + 1) - b0) when they are nearly the same.

Activity

  1. cie commented on Oct 7, 2015

    @cie
    Author

    I use a workaround now:

      if (d1 < 0.00001) {
        dr = NaN;
      }
    

    Like this:

    d3.interpolateZoom = function(p0, p1) {
      var ux0 = p0[0], uy0 = p0[1], w0 = p0[2], ux1 = p1[0], uy1 = p1[1], w1 = p1[2];
      var dx = ux1 - ux0, dy = uy1 - uy0, d2 = dx * dx + dy * dy, d1 = Math.sqrt(d2), b0 = (w1 * w1 - w0 * w0 + ρ4 * d2) / (2 * w0 * ρ2 * d1), b1 = (w1 * w1 - w0 * w0 - ρ4 * d2) / (2 * w1 * ρ2 * d1), r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0), r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1), dr = r1 - r0;
      if (d1 < 0.00001) {
        dr = NaN;
      }
      var S = (dr || Math.log(w1 / w0)) / ρ;
      function interpolate(t) {
        var s = t * S;
        if (dr) {
          var coshr0 = d3_cosh(r0), u = w0 / (ρ2 * d1) * (coshr0 * d3_tanh(ρ * s + r0) - d3_sinh(r0));
          return [ ux0 + u * dx, uy0 + u * dy, w0 * coshr0 / d3_cosh(ρ * s + r0) ];
        }
        return [ ux0 + t * dx, uy0 + t * dy, w0 * Math.exp(ρ * s) ]; 
      }
      interpolate.duration = S * 1e3;
      return interpolate;
    }
    

    With this I couldn't reproduce the NaN's. However, I don't know whether this will disturb the normal zoom-and-pan behaviour.

  2. added
    bug
    on Oct 9, 2015
  3. mbostock commented on Oct 9, 2015

    @mbostock
    Member

    Probably it would be sufficient to change the if (dr) { in the interpolate function to if (dr > ε) {.

  4. cie commented on Oct 10, 2015

    @cie
    Author

    Not really, because dr can be NaN (if ux1==ux0 && uy1==uy0 then d1==0, and then b0 and b1 is NaN).

  5. cie commented on Oct 10, 2015

    @cie
    Author

    Maybe better !isNaN(dr) && dr > ε.

  6. modified the milestones: 3.5.x, 3.5.7 on Oct 21, 2015
  7. added 2 commits that reference this issue on Oct 23, 2015
  8. mbostock commented on Oct 23, 2015

    @mbostock
    Member

    Fix staged for 3.5.7 in #2591.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions