Member Avatar for I_m_rude
int main()
{
      float i=0.3;
      .......
      ........

      if(i<0.3)
      printf("ishq\n");
      else
      printf("hello\n");

      return 0;
}

hi, can anyone please explain why i am getting output as "ishq" even if i=0.3 so if condition must be false. pleas help i am not getting it.
thanks.

What happens if you try:

if(i>=0.3)
    printf("hello\n");
else
    printf("ishq\n");

?

Some simple decimals cannot be perfectly represented using the internal floating point representation.

Member Avatar for I_m_rude

@deceptikon what should i read in this for getting my answer ? it is really a big section you gave me to read. also, i am feeling difficiult to understand this(although i have not read it completely).

Floating point values are approximations, and any manipulation of a floating point number is like to produce an approximate value that may not be what pure mathmatical logic would suggest the answer should be. For example take

float i=0.3;

i += 0.000005 * 30000000;
i -= 0.000005 * 30000000;

Logic suggests that at the end of these 3 line i still has the value 0.3 since you have taken off exactly what you added on but on my system it has the value 0.300003 because of the inherent inaccuracies introduced in performing floating point calculations.

You should absolute never use == or != with floating points you can assume that they always equate to false and true respectively. If you do use <, <=, > and >= then you need to be aware and accept that there will be cases where the program produces the opposite result to that which pure logic produces.

@deceptikon what should i read in this for getting my answer ?

All of it. Just getting the answer to your immediate question won't stop you from making a similar but seemingly unrelated mistake with floating-point in the future. So it behooves you to study up on the representation and behavior of floating-point in general.

Member Avatar for I_m_rude

but when i use 0.3f then the o/p is correct. then why is it so ? And also, tell me when should i not use comparisons with floating numbers? @banfa

Member Avatar for I_m_rude

@decetikon ok! I am reading it. but if there is any problem, then i will ask you only and in this thread only. is it ok ?

Because 0.3f has type float and 0.3 has type double so using 0.3 inloves an implicit conversion of your variable i to type double and since, as discussed, floating point representations are approximations and double as a different represenation to float the conversion can change the value slightly making it higher or lower.

You should not use comparisons in floating point anywhere that you require absolute acuracy, it is fine if you are testing some tolerance, you have a complex equation with a check value and the output is right if the check value is <0.1 because if the check value is actually 0.1000001 then the result was borderline anyway and it is probably fine to discard it.

If you want accuracy, or you are on an embedded platform and you don't want to pull the bloat of the floating-point emulation library into your application then you use scaled integers. A typical example is the banking world, they do not hold money as pounds (£) in a float type with pence as a fractional part. Rather they use a scaled integer and hold money as pence (or may be even 1/10th of pence) in an integer value where practical maths operates as you would expect theoryetical maths to work. Of course you have to watch out for the upper bound of an integer but generally today that can be solved by using some sort of big-int class that has no upper bound.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.