克隆的node.js项目给日志带来了一个奇怪的错误

时间:2018-02-28 13:03:08

标签: node.js

Git模块 https://github.com/eugenioclrc/agar.io-clone

> gulp run

[14:50:54] Using gulpfile C:\....\gulpfile.js
[14:50:54] Starting 'lint'...
[14:50:54] Starting 'move-client'...
[14:50:57] Finished 'move-client' after 2.84 s
[14:50:57] Finished 'lint' after 2.88 s
[14:50:57] Starting 'build-client'...
[14:50:57] Starting 'build-server'...
[14:50:57] Starting 'test'...
[14:50:57] Finished 'test' after 2.21 ms


  util.js
    #massToRadius
      V should return non-zero radius on zero input
      V should convert masses to a circle radius
[14:50:58] Finished 'build-server' after 848 ms
    #validNick
      V should allow empty player nicknames
      V should allow ascii character nicknames
      V should disallow unicode-dependent alphabets
      V should disallow spaces in nicknames
    #log
      1) should compute the log_{base} of a number
    #getDistance
      V should return a positive number


  7 passing (390ms)
  1 failing

  1) util.js #log should compute the log_{base} of a number:

      AssertionError: expected 2.0000000000000004 to deeply equal 2
      + expected - actual

      -2.0000000000000004
      +2

2 个答案:

答案 0 :(得分:1)

测试在#log @should compute the log_{base} of a number

失败

测试本身,var logNineThree = util.log(9,3);

中的问题
  describe('#log', function () {

    it('should compute the log_{base} of a number', function () {
      var base10 = util.log(1, 10),
          base2  = util.log(1, 2);

      var identity = util.log(10, 10);

      var logNineThree = util.log(9,3);

      //  log(1) should equal 0, no matter the base
      expect(base10).to.eql(base2);

      // log(n,n) === 1
      expect(identity).to.eql(1);

      // perform a trivial log calculation: 3^2 === 9
      expect(logNineThree).to.eql(2);
    });

  });

util.log

// overwrite Math.log function
exports.log = (function () {
    var log = Math.log;
    return function (n, base) {
        return log(n) / (base ? log(base) : 1);
    };
})();

结论

Safari浏览器: Math.log(3) === 1.0986122886681098
Chrome浏览器: Math.log(3) === 1.0986122886681096
MacOS Nodejs v8.2.1: Math.log(3) === 1.0986122886681096
Unix终端在线: echo 'l(3)' | bc -l === 1.0986122886681096 9139 try

可能您的操作系统/环境与原始存储库开发人员不同

答案 1 :(得分:1)

我认为问题是你的节点版本。

创建一个像这样的文件

// log-check.js
// from https://github.com/eugenioclrc/agar.io-clone/blob/master/src/server/lib/util.js 
function multibaselog(n, base) {
  return Math.log(n) / (base ? Math.log(base) : 1);
}

const log93 = multibaselog(9,3)
console.log(log93)
console.log(log93 === 2);

然后尝试使用节点4

$ nvm use 4
$ node log-check.js 
2
true

(作品)

然后尝试使用节点8

$ nvm use 8
$ node log-check.js 
2.0000000000000004
false

(没)

如果切换到早期版本的节点,我认为此测试将通过。