Javascript bigint分部

时间:2017-11-19 20:01:51

标签: javascript numbers

我正在尝试决定是否需要bigint lib或正常的js数字就足够了。我想将文件大小除以块大小(最大为100GB)以获得我有多少块:HTMLWidgets.widget({ name: 'IMposterior', type: 'output', factory: function(el, width, height) { // TODO: define shared variables for this instance return { renderValue: function(opts) { console.log("MME: ", opts.MME); console.log("threshold: ", opts.threshold); console.log("prob: ", opts.prob); console.log("colors: ", opts.colors); var margin = {left:50,right:50,top:40,bottom:0}; var xMax = opts.x.reduce(function(a, b) { return Math.max(a, b); }); var yMax = opts.y.reduce(function(a, b) { return Math.max(a, b); }); var xMin = opts.x.reduce(function(a, b) { return Math.min(a, b); }); var yMin = opts.y.reduce(function(a, b) { return Math.min(a, b); }); var y = d3.scaleLinear() .domain([0,yMax]) .range([height,0]); var x = d3.scaleLinear() .domain([xMin,xMax]) .range([0,width]); var yAxis = d3.axisLeft(y); var xAxis = d3.axisBottom(x); var area = d3.area() .x(function(d,i){ return x(opts.x[i]) ;}) .y0(height) .y1(function(d){ return y(d); }); var svg = d3.select(el).append('svg').attr("height","100%").attr("width","100%"); var chartGroup = svg.append("g").attr("transform","translate("+margin.left+","+margin.top+")"); chartGroup.append("path") .attr("d", area(opts.y)); chartGroup.append("g") .attr("class","axis x") .attr("transform","translate(0,"+height+")") .call(xAxis); }, resize: function(width, height) { // TODO: code to re-render the widget with a new size } }; } }); 。我记得js在精确划分方面存在问题。

我在结果中获得howManyChunks = Math.ceil(fileSize/chunkSize)而不是x+1 = Math.ceil(x.00000000000..01)的机会是什么?

1 个答案:

答案 0 :(得分:0)

我决定尝试一下。正常的js数字的精度在3-4PB文件大小附近开始不够。因此在Firefox中,js整数的精度约为55位。我想这可能因浏览器而异,但我需要这个用于nodejs。

console.log("start");

var B = 8;
var KB = 1024*B;
var MB = 1024*KB;
var GB = 1024*MB;
var TB = 1024*GB;
var PB = 1024*TB;
var EB = 1024*PB;

var maxBits = 4 * PB;
var bitSteps = 100 * TB;
var maxChunkSize = 333 * B;

for (var bits = 0; bits <= maxBits; bits += bitSteps)
    for (var chunkSize = 1; chunkSize <= maxChunkSize; ++chunkSize) {
        var calculatedChunkCount = Math.ceil(bits/chunkSize);
        var calculatedBits = calculatedChunkCount * chunkSize;
        var difference = calculatedBits - bits;
        var error = difference>= chunkSize || difference<0;
        if (error)
            console.log({
                chunkSize: chunkSize,
                calculatedChunkCount: calculatedChunkCount,
                bits: bits,
                calculatedBits: calculatedBits,
                difference: difference
            });
    }

console.log("end");

输出:

{ chunkSize: 97, calculatedChunkCount: 290180388361501, bits: 28147497671065600, calculatedBits: 28147497671065596, difference: -4 }
{ chunkSize: 1579, calculatedChunkCount: 20611490932343, bits: 32545544182169600, calculatedBits: 32545544182169596, difference: -4 }

我会在晚上启动并行nodejs脚本以确保,但我认为在0 - 100GB范围内精度足够。

根据另一个答案:https://stackoverflow.com/a/2803010/607033 js整数精确到53位。所以这个除法限制可能接近那个值,可能会低一些。