1

I am getting some unexpected value from following code :

- (void) test
{
int target = 0;  
int received = 0;

float result = (float)received/target;

NSLog(@"%.0f",result);
}

in console it prints " nan ". The target and received values may change. but when both are 0 than issue is created. I want to display result's value on label but it prints nan instead of float value.

what is wrong in code ?

Thanks...

Maulik
  • 19,348
  • 14
  • 82
  • 137

5 Answers5

6

IEEE 754 defines that the result of 0.0 / 0.0 is a (silent) NaN.

A positive float divided by zero yields positive infinity. A negative dividend returns negative infinity.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
3

You divide by zero, which is usually an illegal arithmetic operation, will in this case produce an exception in the FPU (due to IEEE 754, which handles floating point arithmetics in microprocessors) and the result will be NaN which means 'Not a Number'.

The variable 'target' has to have a value which is separated from zero or the division will never be executed right. In past times, division by zero was a sure way to get a process to crash.

To catch the Nan you can include math.h and use the function isnan() to decide to proceed with errorhandling or the regular flow.

Tobias Wärre
  • 793
  • 5
  • 11
  • 2
    Division by zero is not an illegal operation but has clearly defined results in IEEE 754. – Nikolai Ruhe Apr 29 '11 at 06:37
  • 1
    @Nikolai: Illegal operation, hence the exception? Having this behaviour defined as an exception is to make sure that a process doesn't crash or otherwise deteriorate, it doesn't make it legal. – Tobias Wärre Apr 29 '11 at 10:52
  • IEEE does not define whether or not floating point exceptions are on or off by default. It does not even define an interface to control the behavior. On Mac OS X (and many other platforms) a floating point exception would be delivered by SIGFPE but is masked off by default. And, b.t.w. even though there might be an exception, the operation still would have a well defined result. So, yes, **no illegal operation** here. – Nikolai Ruhe Apr 29 '11 at 12:59
  • Another wrong statement: The compiler/runtime does not make the result to a NaN but the floating point processor. – Nikolai Ruhe Apr 29 '11 at 13:01
2

You can catch the value with the method

isnan()

It takes a double and returns a bool. You could modify your code like this to work:

- (void) test
{
    int target = 0;  
    int received = 0;

    float result = (float)received/target;

    if (isnan(result))
    {
        result = 0;
    }
    NSLog(@"%.0f",result);
}
ljkyser
  • 999
  • 5
  • 9
  • 1
    As other answers mention it is a result of division by 0, but it can be caught and handled. See here for a similar question http://stackoverflow.com/questions/719417/determine-if-nsnumber-is-nan. You could also check that `target` is 0 and not even try the division in that case. – ljkyser Apr 29 '11 at 06:37
0

If you devide by zero the result is said to be undefined for programming language, in maths it is called infinity..See this , this and this..And nan means NotANumber..

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
0

One other note here that is not mentioned in the other answers yet.

The line...

float result = (float)received/target;

...first casts recevied to (float), then divides by the integer value target. I don't know about the rest of you folks out there, but I do not know what the compiler does with this. I'd have to look it up.

On the other hand, I do know what the compiler does when you divide an int by an int, or a float by a float, ... without looking it up.

So. For the sake of future readers of your code, I encourage you to re-state your line of code as either int-by-int or float-by-float division. If you mean float-by-float, then a line like this would be easier for future readers to understand:

float result = (float) received / (float) target;
DLRdave
  • 13,876
  • 4
  • 53
  • 70