Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecstat 使用 histogram时,计算分组起始值时 存在0.1+0.2 != 0.3的 #34

Open
zhang867dong opened this issue Mar 29, 2021 · 0 comments

Comments

@zhang867dong
Copy link

如源代码中出现的rangeArray[i - 1] + step,

`function computeBins(data, optOrMethod) {
var opt = typeof optOrMethod === 'string'
? { method: optOrMethod }
: (optOrMethod || {});

    var threshold = opt.method == null
        ? thresholdMethod.squareRoot
        : thresholdMethod[opt.method];
    var dimensions = normalizeDimensions(opt.dimensions);

    var values = dataPreprocess(data, {
        dimensions: dimensions,
        toOneDimensionArray: true
    });
    var maxValue = max(values);
    var minValue = min(values);
    var binsNumber = threshold(values, minValue, maxValue);
    var tickStepResult = tickStep(minValue, maxValue, binsNumber);
    var step = tickStepResult.step;
    var toFixedPrecision = tickStepResult.toFixedPrecision;

    // return the xAxis coordinate for each bins, except the end point of the value
    var rangeArray = range(
        // use function toFixed() to avoid data like '0.700000001'
        +((Math.ceil(minValue / step) * step).toFixed(toFixedPrecision)),
        +((Math.floor(maxValue / step) * step).toFixed(toFixedPrecision)),
        step,
        toFixedPrecision
    );

    var len = rangeArray.length;

    var bins = new Array(len + 1);

    for (var i = 0; i <= len; i++) {
        bins[i] = {};
        bins[i].sample = [];
        bins[i].x0 = i > 0
            ? rangeArray[i - 1]
            : (rangeArray[i] - minValue) === step
            ? minValue
            : _(rangeArray[i] - step);_
        bins[i].x1 = i < len
            ? rangeArray[i]
            : (maxValue - rangeArray[i-1]) === step
            ? maxValue
            : _rangeArray[i - 1] + step;_
    }

    for (var i = 0; i < values.length; i++) {
        if (minValue <= values[i] && values[i] <= maxValue) {
            bins[bisect(rangeArray, values[i], 0, len)].sample.push(values[i]);
        }
    }

    var data = map(bins, function (bin) {
        // use function toFixed() to avoid data like '6.5666638489'
        return [
            +((bin.x0 + bin.x1) / 2).toFixed(toFixedPrecision),
            bin.sample.length,
            bin.x0,
            bin.x1,
            bin.x0 + ' - ' + bin.x1
        ];
    });

    var customData = map(bins, function (bin) {
        return [bin.x0, bin.x1, bin.sample.length];
    });

    return {
        bins: bins,
        data: data,
        customData: customData
    };
}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant