为什么 0 除以 0 会出错?

发布于 2024-09-09 10:03:45 字数 358 浏览 11 评论 0原文

我在代码中进行的计算中遇到了这个问题,如果除数也为 0,则除数为 0。在我的代码中,对于这种情况我返回 0。我想知道,虽然除以零通常是未定义的,但为什么不为这种情况破例呢?我的理解为什么除以零是未定义的基本上是它不能逆转。然而,我在 0/0 的情况下没有看到这个问题。

编辑好的,所以这个问题引发了很多讨论。我犯了一个错误,因为它收到了很多选票,所以过于急切地接受了它。我现在接受了 AakashM 的回答,因为它提供了如何分析问题的想法。

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 division by zero is generally undefined, why not make an exception for this case? My understanding why division by zero is undefined is basically that it cannot be reversed. However, I do not see this problem in the case 0/0.

EDIT OK, so this question spawned a lot of discussion. I made the mistake of over-eagerly accepting an answer based on the fact that it received a lot of votes. I now accepted AakashM's answer, because it provides an idea on how to analyze the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

瑶笙 2024-09-16 10:03:45

比方说:

0/0 = x

现在,重新排列方程(两边都乘以 0)得出:

x * 0 = 0

现在你看到问题了吗? x 有无数个值,因为任何乘以 0 的值都是 0。

Let's say:

0/0 = x

Now, rearranging the equation (multiplying both sides by 0) gives:

x * 0 = 0

Now do you see the problem? There are an infinite number of values for x as anything multiplied by 0 is 0.

笨死的猪 2024-09-16 10:03:45

这是数学而不是编程,但简单地说:

  • 在某种意义上将正无穷大“值”分配给some-strictly-positive-quantity / 0是合理的,因为极限是定义明确

  • 然而,x / y 的极限为 xy 都趋向于零取决于它们所采取的路径 。例如,lim (x -> 0) 2x / x 显然是 2,而 lim (x -> 0) x / 5x 显然是 1/5。极限的数学定义要求无论通过什么路径到达极限,它都是相同的。

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.

余生共白头 2024-09-16 10:03:45

(受到托尼·布雷亚尔(Tony Breyal)在我自己的帖子中发表的相当好的答案的启发)

零是一个棘手而微妙的野兽 - 它不符合我们所知的代数通常规律。

零除以任何数字(零本身除外)都为零。用更数学的方式来说:

 0/n = 0      for all non-zero numbers n.

当你尝试除以零本身时,你就会进入棘手的领域。除以 0 的数字并不总是未定义。这取决于问题。我将为您提供一个微积分示例,其中定义了数字 0/0。

假设我们有两个函数,f(x) 和 g(x)。如果你取它们的商 f(x)/g(x),你会得到另一个函数。我们称之为 h(x)。

您还可以限制功能。例如,当 x 趋近 2 时,函数 f(x) 的极限是该函数在 x 接近 2 时最接近的值。我们可以将此极限写为:

 lim{x->2} f(x) 

这是一个非常直观的概念。只需绘制函数的图表,然后沿着它移动铅笔即可。当 x 值接近 2 时,查看函数的走向。

现在以我们的例子为例。让:

 f(x) = 2x - 2
 g(x) = x - 1

并考虑它们的商:

 h(x) = f(x)/g(x)

如果我们想要 lim{x->1} h(x) 怎么办?有定理说,

 lim{x->1} h(x) = lim{x->1} f(x) / g(x) 
                = (lim{x->1} f(x)) / (lim{x->1} g(x))  
                = (lim{x->1} 2x-2) / (lim{x->1} x-1)
                =~ [2*(1) - 2] / [(1) - 1]  # informally speaking...
                = 0 / 0 
                  (!!!)

所以我们现在有:

 lim{x->1} h(x) = 0/0

但是我可以采用另一个定理,称为l'Hopital 规则,它告诉我这个极限也等于 2。所以在这种情况下,0/0 = 2(我没有告诉过你这是一个奇怪的野兽吗?)

