Last active
April 15, 2020 09:33
-
-
Save 623637646/409a751a84c1588bae9bcd8a4edf3a2d to your computer and use it in GitHub Desktop.
compare CGFloat in iOS (refer https://github.com/material-components/material-components-ios/blob/develop/components/private/Math/src/MDCMath.h and https://stackoverflow.com/a/10335601/9315497)
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
static CGFloat MDCFabs(CGFloat value) { | |
#if CGFLOAT_IS_DOUBLE | |
return fabs(value); | |
#else | |
return fabsf(value); | |
#endif | |
} | |
static BOOL MDCCGFloatEqual(CGFloat a, CGFloat b) { | |
const CGFloat constantK = 3; | |
#if CGFLOAT_IS_DOUBLE | |
const CGFloat epsilon = DBL_EPSILON; | |
const CGFloat min = DBL_MIN; | |
#else | |
const CGFloat epsilon = FLT_EPSILON; | |
const CGFloat min = FLT_MIN; | |
#endif | |
return (MDCFabs(a - b) < constantK * epsilon * MDCFabs(a + b) || MDCFabs(a - b) < min); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment