由于类型转换潜在的算术错误

由于类型转换潜在的算术错误

本文介绍了如何被警告,由于类型转换潜在的算术错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个计算模块上工作使用C#,我撞到这个:

I am working on a calculation module using C#, and I bumped on this :

double v = 4 / 100;



我知道这是一个错误的初始化,返回 V = 0.0 ,而不是 v = 0.04

C#的规则,说我必须确保至少有一个该成员是双,是这样的:

The c# rules says I must ensure at least one of the member is a double, like this :

double v = (double) 4 / 100;
double v = 4.0 / 100;



不过,我有那种涉及整型变量的操作很多很多的初始化,我觉得懒得浏览我的代码一行一行来检测这样的错误。

However, I have many many initializations of that kind that involves integer variables operations, and I feel lazy to browse my code line by line to detect such mistakes.

相反,是有可能得到由这个编译器警告?

推荐答案

好吧,一些玩耍并没有什么后,我有一个解决方案。我用来到这个解决方案。我使用,所以你需要获得并安装。然后,您可以下载我的C#项目。

Alright, after some playing around and what not, I have a solution. I used this article to come to this solution.I use StyleCop, so you'll need to get and install that. Then, you can download my C# project MathematicsAnalyzer.

首先,我没有考虑所有类型转换不匹配。事实上,我只能容纳一个组成部分。

First off, I did not account for all type conversion mismatches. In fact, I only accommodate one part.

基本上,我检查,看看是否行包含双规后面加一个空格。我知道,可能会导致错误的警告,因为一个类的结尾可以是双或任何其他事情,但我会留给你找出如何正确隔离类型。

Basically, I check to see if the line contains "double" followed by a space. I do know that could lead to false warnings, because the end of a class could be double or any number of other things, but I'll leave that to you to figure out how to properly isolate the type.

如果发现匹配,我检查,看看它这个正则表达式匹配:

If a match is found, I check to see that it matches this regex:

double[ ][A-Za-z0-9]*[ ]?=(([ ]?[0-9]*d[ ]?/[ ]?[0-9]*;)|[ ]?[0-9]*[ ]?/[ ]?[0-9]*d;)

如果它不-not-匹配这个正则表达式,然后我添加违规。这是什么正则表达式将会匹配任何如下:

If it does -not- match this regex, then I add a violation. What this regex will match on is any of the following:


  • 双I = 4D / 100;

  • 双I = 4D / 100;

  • 双I = 4 / 100D;

  • 双I = 4 / 100D;

  • 双I = 4 / 100D;

  • 双I = 4 / 100D;

  • 双I = 4D / 100;

  • 双I = 4 / 100D;

  • 双I = 4 / 100D;

  • double i=4d / 100;
  • double i = 4d / 100;
  • double i = 4 / 100d;
  • double i = 4/ 100d;
  • double i = 4 /100d;
  • double i = 4/100d;
  • double i=4d / 100;
  • double i=4 / 100d;
  • double i=4/100d;

以上任何一项都不会产生冲突。由于它目前正在写的,几乎如果不使用'D',它会抛出一个违规。你需要额外的逻辑添加到账户的明确铸造操作数的其他可能的途径。当我写这篇文章,我刚刚意识到,有两个操作数D将最有可能抛出异常。哎呦。

Any of the above will not create a violation. As it is currently written, pretty much if a 'd' isn't used, it'll throw a violation. You'll need to add extra logic to account for the other possible ways of explicitly casting an operand. As I'm writing this, I've just realized that having a 'd' on both operands will most likely throw an exception. Whoops.

最后,我不能让了StyleCop正确显示我的冲突。它不停地给我讲不存在规则的错误,甚至与第二对上眼睛,我们无法找到一个解决办法,所以我砍死了。该错误表明您试图找到规则的名称,所以我只是把名字规则的东西描述,并包括在它的行号。

And lastly, I could not get StyleCop to display my violation properly. It kept giving me an error about the rule not existing, and even with a second pair of eyes on it, we could not find a solution, so I hacked it. The error shows the name of the rule you were trying to find, so I just put the name of the rule as something descriptive and included the line number in it.

要安装自定义规则,建立MathematicalAnalyzer项目。关闭Visual Studio和复制DLL到了StyleCop安装目录。当您打开Visual Studio中,你应该看到了StyleCop设置的规则。我曾经显示了这样做的文章的步骤5和6。

To install the custom rule, build the MathematicalAnalyzer project. Close Visual Studio and copy the DLL into the StyleCop install directory. When you open Visual Studio, you should see the rule in the StyleCop settings. Step 5 and 6 of the article I used shows where to do that.

这仅得到一次违规在整个解决方案的时候,所以你必须修复违反它显示,并再次运行了StyleCop找下一个。可能有办法解决,但我跑出来的果汁,并在这里停了下来。

This only gets one violation at a time throughout the solution, so you'll have to fix the violation it shows, and run StyleCop again to find the next one. There may be a way around that, but I ran out of juice and stopped here.

享受!

这篇关于如何被警告,由于类型转换潜在的算术错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:26