这里还有 0 的另一点奇怪之处。假设 0/0 遵循古老的代数规则,即任何除以自身的值都是 1然后你可以做以下证明:

我们得到:

 0/0 = 1

现在两边都乘以任意数n。

 n * (0/0) = n * 1

两边化简:

 (n*0)/0 = n 
 (0/0) = n 

同样,使用 0/0 = 1 的假设:

 1 = n 

所以我们刚刚证明了所有其他数字 n 都等于 1!所以 0/0 不能等于 1。

走回 mathoverflow.com 上的家

(Was inspired by Tony Breyal's rather good answer to post one of my own)

Zero is a tricky and subtle beast - it does not conform to the usual laws of algebra as we know them.

Zero divided by any number (except zero itself) is zero. Put more mathematically:

 0/n = 0      for all non-zero numbers n.

You get into the tricky realms when you try to divide by zero itself. It's not true that a number divided by 0 is always undefined. It depends on the problem. I'm going to give you an example from calculus where the number 0/0 is defined.

Say we have two functions, f(x) and g(x). If you take their quotient, f(x)/g(x), you get another function. Let's call this h(x).

You can also take limits of functions. For example, the limit of a function f(x) as x goes to 2 is the value that the function gets closest to as it takes on x's that approach 2. We would write this limit as:

 lim{x->2} f(x) 

This is a pretty intuitive notion. Just draw a graph of your function, and move your pencil along it. As the x values approach 2, see where the function goes.

Now for our example. Let:

 f(x) = 2x - 2
 g(x) = x - 1

and consider their quotient:

 h(x) = f(x)/g(x)

What if we want the lim{x->1} h(x)? There are theorems that say that

 lim{x->1} h(x) = lim{x->1} f(x) / g(x) 
                = (lim{x->1} f(x)) / (lim{x->1} g(x))  
                = (lim{x->1} 2x-2) / (lim{x->1} x-1)
                =~ [2*(1) - 2] / [(1) - 1]  # informally speaking...
                = 0 / 0 
                  (!!!)

So we now have:

 lim{x->1} h(x) = 0/0

But I can employ another theorem, called l'Hopital's rule, that tells me that this limit is also equal to 2. So in this case, 0/0 = 2 (didn't I tell you it was a strange beast?)

Here's another bit of weirdness with 0. Let's say that 0/0 followed that old algebraic rule that anything divided by itself is 1. Then you can do the following proof:

We're given that:

 0/0 = 1

Now multiply both sides by any number n.

 n * (0/0) = n * 1

Simplify both sides:

 (n*0)/0 = n 
 (0/0) = n 

Again, use the assumption that 0/0 = 1:

 1 = n 

So we just proved that all other numbers n are equal to 1! So 0/0 can't be equal to 1.

walks on back to her home over at mathoverflow.com

北笙凉宸 2024-09-16 10:03:45

这是完整的解释:

http://en.wikipedia.org/wiki/Division_by_zero

(包括证明 1 = 2 :-) )

您通常在编程中通过使用 if 语句来处理此问题,以获得应用程序所需的行为。

Here's a full explanation:

http://en.wikipedia.org/wiki/Division_by_zero

( Including the proof that 1 = 2 :-) )

You normally deal with this in programming by using an if statement to get the desired behaviour for your application.

不弃不离 2024-09-16 10:03:45

问题出在分母上。分子实际上是不相关的。

10 / n
10 / 1 = 10
10 / 0.1 = 100
10 / 0.001 = 1,000
10 / 0.0001 = 10,000
Therefore: 10 / 0 = infinity (in the limit as n reaches 0)

模式是,随着 n 变小,结果会变大。当 n = 0 时,结果为无穷大,这是一个不稳定或非固定点。你不能把无穷大写成一个数字,因为它不是,它是一个不断增加的数字的概念

否则,您可以使用对数定律在数学上思考它,从而从方程中取出除法:

    log(0/0) = log(0) - log(0)

但是

    log(0) = -infinity

同样,问题是结果是未定义的,因为它是一个概念,而不是您可以输入的数字。

