简体   繁体   中英

Dividing a typeof number is Infinity?

Sorry in advance is this is an obvious question. I am dividing two variables and receiving Infinity as my result. Here are the details:

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

Here are some more details:

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 is a div and bar is a table a is generated outside a function b is generated inside the function from an .on('scroll', ....)

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

I am assuming my issues comes from the typof = 'number' . In trying to find my answer in the following:

It happens because b is 0, and dividing by 0 in JS returns Infinity. In addition the type of Infinity is number.

 var a = 5 var b = 0 console.log('a ', typeof a); console.log('b ', typeof b); console.log('(ab)/(b) ', (ab)/(b)) console.log('(a - b)/b ', (a - b)/b) console.log('typeof Infinity ', typeof Infinity) 

Probably, you divide by zero. Right?

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM