C#除以0为双打 - 可见 - 无限与Dividebyzeroexception [英] C# division by zero for doubles - comvisible - infinity vs. Dividebyzeroexception

查看:126
本文介绍了C#除以0为双打 - 可见 - 无限与Dividebyzeroexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家

我有一个.net COMVisible程序集,它使用双数字进行一些计算,其中还包括分区。所以让我们说这是出现这种情况:

 double a = 250.0; 
double b = 0.0;
double c = a / b;



a。)使用.net应用程序中的程序集

在这种情况下,除法结果与预期一样

 c = Infinity; 



b。)使用来自W32本机应用程序的COM的 _same _ 程序集

在这种情况下,除法产生

 DivideByZeroException 





这只有当一个组件内部x / 0.0发生时,才会发生,而不会将此结果返回(编组)回W32。[/ Edit0]

一些背景为什么这让我烦恼:我们试图从W32到.NET一步一步。在一个中间时间段,我们喜欢在遗留应用中使用新的.NET程序集[/ Edit1]



首先,这对我来说非常令人惊讶,但是我可以理解这可能与W32-主持人有关。



问题:是否有人有想法让组件始终保持相同,最好是除以0.0将得到结果 Infinity



非常感谢您提前获得一些提示。 />


我尝试了什么:



谷歌搜索了很多,但没有成功。所有计算机中的

解决方案

DivideByZeroException 是标准行为

你的.net COMVisible结果来自异常的处理, infinity 是异常处理程序推送的一个人为结果。



DivideByZeroException 是合理的,因为可以应用许多规则和会导致不同的结果:

- 任何除以它自己的结果是1:1/1 = 1,为什么不0/0 = 1?

- 零除以任何东西导致0:0/1 = 0,为什么不0/0 = 0?

- 任何除以非常小的值都会导致一个很大的值,被推到极限导致无穷大。

哪个答案是对的?



引用:

有没有人有一个想法让程序集总是一样的,最好是除以0.0会得到结果Infinity?



除非你在旧模块和新模块之间建立一个中间层来处理问题,我担心没有解决方案。


无法表示 Infinity NaN 所以你得到一个除法错误,这是正确的答案。 C#假装这种划分的实际结果就像一个快捷方式。但开发人员仍需要对其进行测试;例如,您希望以下代码中的 d e 的值是什么?

  double  a =  1  0 ; 
double b = 0 0 ;
double c = a / b;
double d = c * 2 0 ;
double e = c / 5 0 ;


Dear Experts
I have a .net COMVisible assembly which does some calculations with double numbers, among others also divisions. So let's say it comes to this situation:

double a= 250.0;
double b= 0.0;
double c= a / b;


a.) Using the assembly from a .net application
In this case the division result is like expected

c= Infinity;


b.) Using the _same_ assembly by COM from a W32 native App
In this case the division results in a

DivideByZeroException



[Edit0]This happens also only if an assembly internal x/0.0 happens, without returning (marshalling) this result back to W32.[/Edit0]
[Edit1]Some background why this bothers me: We try to move from W32 to .NET step by step. In an intermediate time slot we like to use the new .NET assemblies also in the legacy app[/Edit1]

First of all this was very surprising for me, but I can understand that this is maybe realated to W32-"Host".

Question: Does anybody have an idea to let behave the assembly always the same, preferably that a division by 0.0 will give the result Infinity?

Thank you very much in advance for some hints.

What I have tried:

Googled a lot, but without success.

解决方案

DivideByZeroException is the standard behavior in all computers.
Your ".net COMVisible" result comes from an handling of the exception and infinity is an artificial result pushed by the exception handler.

The DivideByZeroException is justified because many rules can apply and all lead to different results:
- Anything divided by itself results in 1: 1/1=1 and why not 0/0=1 ?
- Zero divided by anything results in 0: 0/1=0 and why not 0/0=0 ?
- Anything divided by a very small value result in a large value, pushed to extreme lead to infinity.
Which answer is correct?

Quote:

Does anybody have an idea to let behave the assembly always the same, preferably that a division by 0.0 will give the result Infinity?


Unless you build an intermediate layer between old and new module's to handle the problem, I fear there is no solution.


There is no way to represent Infinity or NaN in Win32 so you get a divide fault, which is the correct response. C# pretends that there is an actual result from such a division just as a shortcut. But the developer still needs to test for it; for instance what would you expect to be the values of d and e in the following code?

double a = 1.0;
double b = 0.0;
double c = a / b;
double d = c * 2.0;
double e = c / 5.0;


这篇关于C#除以0为双打 - 可见 - 无限与Dividebyzeroexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