Solved

simple but confusing c program

Posted on 2006-06-08
11
514 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
11 Comments
 
LVL 84

Expert Comment

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

Expert Comment

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

Expert Comment

by:kaliyugkaarjun
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
> 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Accepted Solution

by:
srinims earned 63 total points
Comment Utility
#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
Comment Utility
ozo i right.

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

Assisted Solution

by:grg99
grg99 earned 62 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now