Why is 0 divided by 0 an error?

前端 未结 18 760
耶瑟儿~
耶瑟儿~ 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 maths rather than programming, but briefly:

    • It's in some sense justifiable to assign a 'value' of positive-infinity to some-strictly-positive-quantity / 0, because the limit is well-defined

    • However, the limit of x / y as x and y both tend to zero depends on the path they take. For example, lim (x -> 0) 2x / x is clearly 2, whereas lim (x -> 0) x / 5x is clearly 1/5. The mathematical definition of a limit requires that it is the same whatever path is followed to the limit.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-05 08:05

    If a/b = c, then a = b * c. In the case of a=0 and b=0, c can be anything because 0 * c = 0 will be true for all possible values of c. Therefore, 0/0 is undefined.

    0 讨论(0)
  • 2021-02-05 08:06

    As Andrzej Doyle said:

    Anything dived by zero is infinity. 0/0 is also infinity. You can't get 0/0 = 1. That's the basic principle of maths. That's how the whole world goes round. But you can sure edit a program to say "0/0 is not possible" or "Cannot divide by zero" as they say in cell phones.

    0 讨论(0)
  • 2021-02-05 08:09

    Since x/y=z should be equivalent to x=yz, and any z would satisfy 0=0z, how useful would such an 'exception' be?

    0 讨论(0)
  • 2021-02-05 08:10

    This is only a Logical answer not a mathamatical one, imagine Zero as empty how can you Divide an empty by an empty this is the case in division by zero also how can you divide by something which is empty.

    0 讨论(0)
提交回复
热议问题