Last active
December 14, 2015 08:19
-
-
Save de1o/5057299 to your computer and use it in GitHub Desktop.
examples: how to compare float number in Clang.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
main() | |
{ | |
float i=7, j=10, k=0.7; | |
const float EPSILON = 0.000000000000000001; | |
//const float EPSILON = 0.0000001; | |
float tmp = i/j; | |
if (i/j == k) | |
{ | |
printf("compare immediately: i/j==k\n"); | |
} | |
else if (i/j < k) | |
{ | |
printf("compare immediately: i/j<k\n"); | |
} | |
else | |
{ | |
printf("compare immediately: i/j>k\n"); | |
} | |
if (i/j-k > EPSILON) | |
{ | |
printf("compare d-value with EPSILON: i/j larger\n"); | |
} | |
else if (i/j-k < -EPSILON) | |
{ | |
printf("compare d-value with EPSILON: i/j smaller\n"); | |
} | |
else | |
{ | |
printf("compare d-value with EPSILON: i/j == k \n"); | |
} | |
if (tmp-k > EPSILON) | |
{ | |
printf("tmp larger\n"); | |
} | |
else if (tmp-k < -EPSILON) | |
{ | |
printf("tmp smaller\n"); | |
} | |
else | |
{ | |
printf("tmp == k \n"); | |
} | |
printf("end\n"); | |
return ; | |
} |
Author
de1o
commented
Feb 28, 2013
- if EPSILON==0.000000000000000001, output:
- if EPSILON==0.0000001, output:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment