不同的编程语言如何处理被 0 除的情况?
也许在这里问这个问题是错误的,但我很好奇。我知道许多语言在被要求除以 0 时会简单地爆炸并失败,但是有没有任何编程语言可以智能地处理这个不可能的总和 - 如果是这样,它们会做什么?他们是继续处理,将 350/0 视为 350,还是停止执行,或者什么?
Perhaps this is the wrong sort of question to ask here but I am curious. I know that many languages will simply explode and fail when asked to divide by 0, but are there any programming languages that can intelligently handle this impossible sum - and if so, what do they do? Do they keep processing, treating 350/0 as 350, or stop execution, or what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
鲜为人知的Java 编程语言给出了特殊的常量
Double。当您在 IEEE 浮点上下文中除以零时,为 POSITIVE_INFINITY
或Double.NEGATIVE_INFINITY
(取决于分子)。整数除以零是未定义的,并会导致抛出 ArithmeticException ,这与您的“爆炸和失败”场景完全不同。The little-known Java programming language gives the special constant
Double.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
(depending on the numerator) when you divide by zero in an IEEE floating-point context. Integer division by zero is undefined, and results in anArithmeticException
being thrown, which is quite different from your scenario of "explosion and failure".INTERCAL 标准库返回
#0
除以零The INTERCAL standard library returns
#0
on divide by zero来自维基百科:
扩展实数轴的无穷大可以表示为IEEE 浮点数据类型,就像 1、1.5 等普通浮点值一样。它们无论如何都不是错误值,尽管它们经常(但并非总是如此,因为这取决于舍入)在存在错误时用作替换值溢出。除以零异常时,将返回正无穷大或负无穷大作为精确结果。
From Wikipedia:
The infinities of the extended real number line can be represented in IEEE floating point datatypes, just like ordinary floating point values like 1, 1.5 etc. They are not error values in any way, though they are often (but not always, as it depends on the rounding) used as replacement values when there is an overflow. Upon a divide by zero exception, a positive or negative infinity is returned as an exact result.
在 Java 中,浮点上下文中除以零会产生特殊值
Double.POSITIVE_INFINITY
或Double.NEGATIVE_INFINITY
。In Java, division by zero in a floating-point context produces the special value
Double.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
.如果你执行 350/0,如果任何语言返回 350,我会感到惊讶。只是两个例子,但是Java抛出了一个可以捕获的异常。 C/C++ 只是崩溃(我认为它会抛出一个可能会被捕获的信号)。
i'd be surprised if any language returns 350 if you do 350/0. Just two examples, but Java throws an Exception that can be caught. C/C++ just crashes (i think it throws a Signal that can probably be caught).
在 Delphi 中,它要么抛出一个编译时错误(如果除以 0 值常量),要么抛出一个可捕获的运行时错误(如果它发生在运行时)。
对于 C 和 C++ 来说也是一样的。
在 PHP 中你会收到一个警告:
因此,在 PHP 中,对于类似以下内容:
$i 将被设置为空。 BUT $i 不 === NULL,isset($i) 返回 true,is_string($i) 返回 false。
In Delphi, it either throw a compile-time error (if divided by a 0 value const) or a catchable runtime error if it happens at runtime.
It's the same for C and C++.
In PHP you will get a warning:
So, in PHP, for something like:
$i will be set to nothing. BUT $i is not === NULL and isset($i) returns true and is_string($i) returns false.
Python(至少版本2,我没有版本3)抛出一个ZeroDivisionError,它可以被捕获。
打印出:
Python (at least version 2, I don't have 3) throws a ZeroDivisionError, which can be caught.
prints out:
大多数 SQL 实现都会引发“除以零”错误,但 MySQL 只是返回 NULL
Most SQL implementations raise a "division by zero" error, but MySQL just returns NULL
浮点数按照 IEEE 定义常量 NaN 等。任何涉及该值的连续操作将保持不变,直到结束。整数或整数是不同的,抛出异常......在java中......
Floating point numbers as per the IEEE define constants NaN etc. Any continued operation involving thst value will remain unchanged until the end. Integer or whole numbers are different with exceptions being thrown...In java...
在小马中,除以 0 是 0,但我还没有找到一种语言,其中 0/0 是 1
In pony division by 0 is 0 but i have yet to find a language where 0/0 is 1
我正在使用 Polyhedra 并尝试选择一种喜欢 inf 的语言。
多面体 {a,b} 的总边数为,其中 a 是每个多边形的边数,b 是每个角的边数:
E = 1/(1/a + 1/b - 1/2)
如果 E 为负,则为负曲率,但如果 E 为无穷大 (1/0),则平铺平面。示例:{3,6} {4,4}
I'm working with polyhedra and trying to choose a language that likes inf.
The total edges for a polyhedron {a,b} where a is edges per polygon and b is edges per corner is
E = 1/(1/a + 1/b - 1/2)
if E is negative it's a negative curvature, but if E is infinity (1/0) it tiles the plane. Examples: {3,6} {4,4}