From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrej Gelenberg Subject: Re: float Date: Tue, 11 Jan 2011 09:38:17 +0100 Message-ID: <4D2C16F9.4050607@udo.edu> References: Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: Sender: linux-c-programming-owner@vger.kernel.org List-ID: Content-Type: text/plain; charset="us-ascii"; format="flowed" To: ratheesh k Cc: linux-c-programming@vger.kernel.org Hi, you can not check if 2 float are equal, you should check if it in the range: On 01/11/2011 08:38 AM, ratheesh k wrote: > I could not understand why it getting printed like this. Could any > body tell me. > > #include > #define ABS(x) ((x) < 0 ? -(x) : (x)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) const float precision = 0.001f; > int main() > { > float f=0.0f; > int i; > > for(i=0;i<10;i++) > f = f + 0.1f; > if ( ABS(f - 1.0f) < precision ) > printf("f is 1.0 \n"); > else > printf("f is NOT 1.0\n"); > > return 0; > } Here more to that topic: https://www.securecoding.cert.org/confluence/display/seccode/FLP35-C.+Take+granularity+into+account+when+comparing+floating+point+values Here more about c pitfalls: https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Secure+Coding+Standard Regards, Andrej Gelenberg