Wurzelrechnung, wie?



  • Wer haette das gedacht, jetzt brauch ich die Wurzelrechnung.

    Und da ich schon ganz weich im Kopf bin, versuch ich garnicht erst das selber herraus zu finden. 🙄

    Danke im vorraus.



  • double sqrt ( double x );



  • Danke fuer die schnele Antwort. 🙂



  • Und noch so als kleiner Tip:
    http://www.cplusplus.com/ref/cmath/
    Da findet man auch gleich kleine Beispiele!



  • Und falls du eine andere Wurzel brauchst denk einfach daran, das die n-te Wurzel dem Potenzieren mit (1/n) entspricht.



  • edit: Irrtum meinerseits



  • Eigener Code:

    #include <iostream.h>
    
    const int precise=1000;
    double sqt(double x); //Wurzelfunktion nach Heron
    
    int main()
    {
    	double in;
    	cin>>in;
    	cout<<sqt(in)<<endl;
    
    	return 0;
    }
    
    double sqt(double x)
    {
    	if(x>=0){
    		double value=x; //Startwert
    
    		for(int i=0;i<precise;i++)
    		{
    			value=(value+x/value)/2;
    		}
    
    		return value;
    	}else
    		return -1;
    }
    

    Aber benutze selbstverständlich lieber die Standardbibliothek.
    Wollt ich nur dem Verständnis halber, wie so eine Funktion arbeitet, einbringen.



  • wie arbeitet denn sqrt() wirklich?



  • Aus dem Quake 2 Quellcode

    #include <iostream>
    
    using namespace std;
    
    float Sqrt( const float number )
    {
     long i;
     float x2, y;
     const float threehalfs = 1.5f;
    
     x2 = number * 0.5F;
     y  = number;
     i  = * ( long * ) &y;            // evil floating point bit level hacking
     i  = 0x5f3759df - ( i >> 1 );   // what the fuck?
     y  = * ( float * ) &i;
     y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
     y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
    
     return y * number;
    }
    
    int main()
    {
     cout<<sqrt(2345.9)<<endl;
     cout<<Sqrt(2345.9)<<endl;
    
     return 0;
    
    }
    

    Wirklich verstanden hab ichs nie 😉


Log in to reply