我有一个CGFloat的数字,我想把它除以一些数字。如何确保除数不等于零或NaN?
答案 0 :(得分:1)
以下示例检查除法前的除数,以确保它不是0或NaN。
CGFloat dividend = 5.f;
CGFloat divisor = 3.f;
if (! (isnan(divisor) || divisor == 0.f)){
CGFloat quotient = dividend / divisor;
}
答案 1 :(得分:0)
做这样的事情:
CGFloat number = 5 //declare the CGFloat
CGFloat divisor = 2 //where this equals whatever you want to divide by
if (divisor != 0 && !(isnan(divisor))){
CGFloat answer = number / divisor;
}