Search code examples
c++iosarmclangdivide-by-zero

Change default divide by 0 behavior in C++/iOS


I am building an iOS application.

I have the following code:

if(pbCB == 0) { //Don't divide by 0
    c = 1;
} else {
    c = sqrt(pb / pbCB) * PROTANOPIA_WBP;
}

I really want to get rid of the if statement (the code above is within a for-loop).

I know that doing floating point division by 0 and then casting that value to an unsigned char gives (using gdb to test):

//floating-point division by 0
p 10.0/0
$1 = inf

//casted to an unsigned char
p (unsigned char) (10.0/0)
$2 = 0 '\000'

What I'm wondering is if there is a way to change the definition of division by 0 so that it returns 1? I was told by a professor that this is a hardware/architecture problem and that there is no way to do it, but I wanted to see if maybe that wasn't the case. Thanks for any answers/thoughts/advise.


Solution

  • No, you can't. It's specified by the floating point specification and implemented in the hardware.