Skip to content

Commit

Permalink
Fix rounding error.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 15, 2015
1 parent d61e79b commit 0087f90
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/exponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function(value) {
return Math.floor(Math.log(value) / Math.LN10 + 1e-12);
};
8 changes: 2 additions & 6 deletions src/formatAutoPrefix.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import btod from "./btod";

export var exponent;

export function exponentOf(value) { // Fast approximation.
return Math.max(-8, Math.min(8, Math.floor((Math.log(value) / (Math.LN10 * 3) + 1e-12)))) * 3;
};
export var prefixExponent;

export default function(x, p) {
var d = btod(x, p);
Expand All @@ -15,7 +11,7 @@ export default function(x, p) {
else i %= 3;
if (!i) i = 3;
else if (i < 0) i += 3;
exponent = Math.max(-24, Math.min(24, Math.floor((d.exponent - 1) / 3) * 3));
prefixExponent = Math.max(-24, Math.min(24, Math.floor((d.exponent - 1) / 3) * 3));
return d.coefficient.slice(0, i)
+ (d.coefficient.length > i
? "." + d.coefficient.slice(i)
Expand Down
7 changes: 4 additions & 3 deletions src/localeFormat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import exponent from "./exponent";
import formatGroup from "./formatGroup";
import formatSpecifier from "./formatSpecifier";
import formatTypes from "./formatTypes";
import {exponent, exponentOf} from "./formatAutoPrefix";
import {prefixExponent} from "./formatAutoPrefix";

var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];

Expand Down Expand Up @@ -62,7 +63,7 @@ export default function(locale) {
value = formatType(value, precision);

// Compute the suffix.
var valueSuffix = suffix + (type === "s" ? prefixes[8 + exponent / 3] : "");
var valueSuffix = suffix + (type === "s" ? prefixes[8 + prefixExponent / 3] : "");

// Break the formatted value into the integer “value” part that can be
// grouped, and fractional or exponential “suffix” part that is not.
Expand Down Expand Up @@ -102,7 +103,7 @@ export default function(locale) {

function formatPrefix(specifier, value) {
var f = format((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
e = exponentOf(value),
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
k = Math.pow(10, -e),
prefix = prefixes[8 + e / 3];
return function(value) {
Expand Down
4 changes: 3 additions & 1 deletion src/precisionFixed.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import exponent from "./exponent";

export default function(step) {
return Math.max(0, -Math.floor(Math.log(Math.abs(step)) / Math.LN10));
return Math.max(0, -exponent(Math.abs(step)));
};
4 changes: 2 additions & 2 deletions src/precisionPrefix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import exponent from "./exponent";
import precisionFixed from "./precisionFixed";
import {exponentOf} from "./formatAutoPrefix";

export default function(step, value) {
return Math.max(0, exponentOf(value) - Math.floor(Math.log(Math.abs(step)) / Math.LN10));
return Math.max(0, Math.floor(exponent(value) / 3) * 3 - exponent(Math.abs(step)));
};
4 changes: 3 additions & 1 deletion src/precisionRound.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import exponent from "./exponent";

export default function(step, max) {
return Math.max(0, Math.floor(Math.log(Math.abs(max)) / Math.LN10) - Math.floor(Math.log(Math.abs(step)) / Math.LN10)) + 1;
return Math.max(0, exponent(Math.abs(max)) - exponent(Math.abs(step))) + 1;
};

0 comments on commit 0087f90

Please sign in to comment.