Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be warned about potential arithmetic errors due to type conversion?

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

double v = 4 / 100;

I know this is a wrong initialization that returns v = 0.0 instead of v = 0.04

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.

Instead, is it possible to get warned by the compiler about this ?

like image 956
Larry Avatar asked Dec 12 '12 13:12

Larry


People also ask

What is an example of an error in a conversion error?

Conversion/overflow errors aren’t that unusual – normally a data flow broken by some unexpected data ( “no, there’s no chance that field would ever have a character in it” ), or perhaps a column hitting max size ( “INT will be enough for years, like, 5 years. I’ll have left the company by then”)

What is arithmetic error in ODBC?

Arithmetic Errors. The ODBC driver evaluates the WHERE clause in a SELECT statement as it fetches each row. If a row contains a value that causes an arithmetic error, such as divide-by-zero or numeric overflow, the driver returns all rows, but returns errors for columns with arithmetic errors.

How do you avoid computational errors in math?

In my experience, the best way to avoid computational errors is to avoid computation. Develop general algorithms for whatever quantity that you are looking for and then proceed to "plug and chug" as the last step. Mathematics requires precision, however, and you often cannot avoid having to comb over your work tediously.

What is the best way to check for arithmetic errors?

There are certain quick methods called sanity checks which will catch most (but not all) arithmetic errors. One common one is to replace each number with the sum of its digits, which is the "casting out nines" method mentioned in Robert Israel's answer.


1 Answers

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;)

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

  • 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;

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.

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.

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.

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.

Enjoy!

like image 166
seekerOfKnowledge Avatar answered Nov 15 '22 12:11

seekerOfKnowledge