Mberu
Mberu

Reputation: 33

Am I dividing in the right way in C?

I need to apply a formula to three variables. Some of the operations (divisions) give me a 0 instead of the proper number. So I get a different result from what I expected from the formula.

I think that the problem is related to the way C manages data types and their remainders. For this reason, I tried to transform int variables into float variables or to round the numbers that I divided. But all of this is still not working and I'm not able to understand what I'm missing.

Could you please have a look at my code and let me know where is the mistake? Thanks

double grade(int lc, int wc, int sc)
{
    wc = (float)wc;
    lc = (float)lc;
    sc = (float)sc;
    float L = round((wc/100)/lc);
    float S = round((wc/100)/sc);
    float index = 0.0588 * L - 0.296 * S - 15.8;
    return index;
}

Upvotes: 2

Views: 92

Answers (3)

chux
chux

Reputation: 154087

  • wc = (float)wc; casts an int to a float, converts back to int, then assigns. No real benefit here.

  • wc/100 is integer division, which discards the fraction of the quotient. Not likely what OP wants.

  • Converting int to float risks losing precision as so a less precise answer. double is better for wide ranging int values.

  • Code mixes uses of float and double with a call to a double round(double), double multiplication and conversions from float to double to float to double. Simplify and use double throughout.


double grade(int lc, int wc, int sc) {
    //               v------v `double` division 
    double L = round(wc/100.0/lc);
    double S = round(wc/100.0/sc);
    return 0.0588 * L - 0.296 * S - 15.8;
}

Upvotes: 2

Eraklon
Eraklon

Reputation: 4288

Or just do this (the other operands will not need the cast to float since it enough if only one of the operands is it, then the others will be converted to it implicitly)

double grade(int lc, int wc, int sc)
{
    float L = round(((float)wc/100)/lc);
    float S = round(((float)wc/100)/sc);
    float index = 0.0588 * L - 0.296 * S - 15.8;
    return index;
}

Note that the return type is double, but you use just floats. Use double instead of floats in the function as types as well OR change return type to float.

The mistake is that this wc = (float)wc; is essentially this wc = (int)(float)wc; since wc type is int. Basically nothing happens, therefore when you make your calculation like wc/100, then this will be an integer division resulting in integer, so if wc is less than 100 (and >= 0), then the result is 0 and not a real number as you would expect.

Upvotes: 2

Ardent Coder
Ardent Coder

Reputation: 3995

Problem:

wc = (float)wc;
lc = (float)lc;
sc = (float)sc;

All these statements do not do what you think because the types of these variables are originally int.

You are converting an int to float and assigning it back to int.

Solution:

Pass the values to a float rather than int to the function, implicit conversion will take care of the rest.

Change your function signature to

double grade(float lc, float wc, float sc)

Upvotes: 2

Related Questions