Giter Club home page Giter Club logo

Comments (3)

mbostock avatar mbostock commented on May 27, 2024 1

Not sure if we can do anything better here. Feel free to open a pull request if you have a suggestion for a change to the ticks algorithm.

from d3-scale.

mbostock avatar mbostock commented on May 27, 2024 1

JavaScript has native support for big integers now, so we could use that, but I don’t think I’d want to use big integers in the common path (since it’s rare that the domain is so big or so small). And it would need to be a new major version since it would reduce compatibility of d3-array with older JavaScript environments. So I probably don’t think this is worth it unless there is more evidence of demand.

from d3-scale.

ZiedHf avatar ZiedHf commented on May 27, 2024

Yep, it's floating points issues.

// Problem
r0 + i * step;
0 + 3 * 2e+23; // This returns 5.9999999999999995e+23

It looks like using Big in the ticks method solve the issue. But maybe you don't prefer to use another dependencies to solve it ? I don't know.

Big(2e+23).mul(3).toString(); // 6e+23 Nice result !!

The ticks method

function ticks(start, stop, count) {
  var reverse,
      i = -1,
      n,
      ticks,
      step;

  stop = +stop, start = +start, count = +count;
  if (start === stop && count > 0) return [start];
  if (reverse = stop < start) n = start, start = stop, stop = n;
  if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) return [];

  if (step > 0) {
    let r0 = Math.round(start / step), r1 = Math.round(stop / step);
    console.log(start, stop, step)
    console.log(r0, r1)
    if (r0 * step < start) ++r0;
    console.log(r0, r1)
    if (r1 * step > stop) --r1;
    console.log(r0, r1)
    ticks = new Array(n = r1 - r0 + 1);
    console.log(ticks, n)
    while (++i < n) ticks[i] = Number(Big(r0).add(i).mul(step).toString());
  } else {
    step = -step;
    let r0 = Math.round(start * step), r1 = Math.round(stop * step);
    if (r0 / step < start) ++r0;
    if (r1 / step > stop) --r1;
    ticks = new Array(n = r1 - r0 + 1);
    while (++i < n) ticks[i] = Number(Big(r0).add(i).div(step).toString());
  }

  if (reverse) ticks.reverse();

  return ticks;
}

from d3-scale.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.