0

Trouble with Java program for solving Quadratic Formula

When I run the program I get an issue where the roots come out as Na and do not give me an answer and I am having trouble figuring it out. https://code.sololearn.com/cPniQ1yT2anr/?ref=app

18th Jul 2020, 8:00 PM
Angel Armndariz
Angel Armndariz - avatar
3 Réponses
+ 1
What input you are giving..? If Math.sqrt( negetive numbers) return NaN. So take b*b - 4ac absolute value.. root1 = - b + Math.sqrt(Math.abs((b*b)-(4*a*c))); [But I think result may differ. Check ones...]
18th Jul 2020, 9:05 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I have been using positive numbers as my input, I haven't tried any negative numbers yet
18th Jul 2020, 10:13 PM
Angel Armndariz
Angel Armndariz - avatar
0
Angel Armndariz I mean result of b*b - 4*a*c Ex: 5 4 3 Now 4 * 4 - 4*5*3 =16-60= -46 Math.sqrt( -46) is NaN. But chech input 2 5 2 Result you get 5*5 - 4*2*2 =25-16=9 Math.sqrt(9) =3 Check for these two cases..... You can understand.
18th Jul 2020, 10:23 PM
Jayakrishna 🇮🇳