# How to fix a simple problem with division of numbers

This is a discussion on How to fix a simple problem with division of numbers within the C Programming forums, part of the General Programming Boards category; Dear all, I am writing a simple program that performs some statistical calculations. The code looks like: Code: #include <stdio.h> ...

1. ## How to fix a simple problem with division of numbers

Dear all,

I am writing a simple program that performs some statistical calculations. The code looks like:

Code:
```#include <stdio.h>
#include <math.h>

int x, n;
float f_A;
float chi2 = 0;
int genotype[4];
float expected[4];

int main(void)
{

// code here

n = genotype[1]+genotype[2]+genotype[3];
f_A = ((2*genotype[1])+(genotype[2]))/(n*2);

// code here

return 0;
}```
This code doesn't work well, because the variable f_A is not computed. In other words, the code fails to calculate correctly f_A.

If I, nevertheless, replace the formula

f_A = ((2*genotype[1])+(genotype[2]))/(n*2);

to

f_A = ((2*genotype[1])+(genotype[2]));
f_A = f_A/(n*2);

the code gives correct results.

Am I missing some very basic information about C programming?

Thank you for any tips.

cheers!

Tiago

2. You are performing integer division here:

f_A = ((2*genotype[1])+(genotype[2]))/(n*2);

You have to instruct the compiler that it's floating point division, by putting a cast operator
to one of the operands :

f_A = (float) ((2*genotype[1])+(genotype[2])) / (n*2);

3. also, depending on the precision of your data,you should really use type double if you get incorrect answers

4. Thank you very much! worked like a charm!

5. I would suggest losing several of those brackets, and a multiply by 2.0f would avoid the need for a cast.
However, why not divide both the numerator and demoninator by 2 and simplify, giving just this, which will also work great:
Code:
`f_A = (genotype[1] + 0.5f * genotype[2]) / n;`

6. also make sure if n should = 0,to handle it appropriately.

I've crashed many of my programs with unforseen division by zero.

7. Should use double.

Code:
```double x, n;
double f_A;
float chi2 = 0;
double genotype[4];
double expected[4];

int main(void)
{

// code here

n = genotype[1]+genotype[2]+genotype[3];
f_A = ((2*genotype[1])+(genotype[2]))/(n*2);```
Otherwise with all this integer division you are very dependent on the order of multiplication, division.

The most common mistake is A*B/C . It is assumed that A*B comes before division by C - not guaranteed with Microsoft!!!

8. Sorry - forgot that n*2 should be n* (double)2!!!!

9. Originally Posted by johnggold
The most common mistake is A*B/C . It is assumed that A*B comes before division by C - not guaranteed with Microsoft!!!
Are you sure? These operators have the same precedence and are left-associative. This implies that A*B/C is equivalent to (A*B)/C. If a Microsoft compiler differs, then it is a Microsoft compiler bug.

10. Originally Posted by johnggold
Sorry - forgot that n*2 should be n* (double)2!!!!
Wouldn't n*2.0 be easier? (Which is part of iMalc's point.)

11. The most common mistake is A*B/C . It is assumed that A*B comes before division by C - not guaranteed with Microsoft!!!
Why should it matter in this case?

Code:
`A*(B/C)==(A*B)/C`

12. Originally Posted by KBriggs
Why should it matter in this case?
Other than possible differences with respect to underflow and overflow, it does not matter when we are talking about floating point arithmetic. However, johnggold wrote with respect to integer arithmetic, in which case it does matter since integer division could change the result, e.g., (3*3)/2 == 4, but 3*(3/2) == 3.