Re: float

Andrej Gelenberg <[email protected]>
Newsgroups gmane.linux.c-programming
Message-ID <[email protected]>
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<stdio.h>
>

#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
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.