java - Simple Program Does Not Work Correctly -



java - Simple Program Does Not Work Correctly -

import java.util.scanner; public class welcome { public static void main(string[] args) { scanner input = new scanner(system.in); double = input.nextdouble(); double b = input.nextdouble(); double c = input.nextdouble(); double rezultat = - b + math.sqrt(math.pow(b, 2) - 4 * * c) / 2 * a; system.out.println(rezultat); } }

i wonder why code not work should.

printed result always: nan

math.sqrt(math.pow(b, 2) - 4 * * c)

this portion homecoming nan. if plug in values of (1, 0, 1) x^2 + 1, nan never crosses x line.

in other words, if math.pow(b, 2) - 4 * * c) < 0, should expect nan.

if trying quadratic formula, proper equation be:

double rezultat = (-b + math.sqrt(math.pow(b, 2) - 4 * * c)) / (2 * a);

you need take business relationship order of operations, more in java. original operation have returned not have been x.

additionally, there 2 expected results +/- quadratic.

double rezultat2 = (-b - math.sqrt(math.pow(b, 2) - 4 * * c)) / (2 * a);

the rough programme output should be:

double bsqm4ac = (math.pow(b, 2) - 4 * * c); if (bsqm4ac < 0.0) {// no intersections of x axis i.e. x^2+2 system.out.println("no value found"); } else if (bsqm4ac == 0.0) { //one intersection of x axis i.e. x^2 system.out.println(-b / (2 * a) + 0.0); // can -0.0 here if // b = 0, because doubles } else { //two intersections of x axis ie. x^2 - 1 system.out.println((-b - bsqm4ac) / (2 * a) + "," + (-b + bsqm4ac) / (2 * a)); }

java

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -