float comparison!
This commit is contained in:
parent
3c51da2e98
commit
cf6f47c990
43
gowrikumar/src/08b-incrfloat.c
Normal file
43
gowrikumar/src/08b-incrfloat.c
Normal file
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
float f = 0.0f;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
f = f + 0.1f;
|
||||
}
|
||||
|
||||
if (f == 1.0) {
|
||||
printf("f is 1.0 \n");
|
||||
/* nope! */
|
||||
}
|
||||
|
||||
if (f == 1.000000) {
|
||||
printf("f is perhaps 1.000000?\n");
|
||||
/* nope again */
|
||||
}
|
||||
|
||||
printf("f is neither 1.0 nor 1.000000\n");
|
||||
printf("(f is really %.24f)\n", f);
|
||||
printf("OH NOES!\n");
|
||||
printf("But...\n");
|
||||
|
||||
double difference = fabs(f - 1.000000);
|
||||
|
||||
if (difference < 0.00001) {
|
||||
printf("f is close enough to 1.000000 (off by %.24f)\n", difference);
|
||||
/* yes? */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
Loading…
Reference in New Issue
Block a user