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
Post a Comment