user2779715
user2779715

Reputation: 33

why float is not working?

in my code, the output is 2.000000 whereas it's supposed to be 2.11111

#include<stdio.h>main(){
int i,j;
float r;
i = 19;
j = 9;
r = i / j;
printf(" %f ",r);}

why it's not working?

Upvotes: 2

Views: 7521

Answers (6)

r_goyal
r_goyal

Reputation: 1157

Because i / j is integer division (both operands are integers),so their division results in an integer number (with the fractional part discareded) which is again assigned to a float type of variable r so the compiler typecasts(i.e.implicit typecasting) the integer result to float again to show 2.000000

You would get the desired result if one of i and j were a float, e.g.:

r = ((float)i) / j; //explicit typecasting

Upvotes: 0

Rafed Nole
Rafed Nole

Reputation: 122

Here, int was converted to float by default after the RHS (integer division, the result of which is by default the quotient = an integer) was evaluated.That's why you get 2.000000. Typecast any number in RHS to get the result you seek.

See you need to understand type conversions in c and then casting.Check them in any good source. Generally, automatic conversions are those which can convert a narrower operand into a wider one without loss of information. For example, converting an integer to floating point in examples like float + integer (on 64-bit machine).[from wiki].

e.g float x =7.8 ; int k=9; int j = k + x = 16 .

Here, first k is coverted to float, then added and then finally the result is truncated to an integer.

Upvotes: 0

Anthony Accioly
Anthony Accioly

Reputation: 22481

Diving two integers wilds a integer (in your case 2 which is later converted to a float):

Just convert i or j to float (or declare them as floats to begin with):

r = ((float) i) / j;

Working example

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

This is because the division is done in integers before being assigned to a float. Change the type of i and j to float to get it fixed:

main(){
    float i,j,r;
    i = 19;
    j = 9;
    r = i / j;
    printf(" %f ",r);
}

Alternatively, you can cast i to float in the division, like this:

r = ((float)i) / j;

Upvotes: 2

ouah
ouah

Reputation: 145899

Change

r = i / j;

with

r = i / (float) j;

i and j are integers and i / j is a integer division.

Upvotes: 2

Jon
Jon

Reputation: 437754

Because i / j is integer division (both operands are integers). It doesn't make any difference that you store the result in a float.

You would get the desired result if one of i and j were a float, e.g.:

r = ((float)i) / j;

Upvotes: 8

Related Questions