Strange output..

Posted on 2006-07-20
Last Modified: 2010-04-15
Consider the following small code.
#include <stdio.h>
float a = 0.7;
      if (a<0.7)

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
Question by:jpsarkar
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
LVL 14

Expert Comment

ID: 17144928
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.

Expert Comment

by:deepu chandran
ID: 17144935

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

LVL 45

Expert Comment

ID: 17144981
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 -> long nice explanation


Author Comment

ID: 17154451
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 ??….

Accepted Solution

manish_regmi earned 50 total points
ID: 17162219
    The floating point operations are only approximations. Because the precision is infinite.

   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

Manish Regmi

Featured Post

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Macro will not compile after project converted from IAR to Microchip xc8 (in MPLab) 3 203
Console based application in Linux 1 105
C hashtable library 3 113
outlook office 365 8 172
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…
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…
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 how to create, access, and change arrays in the C programming language.

734 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