Solved

Strange output..

Posted on 2006-07-20
6
288 Views
Last Modified: 2010-04-15
Consider the following small code.
#include <stdio.h>
float a = 0.7;
main()
{
      if (a<0.7)
            printf("Hello\n");
      else
            printf("World\n");
}

The output of this  is “Hello”…….
Can anybody explain this ?….

If I change 0.7 to 0.8 then output will be “World”…

Why is the reason behind this?

Thanks in advance
0
Comment
Question by:jpsarkar
6 Comments
 
LVL 14

Expert Comment

by:StillUnAware
Comment Utility
try using a cast:

 if (a < (float)0.7)

I guess the compiler interprets the 0.7 as double, it is more accurate than float, and the values does't match, because of different precision.
0
 
LVL 8

Expert Comment

by:deepu chandran
Comment Utility
hi,

Try this code segment will get you more about this problem

printf("%20.10f %20.10f\n",0.5, 0.5f);
output is: 0.5000000000 0.4999999881

if you want more details just visit

http://docs.sun.com/source/806-3568/ncg_goldberg.html
cheers

deepu
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Hi jpsarkar,

Floating point comparisons are not exact ... typically you would assume float1 == float2 if fabs(float1-float2)<delta where delta is a very small number. Alternatively, you can scale the numbers e.g. in the above example you could have multiplied a by 10 and then compared with 7 instead of 0.7 ... Ofcourse for this you need to know the precision in advance.

Here are some previous discussions on this topic which should provide all the details you are looking for ... In case you need further clarifications, feel free to post back

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20707050.html -> long nice explanation
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21385870.html
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20690437.html
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_10350949.html
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20392174.html

Cheers!
sunnycoder
0
 

Author Comment

by:jpsarkar
Comment Utility
OK deepu…
The same logic should be true and same type of output should come for 0.8 or even 0.6, or 0.5 etc…….
But for those the output is not like 0.7
……

What is the reason behind this ??….
0
 
LVL 8

Accepted Solution

by:
manish_regmi earned 50 total points
Comment Utility
Firstly,
    The floating point operations are only approximations. Because the precision is infinite.

secondly,
   It is Architecture and Compiler Dependent how the floating point numbers are temporarily stored for calculation. So different compilers and different  Arch results different answers.

The correct way to do is pointed out by sunny.

Read this if you need more explanation
http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm

regards
Manish Regmi
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

762 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

9 Experts available now in Live!

Get 1:1 Help Now