Why is 0 divided by 0 an error?

前端 未结 18 784
耶瑟儿~
耶瑟儿~ 2021-02-05 07:20

I have come across this problem in a calculation I do in my code, where the divisor is 0 if the divident is 0 too. In my code I return 0 for that case. I am wondering, while div

18条回答
  •  心在旅途
    2021-02-05 08:03

    This is what I'd do:

    function div(a, b) {
        if(b === 0 && a !== 0) {
            return undefined;
        }
        if(b === 0 && a === 0) {
            return Math.random;
        }
        return a/b;
    }
    

提交回复
热议问题