-4

Have the source code. Content of the function func is unknown:

#include <stdio.h>
#include <math.h>
float func (void)
{
  // Black box
}

int main (void)
{
  float f1, f2;
  int r1, r2;

  f1 = 5.0f;
  f2 = func();

  r1 = (f1 > f2);
  r2 = (f1 <= f2);

  printf ("r1=%d r2=%d\n", r1, r2); 
  return 0;
}

Need to write the contents of the function func, to print out the message:

r1=0 r2=0

Answer:

return pow(-5.0, 0.5);

or

return 0.0/0.0;

Can't understand, why so?

Amazing User
  • 3,473
  • 10
  • 36
  • 75

1 Answers1

1

Regarding the answer

return 0.0/0.0;

it will return NaN. Now in this case both r1 and r2 become 0.

If you return

return pow(-5.0, 0.5); 

the square root of a negative number is imaginary, and thus not representable as a real floating-point number, and so is represented by NaN. In this case both of r1 and r2 is 0.
You function would look like:

float func (void)
{
     return 0.0/0.0 // return pow(-5.0, 0.5);
}  

Another option is that, just return NaN. (include <math.h>).

float func (void)
{
    return NAN;
}
haccks
  • 104,019
  • 25
  • 176
  • 264