2

I have a function which takes 3 double values, none of that from an array or casted integers or some like that, are 3 double values which are calculated before calling this function. Then, the function is expected to return a double value and does... but one of the terms is considered as an integer, specifically (5/3) = 1.0 What I did is to replace (5/3) with 1.6667 Why is this happening? Any idea? Here is the code :

private static double get_fa(double cc, double R, double fy)
{
  double num, den, fa;
  if (cc<=200)
  {
    if (R <= cc)
    {
      num = (1 - (Math.Pow(R, 2) / (2 * Math.Pow(cc, 2)))) * fy;
      den = (5/3) + (3 * R) / (8 * cc) - Math.Pow(R, 3) / (8 * Math.Pow(cc, 3));
      fa = num / den;
    }
    else
    {
      fa = (12 * Math.Pow(Math.PI, 2) * E) / (23 * Math.Pow(R, 2));
    }
    return fa;
  }
  else
  {
    return 0;
  }
}
Mariles
  • 83
  • 5
  • 1
    What is `E` variable? – Backs Aug 23 '15 at 04:23
  • E is a constant = 2.1e6 I know there's other question similar as marked by Alexei L. (thanks) but I wanted to someone else take a look because, like I mentioned, none of the involved variables or constants are integers or type-casted... also, in my program, there's another formula which is also returning an integer under the same conditions... – Mariles Aug 24 '15 at 14:09

2 Answers2

4

"When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2."

Source: / Operator (C# Reference)

To fix this, you can use double constants instead of integer: ( 5.0 / 3.0 ).

The other divisions in your code work as expected because at least one of the operands is already a double. But it wouldn't be a bad idea to make all of your constants in this calculation doubles instead of integers, to help make the intent more clear.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • 1
    Actually, it would be better to assign the expression `5.0 / 3.0` to a named constant, so that the code is more self-documenting. It's bad enough the code has all that uncommented formulae, but for a constant expression to be completely undocumented as well? Yikes. (Actually, it's better to not answer at all questions that have already been answered elsewhere, as it just encourages the poor habit of posting duplicate questions instead of doing real research, but that's a whole other issue :) ). – Peter Duniho Aug 23 '15 at 05:12
  • @PeterDuniho: Excellent points, both of them. OTOH, the linked duplicate uses a `(double)` cast to convert an integer variable to a double, while in this case it is simpler to just use a double constant like `5.0` in the first place. Someone who is just beginning with C# and reads the other answer may not realize that either approach will work. – Michael Geary Aug 23 '15 at 05:14
0

If You want all of your values in "Double" Then in Your Code use all Integers with a "0" after the Decimal Point, For eg: 5 will transformed to 5.0

With This Your Function will plays with only Double data types..

For eg: 
Replace (5 / 3)
With (5.0 / 3.0) 
Keshav bansal
  • 130
  • 1
  • 2