iphone - unexpected values from division -


i getting unexpected value following code :

- (void) test { int target = 0;   int received = 0;  float result = (float)received/target;  nslog(@"%.0f",result); } 

in console prints " nan ". target , received values may change. when both 0 issue created. want display result's value on label prints nan instead of float value.

what wrong in code ?

thanks...

you divide zero, illegal arithmetic operation, in case produce exception in fpu (due ieee 754, handles floating point arithmetics in microprocessors) , result nan means 'not number'.

the variable 'target' has have value separated 0 or division never executed right. in past times, division 0 sure way process crash.

to catch nan can include math.h , use function isnan() decide proceed errorhandling or regular flow.


Comments