说了这么多,如果您对如何将不确定形式转化为确定形式感兴趣,请查阅 l'Hopital 规则,该规则有效地表示:

f(x) / g(x) = f'(x) / g'(x)

假设存在极限,因此您可以获得一个固定点的结果而不是不稳定点。

希望能有所帮助,

托尼​​·布雷亚尔

( Tony Breyal)使用对数规则通常是解决执行运算问题的一种很好的计算方法,这些运算会导致数字非常小,以至于给定机器浮点值的精度,与零无法区分。实际编程示例是“最大似然”,通常必须利用日志才能保持解决方案的稳定

The problem is with the denominator. The numerator is effectively irrelevant.

10 / n
10 / 1 = 10
10 / 0.1 = 100
10 / 0.001 = 1,000
10 / 0.0001 = 10,000
Therefore: 10 / 0 = infinity (in the limit as n reaches 0)

The Pattern is that as n gets smaller, the results gets bigger. At n = 0, the result is infinity, which is a unstable or non-fixed point. You can't write infinity down as a number, because it isn't, it's a concept of an ever increasing number.

Otherwise, you could think of it mathematically using the laws on logarithms and thus take division out of the equation altogther:

    log(0/0) = log(0) - log(0)

BUT

    log(0) = -infinity

Again, the problem is the the result is undefined because it's a concept and not a numerical number you can input.

Having said all this, if you're interested in how to turn an indeterminate form into a determinate form, look up l'Hopital's rule, which effectively says:

f(x) / g(x) = f'(x) / g'(x)

assuming the limit exists, and therefore you can get a result which is a fixed point instead of a unstable point.

Hope that helps a little,

Tony Breyal

P.S. using the rules of logs is often a good computational way to get around the problems of performing operations which result in numbers which are so infinitesimal small that given the precision of a machine’s floating point values, is indistinguishable from zero. Practical programming example is 'maximum likelihood' which generally has to make use of logs in order to keep solutions stable

财迷小姐 2024-09-16 10:03:45

逆向看除法:如果 a/b = c,则 c*b = a。现在,如果替换 a=b=0,则最终得到 c*0 = 0。但是任何乘以零都等于零,因此结果可以是任何值。你希望 0/0 为 0,其他人可能希望它为 1(例如,当 x 接近 0 时,sin(x)/x 的极限值为 1)。所以最好的解决方案是保持未定义并报告错误。

Look at division in reverse: if a/b = c then c*b = a. Now, if you substitute a=b=0, you end up with c*0 = 0. But ANYTHING multiplied by zero equals zero, so the result can be anything at all. You would like 0/0 to be 0, someone else might like it to be 1 (for example, the limiting value of sin(x)/x is 1 when x approaches 0). So the best solution is to leave it undefined and report an error.

黯淡〆 2024-09-16 10:03:45

您可能想看看博士。詹姆斯·安德森 (James Anderson) 的跨算术工作。它没有被广泛接受。

Transarithmetic 引入术语/数字“Nullity”来取 0/0 的值,James 将其比作引入“i”和“j”。

You may want to look at Dr. James Anderson's work on Transarithmetic. It isn't widely accepted.

Transarithmetic introduces the term/number 'Nullity' to take the value of 0/0, which James likens to the introduction 'i' and 'j'.

柳若烟 2024-09-16 10:03:45

现代数学的结构是由根据公理思考的数学家设定的。
拥有额外的公理是没有生产力的,并且实际上不允许人们做更多的事情,这与拥有清晰的数学的理想背道而驰。

The structure of modern math is set by mathematicians who think in terms of axioms.
Having additional axioms that aren't productive and don't really allow one to do more stuff is against the ideal of having clear math.

沉睡月亮 2024-09-16 10:03:45

0 变成 0 有多少次? 5. 是 - 5 * 0 = 0, 11. 是 - 11 * 0 = 0, 43. 是 - 43 * 0 = 0. 也许您可以明白为什么现在它是未定义的? :)

