0
d3.scale.linear()
  .domain([-0.505, 1])
  .nice()
  .domain();

returns this

[-0.6000000000000001, 1]

But what I would really like is this

[-0.6, 1]

Edit:

As mentioned in comments, the cause of this explained in Is floating point math broken?.

This question is about the best way to get the desired result in the context of d3. Maybe the best approach is simply string formatting. But, I'd like to see if anyone has a novel solution to this particular case.

Community
  • 1
  • 1
dstreit
  • 716
  • 1
  • 7
  • 8
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user2864740 Jul 07 '15 at 01:28
  • This question doesn't involve math directly but the cause/result is the same. – user2864740 Jul 07 '15 at 01:29
  • (The closest value that one *could* get to 0.6 is 0.59999999999999997780, but the delta between this and the reported number is irrelevant. For display purposes use `Number.toFixed` or a similar string formatting to round away the tiny tiny delta differences.) – user2864740 Jul 07 '15 at 02:00

1 Answers1

0

Try converting to string with fixed precision and back to number:

d3.scale.linear()
  .domain([-0.505, 1])
  .nice()
  .domain()
  .map(function(val) { return +val.toFixed(10); });

this yields

[-0.6, 1]
prototype
  • 7,249
  • 15
  • 60
  • 94