【问题标题】:Dividing a typeof number is Infinity?除以一个类型的数字是无穷大吗?
【发布时间】:2018-04-07 00:11:17
【问题描述】:

抱歉,这是一个显而易见的问题。我正在划分两个变量并接收 Infinity 作为我的结果。以下是详细信息:

typeof a //'number'
typeof b //'number'
typeof (a-b) //'number'
typeof ((a-b)/(b)) //'number'
     a - b = xxx.xxxxx //this works
     (a - b)/b = Infinity

这里有更多细节:

a and b are five decimal places (XXX.XXXXX)
// the variables are generated from ....
var z = document.getElementById('foo').getBoundingClientRect()
var y = document.getElementById('bar').getBoundingClientRect()
var a = z.x
var b = y.x

foo 是一个 div,bar 是一个表格 a 在函数外部生成 b 是在函数内部从 .on('scroll', ....) 生成的

<div id="foo">
  <table id='bar'>
  </table>
</div>

我假设我的问题来自typof = 'number'。 试图在以下内容中找到我的答案:

【问题讨论】:

  • 问题是什么?
  • 这个怎么样...typeof NaN === "number"!我是不是让你大吃一惊?此外,JavaScript 没有 Division by Zero 错误。相反,你会得到infinity
  • typeof(Infinity) 等于 number 所以((a-b)/(b)) 也可能评估为Infinity

标签: javascript


【解决方案1】:

这是因为b 为 0,在 JS 中除以 0 返回 Infinity。另外Infinity的类型是数字。

var a = 5
var b = 0

console.log('a ', typeof a);
console.log('b ', typeof b);
console.log('(a-b)/(b) ', (a-b)/(b))
console.log('(a - b)/b ', (a - b)/b)
console.log('typeof Infinity ', typeof Infinity)

【讨论】:

    【解决方案2】:

    很可能,你除以零。对吧?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-17
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      相关资源
      最近更新 更多