NaN

AlphaVoice

Mitglied
Hey,

ich will Java folgendes berechnen lassen:

Java:
double v = Math.pow(-q / 2.0 - Math.sqrt(discr), (1.0 / 3.0));
wobei
Code:
double q = -0.5
und
Code:
double discr = 0.2609953703703704

Wenn ich v ausgeben will, zeigt mir die Konsole "NaN", also "Not a Number" an. Ich vermute mal stark, dass der Compiler irgendeine Klammer nicht beachtet und die Wurzel aus einer negativen Zahl zieht.

Wisst ihr, wieso dieser Fehler zustande kommt?
 
M

Marcinek

Gast
ja du musst statt -q => q * -1.0 schreiben

Außerdem kann man mit pow nicht 1/3 potenzieren.
 
Zuletzt bearbeitet von einem Moderator:

AlphaVoice

Mitglied
-q / 2.0 - Math.sqrt(discr)

Ist negativ, davon kann man keine Wurzel ziehen.
Quatsch, discr ist doch positiv. ???:L

Außerdem kann man mit pow nicht 1/3 potenzieren.
Wieso sollte man mit 1/3 nicht potenzieren können? Ich nutze es als Alternative, weil es in Java keine Funktion für Kubikwurzeln gibt.

ja du musst statt -q => q * -1.0 schreiben
Geht auch nicht, macht aus meiner Sicht irgendwie auch keinen Sinn.
 

HoaX

Top Contributor
Also bei...

Code:
double u = Math.pow(-q / 2.0 + Math.sqrt(discr), (1.0 / 3.0));
geht das einwandfrei. Nur bei dem Term im Startpost geht es nicht.

Weil "-q / 2.0 + Math.sqrt(discr)" > 0 ist.
In der Javadoch steht unteranderem:
javadoc hat gesagt.:
If the first argument is finite and less than zero
if the second argument is finite and not an integer, then the result is NaN.

In der Aufzählung der ganzen Fälle ist das der vorletzte Hauptpunkt.
 

HoaX

Top Contributor
Quatsch, discr ist doch positiv. ???:L


Wieso sollte man mit 1/3 nicht potenzieren können? Ich nutze es als Alternative, weil es in Java keine Funktion für Kubikwurzeln gibt.

a) Ich habe da einen ganzen Term geschrieben, nicht nur discr ...

b) mit 1/3 potenzieren geht manchmal, aber nicht immer. Die Ausnahmen listet die Javadoc auf.
 

AlphaVoice

Mitglied
Achso, vielleicht sollte ich für solche Feinheiten doch mehr die Javadoc lesen.
Hat irgendwer eine Idee, wie ich diesen Fehler umgehen könnte? Hilft es wenn ich "q * (-1)" nutze?
 

sambalmueslie

Bekanntes Mitglied
Java:
final double q = -0.5;
final double discr = 0.2609953703703704;
final double a = (-1) * q / 2.0;
final double b = Math.sqrt(discr);
final double c = (1.0 / 3.0);
final double v = Math.pow(a - b, c);
System.out.println("(" + a + "-" + b + ")^" + c + " = " + v);

Ergibt einen negativen Inhalt der Klammer ;) wo du versuchst die dritte Wurzel zu ziehen.
Scheint also korrekt zu rechnen. Stimmen vielleicht die Zahlen nicht??
 

AlphaVoice

Mitglied
Ich poste mal den ganzen Code... ist zwar noch nicht fertig und teils fehlerhaft, aber was soll's...

Java:
import java.lang.Math;
import java.util.Scanner;
import java.io.*;

class Complex {
	double real;
	double imag;

	Complex(double real, double imag) {
		this.real = real;
		this.imag = imag;
	}
	
	// real part
	public double real() {
		return real;
	}
	
	// imaginary part
	public double imag() {
		return imag;
	}
	
	// addition
	public Complex add(Complex cpx) {
		return new Complex(this.real + cpx.real(), this.imag + cpx.imag());
	}
	
	// subtraction
	public Complex sub(Complex cpx) {
		return new Complex(this.real - cpx.real(), this.imag - cpx.imag());
	}
	
	// multiplication
	public Complex mul(Complex cpx) {
		return new Complex(this.real * cpx.real() - this.imag * cpx.imag(), this.real * cpx.imag() + this.imag * cpx.real());
	}
	
	// TODO: implement division
	
	// absolute value
	public double abs() {
		return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2));
	}
	
	// converts complex number to string
	public String toString() {
		if (this.real != 0 && this.imag > 0) {
			return this.real +" + "+ this.imag +"i";
		}
		
		if (this.real != 0 && this.imag < 0) {
			return this.real +" - "+ Math.abs(this.imag) +"i";
		}
		
		// returns only real part
		if (this.imag == 0) {
			return String.valueOf(this.real);
		}
		
		// returns only imaginary part
		if (this.real == 0) {
			return this.imag +"i";
		}
		
		return this.real +" + "+ this.imag +"i";
	}
}

public class CubicEquation {
	// solves the cubic equation ax^3 + bx^2 + cx + d = 0 with cardano's method
	public static Complex[] solve(double a, double b, double c, double d) {
		// x^3 + rx^2 + sx + t = 0
		double r = b / a;
		double s = c / a;
		double t = d / a;
		
		// y^3 + py + q = 0
		double p = s - Math.pow(r, 2) / 3;
		double q = 2 * Math.pow(r, 3) / 27 - s * r / 3 + t;
		
		// discriminant
		double discr = Math.pow((q / 2), 2) + Math.pow((p / 3), 3);
		// array for results
		Complex[] res = new Complex[3];
		
		double u = Math.pow(-q / 2.0 + Math.sqrt(discr), (1.0 / 3.0));
		double v = Math.pow(-q / 2.0 - Math.sqrt(discr), (1.0 / 3.0));
		// WHY IS THIS NAN HRRRRR
		System.out.println(v);
		
		if (d > 0) {
			double y1 = u + v;
			Complex y2 = new Complex(-(u + v) / 2, ((u - v) / 2) * Math.sqrt(3));
			Complex y3 = new Complex(-(u + v) / 2, -((u - v) / 2) * Math.sqrt(3));
			
			double x1 = y1 - r / 3;
			Complex x2 = new Complex(y2.real() - r / 3, y2.imag());
			Complex x3 = new Complex(y3.real() - r / 3, y3.imag());
			
			res[0] = new Complex(x1, 0);
			res[1] = x2;
			res[2] = x3;
		}
		
		if (d == 0) {
			double y1 = u + v;
			double y2 = -u;
			double y3 = -v;
			
			res[0] = new Complex(y1, 0);
			res[1] = new Complex(y2, 0);
			res[2] = new Complex(y3, 0);
		}
		
		if (d < 0) {
			// casus irreducibilis
		}
		
		return res;
	}
	
	public static void main(String[] args) {
	}
}
Genau genommen, ist nur der Code von Zeile 78 bis 90 interessant.
 

Neue Themen


Oben