Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing a typeof number is Infinity?

Tags:

javascript

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:

  • typeof number + typeof number = NaN?
  • Infinity is some number in javascript?
  • Why does typeof NaN return 'number'?
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
  • Overloading Arithmetic Operators in JavaScript? (which I did not understand)
  • https://www.sharkys.com/food/menus/ (don't fault me. I was hungry. )
like image 769
davidhartman00 Avatar asked Jan 28 '26 05:01

davidhartman00


2 Answers

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('(a-b)/(b) ', (a-b)/(b))
console.log('(a - b)/b ', (a - b)/b)
console.log('typeof Infinity ', typeof Infinity)
like image 58
Ori Drori Avatar answered Jan 29 '26 18:01

Ori Drori


Probably, you divide by zero. Right?

enter image description here

like image 25
ivan.posokhin Avatar answered Jan 29 '26 18:01

ivan.posokhin