分割为0时如何检查无穷大

时间:2016-04-27 09:38:35

标签: javascript html

我想检查给定的结果是否等于无穷大。我怎样才能正确检查它?

<input type="text"  name="answer" id="t"  onkeyup="withoutLat(this);withoutCyr(this);checkInfinity(this);" placeholder="Enter data" autofocus>

        function checkInfinity(value)
    {

        var value = calculator.answer.value; 

        if (value == Number.POSITIVE_INFINITY || value == Number.NEGATIVE_INFINITY)
        {
            alert("Помилка ділення на нуль")
        }
    }

2 个答案:

答案 0 :(得分:3)

isFinite(12) //true
isFinite(12/0) //false

答案 1 :(得分:0)

var result = 10 / 0;

if (result === Infinity) {
   alert('Atata!')
}

检查出来

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Infinity

和jsfidle为你

https://jsfiddle.net/6tzvr1na/