0

I searched on the website and I didn't find a problem like this my problem is that in my android application when I do so :

String ET6 = et6.getText().toString();
String ET7 = et7.getText().toString();
String ET8 = et8.getText().toString();
int IET6 = Integer.parseInt(ET6);
int IET7 = Integer.parseInt(ET7);
int IET8 = Integer.parseInt(ET8);
double sqrt1 = (IET7 ^ 2) - 4 *(IET6 * IET8);
sqrt1 = Math.sqrt(sqrt1);

I get a NaN value What is wrong with the calculation ?

sqrt = 3^2 - 4*2*1

sqrt = 9 - 8

sqrt = 1

Then the sqrt root should be 1

3 Answers3

7

The problem here is that 3^2 is not .

Instead, it is 3 XOR 2.

  11 //3
 ^10 //2
 ----
  01 //1

So the result is a negative number (1-8 = -7), and sqrt of that is NaN.

So, you should either do 3*3 or (int)Math.pow(3,2)

dryairship
  • 6,022
  • 4
  • 28
  • 54
2

If the argument is NaN or less than zero, then the result of Math.sqrt(argument) is NaN. Source java documentation link

Abhishek
  • 2,485
  • 2
  • 18
  • 25
2

You are using XOR. You should use pow function from Math class:

double sqrt1 = Math.pow(IET7, 2) - 4 *(IET6 * IET8);
Adnan Isajbegovic
  • 2,227
  • 17
  • 27