linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Amit Virdi <amit.virdi@st.com>
To: Thomas Weber <thomas.weber.linux@googlemail.com>
Cc: ratheesh k <ratheesh.ksz@gmail.com>,
	"linux-c-programming@vger.kernel.org"
	<linux-c-programming@vger.kernel.org>
Subject: Re: float
Date: Tue, 11 Jan 2011 13:26:47 +0530	[thread overview]
Message-ID: <4D2C0D3F.70008@st.com> (raw)
In-Reply-To: <4D2C0AC0.2000200@gmail.com>

In floating point comparisons, test for range rather testing for equality. Something like:

 int compare_float(float f1, float f2)
 {
  float precision = 0.00001;
  if (((f1 - precision) < f2) && 
      ((f1 + precision) > f2))
   {
    return 1;
   }
  else
   {
    return 0;
   }
 }

This is because, floating point numbers are stored in binary format and rounds off in strange ways.

On 01/11/2011 01:16 PM, Thomas Weber wrote:
> Am 11.01.2011 08:38, schrieb ratheesh k:
>> I could not understand  why it getting printed like this. Could any
>> body tell me.
>>
>> #include <stdio.h>
>>
>> int main()
>> {
>>         float f=0.0f;
>>         int i;
>>
>>         for(i=0;i<10;i++)
> missing braces here?
>>                 f = f + 0.1f;
>>
>>         if(f == 1.0f)
>>                 printf("f is 1.0 \n");
>>         else
>>                 printf("f is NOT 1.0\n");
>>
>>         return 0;
>> }
>> --
> 
> 
> Thomas
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

-- 
~Amit Virdi

  reply	other threads:[~2011-01-11  7:56 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-11  7:38 float ratheesh k
2011-01-11  7:46 ` float Thomas Weber
2011-01-11  7:56   ` Amit Virdi [this message]
2011-01-11  7:55 ` float Uriel Corfa
2011-01-11  8:01   ` float Thomas Weber
2011-01-11  8:34     ` float ratheesh k
2011-01-11  8:57       ` float Thomas Weber
2011-01-11  8:38 ` float Andrej Gelenberg
  -- strict thread matches above, loose matches on Subject: below --
2010-01-08 16:30 float phoenixxz
2010-01-08  4:42 ` float Bryan Christ
2010-01-08 16:45   ` float Fawad Lateef
2010-01-08 16:40 ` float Nanakos Chrysostomos
2010-01-08 16:44 ` float Fawad Lateef
2010-01-08 16:47 ` float Nanakos Chrysostomos
2010-01-08 17:11   ` float Michal Nazarewicz
2010-01-08 17:19     ` float Nanakos Chrysostomos
2010-01-08 17:21     ` float Nanakos Chrysostomos
2010-01-08 17:43       ` float Michal Nazarewicz
2010-01-08 18:00 ` float andrej.gelenberg
2010-01-08 22:05 ` float Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D2C0D3F.70008@st.com \
    --to=amit.virdi@st.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=ratheesh.ksz@gmail.com \
    --cc=thomas.weber.linux@googlemail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).