更好的方法来测试符号变化?

eri*_*ric 2 math objective-c

当循环遍历一系列数字(例如0.1,-0.5,1.0,-0.33,......)时,我想要一种方法来测试当前数字是否具有与前一个不同的符号.我的代码在下面但是 - 必须有一个更好的方法..

-(bool)signChanged:(float)prev :(float)value{

    // our value is negative
    if(value < 0.0){

        // previous value is positive or zero
        if(prev >= 0.0) return true;

    // our value is positive
    }else{

        if(prev < 0.0) return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

Ram*_*uri 6

要进行此类快速验证,请使用宏而不是方法:

#define SAME_SIGN(v1,v2) (v1>=0)==(v2>=0)
Run Code Online (Sandbox Code Playgroud)