I've been trying a lot but it only shows NaN. I'm not sure if I'm doing the right thing.
class Imaginary{
double a = 2;
double b = 3;
double c = 5;
double result = b * b - 4 * a * c;
if(result < 0.0){
double im1 = -2 + (Math.sqrt((result))/ 10);
double im2 = -2 - (Math.sqrt((result))/ 10);
System.out.println("x = " + imaginary1 + " or x = " + imaginary2);
}
}
You need to take negate result to make it positive before taking the square root (taking square roots of negative numbers always results in NaN) and append "i" before printing.
double real = -b / (2*a);
double img = Math.sqrt(-result) / (2*a);
System.out.println("x = " + real + " + " + img +"i or x = " + real + " - " + img + "i");
You shouldn't use sqrt(result) since it will always result in you taking the square root of a negative number (that is your condition for result). Instead try to use a formula (eg completing the square).
Hope it answers your question :)
Since you have a complex root, you need to work with complex numbers to solve the equation. Java lacks builtin support for complex numbers, but you can e.g. use Apache Commons:
if (result < 0.0) {
final Complex cb = new Complex(-b, 0.0);
final Complex root = new Complex(result, 0.0).sqrt();
final Complex r1 = cb.add(root).divide(2 * a);
final Complex r2 = cb.subtract(root).divide(2 * a);
}
Related
So, I wrote a Java program that finds the solutions to a quadratic equation and my problem is I can't seem to write the right code to find the "imaginary numbers" when it prints out I just get "NaN". Any solutions?
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the value for a: ");
double a = scan.nextDouble();
System.out.print("Enter the value for b: ");
double b = scan.nextDouble();
System.out.print("Enter the value for c: ");
double c = scan.nextDouble();
double result = b * b - 4.0 * a * c;
if(result > 0.0){
//to find two real solutions
double x1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double x2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("There are two real solutions.");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
//to find one real solution
} else if(result == 0.0){
double x1 = (-b / (2.0 * a));
System.out.println("There is one real solution");
System.out.println("x = " + x1);
//to find the imaginary numbers
} else if(result < 0.0){
double x1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double x2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("There are two imaginary solutions.");
System.out.println("x1 = " + x1 + " + " + x2);
System.out.println("x2 = " + x1 + " - " + x2);
}
}
}
There are a couple of incorrect things in your code when handling complex roots (when result < 0):
You are attempting to evaluate the square root of result which is negative. This is will result in a NaN. The correct approach is to get the square root of -result to get your answer.
The way you are calculating the roots is incorrect. Both roots will have the same real part which is -b/(2*a) and the same value for imaginary part, differing only in its sign.
I fixed your code below to give a correct output. The real part is calculated and then the imaginary part is calculated. Roots are then printed with the imaginary parts suffixed with an 'i' to denote the imaginary part.
double real = -b / (2*a);
double imag = Math.pow(-result, 0.5) / (2.0 * a);
System.out.println("There are two imaginary solutions.");
System.out.println("x1 = " + real + " + " + imag + "i");
System.out.println("x2 = " + real + " - " + imag + "i");
I have some lines of code that produces a number after calculation which is currently in JavaScript and here is the code:
if ( y2 != y1 )
{
// calculate rate
var f;
var y;
if ( y2 > y1 )
{
f = cpi[y2] / cpi[y1];
y = y2 - y1;
}
else
{
f = cpi[y1] / cpi[y2];
y = y1 - y2;
}
var r = Math.pow(f, 1/y);
r = (r-1)*100;
r = Math.round(r*100) / 100;
System.out.println( "number: " + r.toFixed(2) + "%." );
}
I converted the above JS code to Java and here is the code:
DecimalFormat decimalFormat = new DecimalFormat("0.##");
if (cpi[to] != cpi[from]) {
double f, y;
if (cpi[to] > cpi[from]) {
f = cpi[to] / cpi [from];
y = to - from;
}
else {
f = cpi[from] / cpi[to];
y = from - to;
}
q = Math.pow(f, 1/y);
q = (q-1)*100;
q = Math.round(q*100)/100;
Toast.makeText(getApplicationContext(), "number: " + String.valueOf(decimalFormat.format(q)), 2000).show();
}
The JavaScript code produces: number: 2.39
While the Java code produces number: 2
Why am I getting two different value? I will post what cpi[to], cpi[from], to and from values are if needed.
In this line
q = Math.round(q*100)/100;
both operands of the division operation are integral, therefore the result is also an integral type. Use 100.0 as the divisor to coerce the result to a double.
what is q type?
try to put 100.0 also
If you devide two integers in java the result is integer. Every operation with double and integer or with two doubles creates double.
1)In this line
1)q = Math.round(q*100)/100;
2)you are deviding two integers, so it has same output as:
2)q = (int) (Math.round(q*100)/100);
3)you can use casting to double for example:
3)q = Math.round(q*100)/(double)100;
4)or using the 100.0 which makes this number double:
4)q = Math.round(q*100)/100.0;
5)this should work too, because first the result of Math.round is converted to double and then devided by 100:
5)q = (double)Math.round(q*100)/100;
6)However this WILL NOT work, because first Math.round is devided by 100 and it creates integer, so the result of this operation is rounded down and AFTER then casted to double. So it will be double but still rounded down, because it was rounded before it becomes double.
6)q = (double)(Math.round(q*100)/100);
Most likely your issue is Math.round(double) returns a long value. So d would contain a long instead of a double at that point.
I have posted this question before. But have made a huge edit to it. And would like to ask for help in correcting my steps since my java code is not compiling.
write a method printRoots that given 3 terms as input, representing a,b, and c in that order prints their roots.
We have the following given information
**If b²-4ac is a positive number, your program should print “The two roots are X and Y” where X is the larger root and Y is the smaller root
If b²-4ac equals 0, the program should print. “The equation has one X” where X is the only root
If b²-4ac is a negative number, the program should print.” The equation has two roots(-X1 + Y1i) and (-X2 and Y2i)**
The term can be determined based on:
If b^2 - 4ac is a negative number, then the quadratic equation becomes: (-b+/- √C)/2a -This means the equation can be simplified to become (-b+/- √Ci)/2a where the square root is not a positive number
Calculate the coefficient and print that(i.e X1 is -b/2a and Y1 is sqrt(-C)/2i)
Note: Not allowed to use Scanners for this question
Is it possible for someone to review my program and tell me where I have gone wrong and do i just remove my scanners to make it a program without scanners? How do i enter a,b,c then??
Give note: you should NOT use Scanner inside this method. To test your method you can use SCanner in the main method OR you can hard code values into your code from the main method and then call the method printRoots with those inputs or ouputs.
import java.util.Scanner;//delete this part after
public class findingRoots {
public static void main(String[] args)
{
}
public static double printRoots (){ //should it be double here or int?
//read in the coefficients a,b,and c
Scanner reader = new Scanner(System.in);
System.out.println("Enter the value of a");
int a=reader.nextInt();
System.out.println("Enter the value of b");
int b=reader.nextInt();
System.out.println("Enter the value of c");
int c=reader.nextInt();
//now compte the discrimintat d
double discriminant = (Math.pow(b, 2.0)) - (4 * a * c);
d=Math.sqrt(discriminant);
double X,Y; //root 1 & root 2, respectively
// is the step double X,Y necessary?
double d = (b*b)-(4.0*a*c);
if (discriminant > 0.0){
X = (-b + d)/(2.0 * a ); //X= root 1, which is larger
**//do i put int or double in front of X?**
Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root
String root1 = Double.toString(X)
String root2 = Double.toString(Y)
System.out.println("The two roots are X and Y:" + root1 + "and" + root2);
}
else{
if (discriminant==0.0)
X = (-b + 0.0)/(2.0 * a);//repeated root
String root2 = Double.toString(X)
System.out.println("The equation has one root X:" + root1);//where X is the only root
}
if(discriminant < 0.0){
X1 = -b/(2*a);
Y1 = (Math.sqrt(-C))/(2*a);
X2 = -b/(2*a);
Y2 = (-(Math.sqrt(-C)))/(2*a);
String root1 = Double.toString(X1)
String root2 = Double.toString(Y1)
String root3 = Double.toString(X2)
String root4 = Double.toString(Y2)
System.out.println("The equation has two roots:" + (root1 + (root2)"i") "and" + (root3 + (root4)"i");
// where i represents the square root of negative 1
}
}
}
Ok, so there a quite a bit wrong with the code. Here is a running version of your program. (or at least as close as I can get to what you are trying to do).
//import java.util.Scanner;//delete this part after
public class findingRoots {
public static void main(String[] args) {
double temp = printRoots(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
}
public static double printRoots (int a, int b, int c){ //should it be double here or int?
//read in the coefficients a,b,and c
//now compte the discrimintat d
double discriminant = (Math.pow(b, 2.0)) - (4 * a * c);
double d=Math.sqrt(discriminant);
double X = 0,Y = 0; //root 1 & root 2, respectively
// is the step double X,Y necessary?
d = (b*b)-(4.0*a*c);
if (discriminant > 0.0) {
X = (-b + d)/(2.0 * a ); //X= root 1, which is larger
//do i put int or double in front of X?**
Y = (-b - d)/(2.0 *a); //Y= root 2, which is the smaller root
String root1 = Double.toString(X);
String root2 = Double.toString(Y);
System.out.println("The two roots are X and Y:" + root1 + "and" + root2);
}
else if (discriminant==0.0){
X = (-b + 0.0)/(2.0 * a);//repeated root
String root2 = Double.toString(X);
System.out.println("The equation has one root X:" + root2);//where X is the only root
}
else if (discriminant < 0.0){
double X1 = -b/(2*a);
double Y1 = (Math.sqrt(-c))/(2*a);
double X2 = -b/(2*a);
double Y2 = (-(Math.sqrt(-c)))/(2*a);
String root1 = Double.toString(X1);
String root2 = Double.toString(Y1);
String root3 = Double.toString(X2);
String root4 = Double.toString(Y2);
System.out.println("The equation has two roots:" + root1 + root2 + "and" + root3 + root4);
// where i represents the square root of negative 1
}
return -1;
}
}
To run this code simply type:
java findingRoots 1 2 3 where 1 2 3 are your a, b, c values respectively.
This is not working the way you want it to (i assume). This should get you a start as to trouble shooting though as it can now run. Let's see what you can do from here.
I saw this question of mapping numbers algorithm.
I am trying to implement the #PeterAllenWebb solution in java like this:
long A = 1l;
long B = 999999999l;
long C = 1000000000l;
long D = 9999999999l;
long X = 999999998l;
long Y = (D-C)*(X-A)/(B-A) + C;
System.out.println("original is " + X);
long reverseX = (B-A)*(Y-C)/(D-C) + A;
System.out.println("reverse is " + reverseX);
However, this doesn't always work.
See below:
X reverseX
999999998 999999997
1 1
999999999 999999999
12 11
As you can see, only minimum (A) and maximum (B) are returning fine.
For the rest, I need to add 1. This seems to me as floor/round/Math issue and I don't want to rely on the JVM that calculates it. I would like it to always work.
How can I make the above work for reverseX?
You are facing an age old issue. By default division is double in Java. So if your division result is 1.0 or 1.3 or 1.9 it will be truncated to 1. In your case the same is happening. Try changing it to double from long as below
double A = 1L;
double B = 999999999l;
double C = 1000000000l;
double D = 9999999999l;
double X = 999999998l;
double Y = (D - C) * (X - A) / (B - A) + C;
System.out.println("original is " + new DecimalFormat("#").format(X));
double reverseX = (B - A) * (Y - C) / (D - C) + A;
System.out.println("reverse is "
+ new DecimalFormat("#").format(reverseX));
I am looking to implement the simple equation:
i,j = -Q ± √(Q2-4PR) / 2P
To do so I have the following code (note: P = 10. Q = 7. R = 10):
//Q*Q – 4PR = -351 mod 11 = -10 mod 11 = 1, √1 = 1
double test = Math.sqrt(modulo(((Q*Q) - ((4*P)*R))));
// Works, but why *-10 needed?
i = (int)(((-Q+test)/(P*2))*-10); // i = 3
j = (int)(((-Q-test)/(P*2))*-10); // j = 4
To put it simply, test takes the first part of the equation and mods it to a non-zero integer in-between 0 and 11, then i and j are written. i and j return the right number, but for some reason *-10 is needed to get them right (a number I guessed to get the correct values).
If possible, I'd like to find a better way of performing the above equation because my way of doing it seems wrong and just works. I'd like to do it as the equation suggests, rather than hack it to work.
The quadratic equation is more usually expressed in terms of a, b and c. To satisfy ax2+bx+c = 0, you get (-b +/- sqrt(b^2-4ac)) / 2a as answers.
I think your basic problem is that you're using modulo for some reason instead of taking the square root. The factor of -10 is just a fudge factor which happens to work for your test case.
You should have something like this:
public static void findRoots(double a, double b, double c)
{
if (b * b < 4 * a * c)
{
throw new IllegalArgumentException("Equation has no roots");
}
double tmp = Math.sqrt(b * b - 4 * a * c);
double firstRoot = (-b + tmp) / (2 * a);
double secondRoot = (-b - tmp) / (2 * a);
System.out.println("Roots: " + firstRoot + ", " + secondRoot);
}
EDIT: Your modulo method is currently going to recurse pretty chronically. Try this instead:
public static int modulo(int x)
{
return ((x % 11) + 11) % 11;
}
Basically the result of the first % 11 will be in the range [-10, 10] - so after adding another 11 and taking % 11 again, it'll be correct. No need to recurse.
At that point there's not much reason to have it as a separate method, so you can use:
public static void findRoots(double a, double b, double c)
{
int squareMod11 = (((b * b - 4 * a * c) % 11) + 11) % 11;
double tmp = Math.sqrt(squareMod11);
double firstRoot = (-b + tmp) / (2 * a);
double secondRoot = (-b - tmp) / (2 * a);
System.out.println("Roots: " + firstRoot + ", " + secondRoot);
}
You need to take the square root. Note that Q^2-4PR yields a negative number, and consequently you're going to have to handle complex numbers (or restrict input to avoid this scenario). Apache Math may help you here.
use Math.sqrt for the square root. Why do you cast i and j to ints? It is equation giving you roots of square function, so i and j can be any complex numbers. You shall limit the discriminant to positive-only values for real (double) roots, otherwise use complex numbers.
double test = Q*Q - 4*P*R;
if(Q < 0) throw new Exception("negative discriminant!");
else {
test = Math.sqrt(test);
double i = (-Q + test) / 2*P;
double i = (-Q - test) / 2*P;
}
Why are you doing modulo and not square root? Your code seems to be the way to get the roots of a quadratic equation ((a±sqrt(b^2-4ac))/2a), so the code should be:
double delta = Q*Q-4*P*R);
if(delta < 0.0) {
throw new Exception("no roots");
}
double d = Math.power(delta,0.5);
double r1 = (Q + d)/(2*P)
double r2 = (Q - d)/(2*P)
As pointed out by others, your use of mod isn't even wrong. Why are you making up mathematics like this?
It's well known that the naive solution to the quadratic equation can have problems if the value of b is very nearly equal to the discriminant.
A better way to do it is suggested in section 5.6 of "Numerical Recipes in C++": if we define
(source: equationsheet.com)
Then the two roots are:
and
Your code also needs to account for pathological cases (e.g., a = 0).
Let's substitute your values into these formulas and see what we get. If a = 10, b = 7, and c = 10, then :
(source: equationsheet.com)
Then the two roots are:
(source: equationsheet.com)
and
(source: equationsheet.com)
I think I have the signs right.
If your calculation is giving you trouble, it's likely due to the fact that you have complex roots that your method can't take into account properly. You'll need a complex number class.