Math.log2 precision has changed in Chrome

后端 未结 2 653
栀梦
栀梦 2021-02-13 10:51

I\'ve written a JavaScript program that calculates the depth of a binary tree based on the number of elements. My program has been working fine for months, but recently I\'ve f

2条回答
  •  迷失自我
    2021-02-13 11:02

    You could perhaps do this instead

    // Math.log2(n_elements) to 10 decimal places
    var tree_depth = Math.floor(Math.round(Math.log2(n_elements) * 10000000000) / 10000000000);
    

提交回复
热议问题