Skip to content

Instantly share code, notes, and snippets.

@hfossli
Created May 14, 2014 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hfossli/4616c778bea3a334f034 to your computer and use it in GitHub Desktop.
Save hfossli/4616c778bea3a334f034 to your computer and use it in GitHub Desktop.
CGFloatEqual
BOOL CGFloatEqual(CGFloat a, CGFloat b, CGFloat accuracy)
{
#if CGFLOAT_IS_DOUBLE
if (fabs(a-b) < accuracy * DBL_EPSILON * fabs(a+b) || fabs(a-b) < DBL_MIN)
{
return YES;
}
#else
if (fabs(a-b) < accuracy * FLT_EPSILON * fabs(a+b) || fabs(a-b) < FLT_MIN)
{
return YES;
}
#endif
return NO;
}
@AlbertRenshaw
Copy link

AlbertRenshaw commented Apr 22, 2019

What is a suggested value for accuracy? Should it be like 0.9999f and we add more 9s to lower the tolerance?

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