Home > Blockchain >  Return a float value when using int parameters C
Return a float value when using int parameters C

Time:11-02

I have this issue where I need to print float number while I use int parameters in function.

float lift_a_car(const int stick_length, const int human_weight, const int car_weight) {
  return (stick_length*human_weight)/(car_weight human_weight);
}

I'm checking it by using:

printf("%.4f\n", lift_a_car(2, 80, 1400));

It only returns 0.0000.

CodePudding user response:

The calculation

(stick_length*human_weight)/(car_weight human_weight)

is an all integer calculation with an integer result. You should cast at least one of the variables or intermediate results to a floating point value.

Like for example

(float) (stick_length*human_weight)/(car_weight human_weight)

That will convert the result of stick_length*human_weight into a float value, making the division a floating-point operation with a floating-point result.

CodePudding user response:

The reason for this is, that in C every calculation is made with the most complex type in use. In your case this type is int, because int/int is treated as an integer division. The same for the addition and multiplication. To fix this, you have to cast the integers to floats explicitly, otherwise it will only be done at the end.

Your code return (stick_length*human_weight)/(car_weight human_weight); equals the following operation:

int t1 = stick_length * human_weight; // 2 * 80 = 160
int t2 = car_weight * human_weight;   // 1400 * 80 = 112000
int t3 = t1 / t2; (integer division)  // 160 / 112000 = 0
return (float) t3;

But what you want is to do this:

return ((float) stick_length*human_weight)/((float) car_weight human_weight);
// or
return (float) (stick_length*human_weight)/(car_weight human_weight);

This will be evaluated like:

float t1 = (float) stick_length * human_weight; // 2.0f * 80 = 160.0f
float t2 = (float) car_weight   human_weight;   // 160.0f * 140 = 112000.0f
float t3 = t1 / t2; (floating division)         // 160.0f / 112000.0f = 0.0014...
// or
int t1 = stick_length * human_weight;           // 2 * 80 = 160
int t2 = car_weight   human_weight;             // 160 * 140 = 112000
float t3 = (float) t1 / t2; (floating division) // 160.0f / 140 = 0.0014...

CodePudding user response:

Although the answer of our programming dude is correct, it looks quite dangerous:

Indeed, this is integer arithmetic:

(stick_length*human_weight)/(car_weight human_weight)

Indeed, this is floating point arithmetic:

(float) (stick_length*human_weight)/(car_weight human_weight)

But why? Simply because typecasting precedes multiplication (or division): it's the same as:

((float) (stick_length*human_weight))/(car_weight human_weight)

But beginners might not be aware of that and might start to do stuff like:

(float) ((stick_length*human_weight)/(car_weight human_weight))

=> which will again give a bad result.

Therefore I would propose to perform a typecasting, as narrow as possible, something like:

((float) stick_length*human_weight)/(car_weight human_weight)
  •  Tags:  
  • c
  • Related