LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2012, 10:41 PM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
why this result when using if :: c


Code:
float a = .7 ;

if ( .7 > a )
{
    printf("yes");
}
else
{
    printf("no");
}
why output is :: Yes
 
Old 08-21-2012, 10:52 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The floating-point numbers aren't accurate when storing fractions. Plus, float values are less accurate than double values. Conclusions:

1. Don't use type float.
2. Be careful when working with floating-point values. Eg:

Code:
wrong: if (x==0.1) ...
good:  if (fabs(x-0.1)<1e-9) ...
 
Old 08-22-2012, 12:33 AM   #3
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Rep: Reputation: Disabled
Code:
float a=.7;
(.7f>a)?printf("Yes\n"):printf("No\n");
by default .7 is stored as double that are accurate upto 14 decimal points, but float is accurate upto 6 decimal place only,
so .7(double) > a(float) /* Result is yes */
and .7f(float) > a(float) /* Result is no because both are equal now */
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 02:39 PM
no result after compile, nasim751 Programming 1 01-29-2008 03:41 PM
copy ls result athena_1 Programming 1 07-29-2006 03:46 AM
ping result. bruse Linux - Networking 1 11-18-2005 04:53 PM
df gets different result from du? liyuefu Linux - General 4 10-01-2005 10:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration