Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple but confusing c program

Posted on 2006-06-08
11
Medium Priority
?
614 Views
Last Modified: 2010-04-15
hello experts, please justify the output of the follwing simple program.

void main()
{
      float a=0.5;
      float b=0.7;
      clrscr();
      if(a==.5)
           printf("Ok");
      else
           printf("NotOk");
      
      if(b==.7)
          printf("  Ok");
      else
          printf("  NotOk");
}

Thanks.
0
Comment
Question by:soodrajni123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 16867717
floating point numbers are not exact
float 0.7 may actually be closer to 0.699999988079071044921875
0
 
LVL 84

Expert Comment

by:ozo
ID: 16867749
whereas 0.7 means (double) 0.7, which may be closer to 0.6999999999999999555910790149937383830547332763671875
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16867862
While comparing two float variables, the compiler converts them in double for more accuracy thats why .7 after conversion becomes 0.6999999999999999555910790149937383830547332763671875
which means value is very very near to 0.7.

So first comparision  i.e. with 0.5 output is "Ok"
and for 0.7 the output is "Not ok" since the equality condition fails.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:ozo
ID: 16868016
> comparing two float variables, the compiler converts them in double
no, when comparing two float variables, the compiler does not convert them.
when compating a float and a double, the compiler converts the float to a double.
0
 
LVL 3

Accepted Solution

by:
srinims earned 252 total points
ID: 16868695
#include <stdio.h>
void main()
{
     float a=0.5;
     float b=0.7;

       double c=0.5;
       double d=0.7;

     //clrscr();
     if(c==.5)
          printf("Ok");
     else
          printf("NotOk");
     
     if(d==.7)
         printf("  Ok");
     else
         printf("  NotOk");
}


this will work finely.

the thing is the default data type of any numbers with fractions(like 4.5) is doble.


and for the values
a=0.5
b=0.7 in float are handled as

      a      0.500000
      b      0.700000


but if the values of
c=0.5
d=0.7 in double are handle as

      c      0.50000000000000000
      d      0.69999999999999996

as the default type of the fraction numbers are double.
comparison with b==0.7 comparing  0.700000 with 0.69999999999999996
that's y it is giving not ok to u.

and for a==0.5 it's comparing  0.500000 with 0.50000000000000000 and its giving ok

check it in debugger.
0
 
LVL 3

Expert Comment

by:chandru_in
ID: 16868813
ozo i right.

Just say b == .7f and the result should be as expected.
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 248 total points
ID: 16869311
In general you should NEVER expect any two floating-point numbers to ever be exactly equal.  As you have seen, some code as simple as:

double f = 0.7;
if( f == 0.7 ) .....

may not work as expected.  

The reasons are too complex to explain in this little box.   They include diwsparate factors such as the precision of floating-point constants, float and double, the IEEE rounding mode in effect, the hidden conversions from float to double, the varying precision of 32, 64, 80, 96, and 128 bit floating-point registers, the vagaries of bit versus hex-oriented normalization, the effects of the assumed leading bit, the effects of non-IEEE flating-point formats, the effects of printf and sprintf rounding, the effects of "banker's" rounding, the effects of compiler optimization of rounding and algebraic rearrangement and factoring, and probably more things I can't think of but can mess up floating-point math in the wee bits.

Just assume that you can NEVER comapre floating point numbers for exact equality and you'll do okay.

A better way is to specify how "close" you need the numbers to be, relative to their size.  For example, in many cases you can live with a fudge factor of one part per million, or billion.  So you'd use a function something like this:

Boolean Compare( double a, b ){ double avg, delta;  Boolean closeEnough;
avg = (a + b) / 2.0;
delta = avg / 1.0E6;  // or 1.0E9 for a billionth of course
closeEnough=  abs(a-b) < delta;
return closeEnough;
}

0
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 16883944
>>Just assume that you can NEVER comapre floating point numbers for exact equality and you'll do okay
dat's the key!!
as ozo has also said
when u compare a float with a double it converts the float value to double
dat's why when u comapred variable "b" with 0.7 ,the comparison fails
0
 
LVL 1

Expert Comment

by:tushar_comp
ID: 16925491
hi,

look u can not compare the floats like this
u have to round off it
fix the decimal places then compare
else
u wont get the answer
use %.4f means 4 dec places after point
u will get lot of ref on google for this  ..

tushar  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question