0

I'm trying to do some arithmetic on integers. The problem is when I'm trying to do division to get a double as a result, the result is always 0.00000000000000000000, even though this is obviously not true for something like ((7 * 207) / 6790). I have tried type-casting the formulas, but I still get the same result.

What am I doing wrong and how can I fix it?

int o12 = 7, o21 = 207, numTokens = 6790;
double e11 = ((o12 * o21) / numTokens);
printf(".%20lf", e11); // prints 0.00000000000000000000
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
user2052561
  • 1,339
  • 2
  • 15
  • 18

4 Answers4

3

Regardless of the actual values, the following holds:

int / int = int

The output will not be cast to a non-int type automatically.

So the output will be floored to an int when doing division.

What you want to do is force any of these to happen:

double / int = double
float / int = float
int / double = double
int / float = float

The above involves an automatic widening conversion - note that only one needs to be a floating point value.

You can do this by either:

  • Putting a (double) or (float) before one of your values to cast it to the corresponding type or
  • Changing one or more of the variables to double or float

Note that a cast like (double)(int / int) will not work, as this first does the integer division (which returns an int, and thus floors the value) and only then casts the result to double (this will be the same as simply trying to assign it to a double without any casting, as you've done).

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
1

It is certainly true for an expression such as ((7 * 207) / 6790) that the result is 0, or 0.0 if you think in double.

The expression only has integers, so it will be computed as an integer multiplication followed by an integer division.

You need to cast to a floating-point type to change that, e.g. ((7 * 207) / 6790.0).

Many poeple seem to expect the right-hand side of an assignment to be automatically "adjusted" by the type of the target variable: this is not how it works. The result is converted, but that doesn't affect any "inner" operations in the right-hand expression. In your code:

e11 = ((o12 * o21) / numTokens);

All of o12, o21 and numTokens are integer, so that expression is evaluated as integer, then converted to floating-point since e11 is double.

This like doing

const double a_quarter = 1 / 4;

this is just a simpler case of the same problem: the expression is evaluated first, then the result (the integer 0) is converted to double and stored. That's how the language works.

The fix is to cast:

e11 = ((o12 * o21) / (double) numTokens);
unwind
  • 391,730
  • 64
  • 469
  • 606
0

You must cast these numbers to double before division. When you perform division on int the result is also an integer rounded towards zero, e.g. 1 / 2 == 0, but 1.0 / 2.0 == 0.5.

Archie
  • 6,391
  • 4
  • 36
  • 44
0

If the operands are integer, C will perform integer arithmetic. That is, 1/4 == 0. However, if you force an operand to be double, then the arithmetic will take fractional parts into account. So:

int a = 1;
int b = 4;
double c = 1.0

double d = a/b;         // d == 0.0
double e = c/b;         // e == 0.25
double f = (double)a/b; // f == 0.25
Chowlett
  • 45,935
  • 20
  • 116
  • 150