How can I do that? I tried the abs() but it only works for int’s. Is there a built in way to do so?
CGFloat flo = -123;
abs(flo)
this returns 0
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use fabs()
CGFloatis defined as adoubleon 64-bit machines, andfloaton 32-bit machines. If you are targeting both 64 and 32 bit than this answer gets more complicated, but is still correct.You want to use
fabs()because it works ondoubledatatypes which are larger thanfloat. On 32-bit, assigning the return value offabs()(which is adouble) into aCGFloat(which is afloat) is ok ONLY if you are taking the absolute value of aCGFloatorfloat. You would potentially overflow a 32-bitCGFloatif you try to store the absolute value of a largedoublenumber. In short, 64-bit is fine, and on 32-bit don’t mix and matchdoubleandCGFloatand you’ll be fine.The
ABS()macro apparently can have side-effects on some compilers.