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
Medium Priority
614 Views
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
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

LVL 84

Expert Comment

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

LVL 84

Expert Comment

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

LVL 8

Expert Comment

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

LVL 84

Expert Comment

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

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

ID: 16868813
ozo i right.

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

LVL 22

Assisted Solution

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

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

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
use %.4f means 4 dec places after point
u will get lot of ref on google for this  ..

tushar
0

## Featured Post

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.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
###### Suggested Courses
Course of the Month11 days, 6 hours left to enroll