0

I've been writing this program in C, and I've noticed that the division result, captured in the fee variable, has always fixed value, and that is 5.00, but the result should be 5.78. Can you explain why it has this behaviour and what should I do to fix it ?

#include <stdio.h>
#include <stdlib.h>

int main(void){

    printf("Input package dimensions: width, heigth, length \n");
    int width, height, length;
    scanf("%d", &width ); 
    scanf("%d", &height ); 
    scanf("%d", &length );

    int weight = width*height*length;
    printf("%d\n", weight);
    float fee = weight/166;

    printf("%.2f\n", fee);
    printf("The fee is : $%.2f\n and", fee); 

    system("pause");
    return 0;
}
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
Justplayit94
  • 385
  • 2
  • 19

7 Answers7

4

Thats because you are dividing int by int. To get a floating point number at least one of them should be a floating number.

float fee = weight/166.0;
Nakib
  • 4,593
  • 7
  • 23
  • 45
4

Both weight and 166 have type int, therefore weight/166 is an integer division, which truncates any fractional part. The fact that you assign the result to a variable of type float is irrelevant.

You want to instead perform floating-point division, which you can accomplish by ensuring that at least one of the operands has a floating-point type. One of the simplest ways to do that would be

float fee = weight / 166.0;
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 1
    Won't that do `int` -> `double` -> (division) -> `float` conversion? – Amit Dec 04 '15 at 19:01
  • 1
    @Amit, yes it will. Division of `double`s is floating-point division, just as division of `float`s is (and in practice, division of `float`s is often performed as division of `double`s anyway). – John Bollinger Dec 04 '15 at 19:19
3

Try:

float fee = (float)weight/166.0;

Otherwise an integer-division is performed and then casted to float.

Ctx
  • 18,090
  • 24
  • 36
  • 51
3
float fee = weight/166;

166 is int and wight is int. int divided by another int will always result in int and only when the result is stored in fee, it is casted to float.

You can fix it by

float fee = (float)weight/166;

or by

float fee = weight/166.0;
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
2

In this line weight and 166 will be divided as integers and then cast to float.

float fee = weight/166;  // integer division

You need to cast or define one of them as float for a more accurate division.E.g.

float fee = weight/166.0; // double precision division

Or

float fee = (float) weight/166; // single precision division (float)

Or if you want to surprise your colleagues :

float fee = weight/166.0f; // single precision division (float)
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • Found `float fee = weight/166.0;` is not the same as `float fee = (float) weight/166;`. The first does the division using `double` math. The 2nd may only use `float` math. Try `weight == 16777217`. (Just trying to surprise my colleagues. :-)) – chux - Reinstate Monica Dec 04 '15 at 19:30
  • @chux that is correct. And because you made me actually compile and test my examples, I found that my last recommendation doesn't compile (`error: invalid suffix "f" on integer constant`) – Manos Nikolaidis Dec 04 '15 at 21:28
2

That's because dividing an integer by an integer results in an integer, And that's what weight/166 does. It is later covered to a floating point, but that's "too late".

The most explicit way to have a floating point division in your case is:

(float)weight / 166.0f
Amit
  • 45,440
  • 9
  • 78
  • 110
1

This should fix it.

float fee = weight/166.0;
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
Tony Tannous
  • 463
  • 1
  • 10
  • 23