How many times does 0 go into 0? 5. Yes - 5 * 0 = 0, 11. Yes - 11 * 0 = 0, 43. Yes - 43 * 0 = 0. Perhaps you can see why it's undefined now? :)

坠似风落 2024-09-16 10:03:45

由于x/y=z应该等于x=yz,并且任何z都满足0=0z ,这样的“例外”有多大用处?

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

无法言说的痛 2024-09-16 10:03:45

对于 0/0 未定义的另一个解释是,您可以这样写:

0/0 = (4 - 4)/0 = 4/0 - 4/0

并且 4/0 未定义。

Another explanation of why 0/0 is undefined is that you could write:

0/0 = (4 - 4)/0 = 4/0 - 4/0

And 4/0 is undefined.

梦明 2024-09-16 10:03:45

如果 a/b = c,则 a = b * c。
在 a=0 和 b=0 的情况下,c 可以是任何值,因为 0 * c = 0 对于 c 的所有可能值都成立。因此,0/0 是未定义的。

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.

熊抱啵儿 2024-09-16 10:03:45

这只是一个逻辑答案,而不是数学答案,
想象零为空,你怎么能用一个空除以一个空,除以零就是这种情况,你怎么能除以一个空的东西。

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.

椵侞 2024-09-16 10:03:45

0意味着什么都没有,所以如果你什么都没有,并不意味着向任何东西分配给任何东西。一些公交设施在列出特定线路的班次时,班次编号 0 通常是以不同方式路由的特殊路线。通常,托伦斯交通系统就是一个很好的例子,其中 2 号线在第一个行程之前有一个行程,称为 0 号行程,仅在工作日运行,该行程特别是 0 号行程,因为它是一条专门路由的路线与所有其他路线不同。

详细信息请参见以下网页:
http://transit.torrnet.com/PDF/Line-2_MAP.pdf
http://transit.torrnet.com/PDF/Line-2_Time_PDF.pdf

在地图上,行程编号 0 是虚线映射的行程,实线映射常规路线。

有时,可以使用 0 对被视为“特快服务”路线的路线所经过的行程进行编号。

0 means nothing, so if you have nothing, it does not imply towards anything to distribute to anything. Some Transit Facilities when they list out the number of trips of a particular line, trip number 0 is usually the special route that is routed in a different way. Typically, a good example would be in the Torrance Transit Systems where Line 2 has a trip before the first trip known as trip number 0 that operates on weekdays only, that trip in particular is trip number 0 because it is a specialized route that is routed differently from all the other routes.

See the following web pages for details:
http://transit.torrnet.com/PDF/Line-2_MAP.pdf
http://transit.torrnet.com/PDF/Line-2_Time_PDF.pdf

On the map, trip number 0 is the trip that is mapped in dotted line, the solid line maps the regular routing.

Sometimes 0 can be used on numbering the trips a route takes where it is considered the "Express Service" route.

快乐很简单 2024-09-16 10:03:45

为什么不破例呢
案例?

因为:

  • 正如其他人所说,这并不那么容易;)
  • 没有定义 0/0 的应用程序 - 添加异常会使数学变得复杂而没有任何好处。

why not make an exception for this
case?

Because:

  • as others said, it's not that easy;)
  • there's no application for defining 0/0 - adding exception would complicate mathematics for no gains.
翻身的咸鱼 2024-09-16 10:03:45

这就是我要做的:

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

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;
}
你与清晨阳光 2024-09-16 10:03:45

当您输入零除以零时,会出现错误,因为无论您将零与零相乘,都将为零,因此它可以是任何数字。

When you type in zero divided by zero, there's an error because whatever you multiply zero from will be zero so it could be any number.

青芜 2024-09-16 10:03:45

正如安杰伊·多伊尔(Andrzej Doyle)所说:

任何低于零的东西都是无穷大。 0/0 也是无穷大。你不可能得到 0/0 = 1。这是数学的基本原理。整个世界就是这样运转的。但你肯定可以编辑一个程序来表示“0/0 不可能”或“不能除以零”,就像手机中所说的那样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文