Javascript Infinity Object

核能气质少年 提交于 2019-12-23 06:09:37

问题


I'm caclulating the mean value of a function's request/sec, appearently the result number sometimes is too long so it displays as Infinity, is there a way to round it so it show a number only? Or make a sleep()/wait() while it's on Infinity?

well to be exactly, im monitoring req/sec on a graph, when it's infinity the line goes up not towards zero


回答1:


It's not too long to display. If you get Inf then you can't do anything with it other than know that it is something larger than the maximum possible value. This is the behavior of IEEE floating point numbers that are used in JavaScript.




回答2:


Probably the cause for this Infinity is a division by zero, not a big number.




回答3:


You are most likely unintentionally dividing by zero.

var num = 1/0;
console.log(num);
//>Infinity

Conditionally check that the divisor is not null.

You can check the maximum value of an integer as follows:

console.log([Number.MAX_VALUE, Number.MIN_VALUE]);
//>[1.7976931348623157e+308, 5e-324]

See also the official ECMA Description on Numbers



来源:https://stackoverflow.com/questions/12530647/javascript-infinity-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!