nan和infinity_NaN,Infinity和VB.NET中的零除

nan和infinity

Beginning programming books usually include this warning: "Don't divide by zero! You'll get a runtime error!"

入门的编程书籍通常包含以下警告:“不要被零除!您会收到运行时错误!”

Things have changed in VB.NET. Although there are more programming options and the calculation is more accurate, it isn't always easy to see why things happen the way they do.

VB.NET发生了变化。 尽管有更多的编程选项并且计算更加准确,但要了解为什么事情以它们的方式发生并不总是那么容易。

Here, we learn how to handle division by zero using VB.NET's structured error handling. And along the way, we also cover the new VB.NET constants: NaN, Infinity, and Epsilon.

在这里,我们学习如何使用VB.NET的结构化错误处理来处理零除。 在此过程中,我们还将介绍新的VB.NET常量:NaN,Infinity和Epsilon。

如果在VB.NET中运行“除以零”会发生什么 ( What Happens If You Run 'Divide By Zero' in VB.NET )

If you run a 'divide by zero' scenario in VB.NET, you get this result:

如果在VB.NET中运行“被零除”方案,则会得到以下结果:

 Dim a, b, c As Double
 a = 1 : b = 0
 c = a / b
 Console.WriteLine( _
 "Have math rules" _
 & vbCrLf & _
 "been repealed?" _
 & vbCrLf & _
 "Division by zero " _
 & vbCrLf & _
 "must be possible!") 

So what's going on here? The answer is that VB.NET actually gives you the mathematically correct answer. Mathematically, you can divide by zero, but what you get is "infinity".

那么这是怎么回事? 答案是VB.NET实际上为您提供了数学上正确的答案。 从数学上讲,您可以除以零,但是得到的是“无穷大”。

 Dim a, b, c As Double
 a = 1 : b = 0
 c = a / b
 Console.WriteLine( _
 "The answer is: " _
 & c)
 ' Displays:
 ' The answer is: infinity 

The value "infinity" isn't too useful for most business applications. (Unless the CEO is wondering what the upper limit on his stock bonus is.) But it does keep your applications from crashing on a runtime exception like less powerful languages do.

值“ infinity”对于大多数业务应用程序不太有用。 (除非首席执行官想知道他的股票红利上限是多少。)但是,它确实使您的应用程序不会像功能较弱的语言那样在运行时异常时崩溃。

VB.NET gives you even more flexibility by even allowing you to perform calculations. Check this out:

VB.NET甚至允许您执行计算,从而为您提供了更大的灵活性。 看一下这个:

 Dim a, b, c As Double
 a = 1 : b = 0
 c = a / b
 c = c + 1
 ' Infinity plus 1 is
 ' still infinity 

To remain mathematically correct, VB.NET gives you the answer NaN (Not a Number) for some calculations such as 0 / 0.

为了保持数学上的正确性,VB.NET为某些计算(例如0/0)提供了答案NaN(非数字)。

 Dim a, b, c As Double
 a = 0 : b = 0
 c = a / b
 Console.WriteLine( _
 "The answer is: " _
 & c)
 ' Displays:
 ' The answer is: NaN 

VB.NET can also tell the difference between positive infinity and negative infinity:

VB.NET还可以分辨出正无穷大和负无穷大:

 Dim a1, a2, b, c As Double
 a1 = 1 : a2 = -1 : b = 0
 If (a1 / b) > (a2 / b) Then _
 Console.WriteLine( _
 "Postive infinity is" _
 & vbCrLf & _
 "greater than" _
 & vbCrLf & _
 "negative infinity.") 

In addition to PositiveInfinity and NegativeInfinity, VB.NET also provides Epsilon, the smallest positive Double value greater than zero.

除了PositiveInfinity和NegativeInfinity,VB.NET还提供Epsilon,它是大于零的最小正Double值。

Keep in mind that all of these new capabilities of VB.NET are only available with floating point (Double or Single) data types. And this flexibility can lead to some Try-Catch-Finally (structured error handling) confusion. For example, the .NET code above runs without throwing any kind of exception, so coding it inside a Try-Catch-Finally block won't help. To test for a divide by zero, you would have to code a test something like:

请记住,VB.NET的所有这些新功能仅适用于浮点(双精度或单精度)数据类型。 而且这种灵活性可能会导致最终的“尝试-捕获-最终”(结构化错误处理)混乱。 例如,上面的.NET代码在运行时不会引发任何异常,因此在Try-Catch-Finally块中进行编码将无济于事。 要测试除零,您必须编写如下测试代码:

 If c.ToString = "Infinity" Then ... 

Even if you code the program (using Integer instead of Single or Double types), you still get an "Overflow" Exception, not a "Divide by Zero" exception. If you search the web for other technical help, you will notice that the examples all test for OverflowException.

即使您对程序进行编码(使用Integer而不是Single或Double类型),您仍然会收到“溢出”异常,而不是“被零除”异常。 如果您在网络上搜索其他技术帮助,则会注意到所有示例都测试了OverflowException。

.NET actually has the DivideByZeroException as a legitimate type. But if the code never triggers the exception, when will you ever see this elusive error?

.NET实际上将DivideByZeroException作为合法类型。 但是,如果代码从不触发异常,什么时候会看到这个难以捉摸的错误?

何时看到DivideByZeroException ( When You'll See DivideByZeroException )

As it turns out, Microsoft's MSDN page about Try-Catch-Finally blocks actually uses a divide by zero examples to illustrate how to code them. But there's a subtle "catch" that they don't explain. Their code looks like this:

事实证明, Microsoft的有关Try-Catch-Finally块的MSDN页面实际上使用除以零的示例来说明如何对其进行编码。 但是有一个微妙的“陷阱”,他们没有解释。 他们的代码如下所示:

 Dim a As Integer = 0
 Dim b As Integer = 0
 Dim c As Integer = 0
 Try
    a = b \ c
 Catch exc As Exception
    Console.WriteLine("A run-time error occurred")
 Finally
    Console.ReadLine()
 End Try 

This code does trigger an actual divide by zero exception.

该代码确实触发了实际的零除异常。

But why does this code trigger the exception and nothing we've coded before does? And what is Microsoft not explaining?

但是,为什么这段代码会触发异常,而我们之前编写的代码却没有触发呢? 微软没有解释什么?

Notice that the operation they use is not divide ("/"), it's integer divide ("\")! (Other Microsoft examples actually declare the variables as Integer.) As it turns out, integer calculation is the only case that actually throws that exception. It would have been nice if Microsoft (and the other pages that copy their code) explained that little detail.

注意,他们使用的运算符不是除法(“ /”),而是整数除法(“ \”)! (其他Microsoft示例实际上将变量声明为Integer。)事实证明,整数计算是唯一实际引发该异常的情况。 如果Microsoft(以及复制其代码的其他页面)解释了这么少的细节,那将是很好的。

翻译自: https://www.thoughtco.com/nan-infinity-and-divide-by-zero-3424193

nan和infinity

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值