Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:28:05+00:00 2026-06-16T04:28:05+00:00

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

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:28:06+00:00Added an answer on June 16, 2026 at 4:28 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the jQuery calculation and have just about everything working properly, but I
I am using Delphi 2007 and working on some presentation software. The current module
Hej guys, I was wondering if you know any well working Math or Calculation
I have a small javascript pricing calculation that is not working on a friend's
Working through this for fun: http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/ Example calculation of nullable and first uses a
My calculation is not working properly. I can not see anything wrong with the
I'm working on a for-fun-project that I will be doing some calculations with and
Working on a small game using an HTML5 canvas, and javascript. And it's working
Working with an undisclosed API, I found a function that can set the number
Working with H2 I get this error when I try to write a row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.