2

This gives me 0:

int B=-4;
double A = Math.Sqrt(1/B);

but this NaN

double A = Math.Sqrt(-4);

How is possible that the first calculation does not fail or at least does not return NaN as well as there is a square root of a negative number

EDIT: Thanks for the answers. I will add this one:

 int B=0;
 double A = 1/Math.Sqrt(B);

How come this is not the Zero divide exception?!

2
  • You're not getting an exception, because double can represent the result of diving by zero as +Infinity.
    – svick
    Apr 21, 2013 at 14:00
  • 2
    If you have a second question then mark this question as answered and start a new question. It makes it hard on the people trying to answer the question if more questions keep on appearing and it is not clear which ones have answers. Apr 21, 2013 at 14:18

2 Answers 2

12

This here

int B=-4;
double A = Math.Sqrt(1/B);

is the same as

double A = Math.Sqrt(0);

because if you divide an integer by an integer you will get an integer

4

Your first code is equivalent to:

int B = -4;
int C = 1 / B;
double A = Math.Sqrt(C);

Now do this:

Console.WriteLine(C);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.