Skip to content

Instantly share code, notes, and snippets.

@de1o
Last active December 14, 2015 08:19
Show Gist options
  • Save de1o/5057299 to your computer and use it in GitHub Desktop.
Save de1o/5057299 to your computer and use it in GitHub Desktop.
examples: how to compare float number in Clang.
#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 ;
}
@de1o
Copy link
Author

de1o commented Feb 28, 2013

  1. if EPSILON==0.000000000000000001, output:
compare immediately: i/j>k
compare d-value with EPSILON: i/j larger
tmp == k
  1. if EPSILON==0.0000001, output:
compare immediately: i/j>k
compare d-value with EPSILON: i/j == k
tmp == k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment