Skip to content
Snippets Groups Projects
Commit 6b637279 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispiele und Notizen 27.6.2024

parent 70c0c708
No related branches found
No related tags found
No related merge requests found
Showing
with 556 additions and 0 deletions
Aufgabe: Schreiben Sie die Sinusfunktion selbst.
~~~~~~~~
- Wir setzen nur die Grundrechenarten voraus.
- möglichst effizient
Lösungsansätze:
- Taylor-Reihe: sin x = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + ...
- Kleinwinkelnäherung: Spezialfall der Taylor-Reihe
- Verallgemeinerung der Taylor-Reihe: mathematische Folge, die gegen die Lösung konvergiert
- Differentialgleichung lösen: f''(x) = -f(x) mit f(0) = 0, f'(0) = 1
- möglichst effizient: Tabelle abspeichern, interpolieren
- Drehmatrizen --> CORDIC-Algorithmus
.----------------------------------------------------------------------------.
| Bis 17:45 Uhr bitte einen selbstgewählten Lösungsansatz implementieren, |
| so daß der berechnete Sinus mit dem Sinus der Laufzeitbibliothek möglichst |
| gut übereinstimmt. Dann vergleichen wir die Ergebnisse in Hinblick auf |
| Genauigkeit, Verbrauch an Rechenzeit, Verbrauch an Speicherplatz. |
| (Die verwendete Computersprache ist beliebig. Aber bitte erklären können.) |
`----------------------------------------------------------------------------'
pi = 3.1415926535897932384626433...
22/7 = 3.1428...
355/113 = 3.14159292...
(siehe auch: https://xkcd.com/2205/)
sin (218 * 3.14159265358979323846 / 180) = -0.61566147532565827966
sin (45 * 3.14159265358979323846 / 180) = 0.70710678118654752439
sin (360 * 3.14159265358979323846 / 180) = 0.00000000000000000000
sin (90 * 3.14159265358979323846 / 180) = 1.00000000000000000000
sin (30 * 3.14159265358979323846 / 180) = 0.49999999999999999999 = 0.5
Taylor-Reihe bis 10: 5-6 Stellen Genauigkeit
längere Taylor-Reihe: 15-16 Stellen Genauigkeit
längere Taylor-Reihe rückwärts: Double-Genauigkeit :-)
Achtung: Wenn man stark unterschiedliche Gleitkommazahlen addiert,
verliert man Rechengenauigkeit!
Beispiel: Rechenwerk mit 5 Dezimalstellen Genauigkeit
1 = 1e0
1000 = 1e3
123.45 = 1.2345e2
123.45 + 0.0123 = 123.46 <-- Wir verlieren 2 Stellen Genauigkeit!
123.45 + 123.45 + 123.45 + ... + 123.45 + 123.45 (101 Summanden)
`-------------------v-----------------'
Summe so groß, daß der letzte Summand nur noch sehr ungenau zum Zuge kommt
(vereinfacht: 12345.0 + 123.45 = 12468.0, wir verlieren 2 Stellen)
--> Beim Aufsummieren erst kleine Zahlen addieren,
so daß man insgesamt immer ungefähr gleich große Zahlen addiert
Drehmatrizen: (siehe auch: https://xkcd.com/184/)
. .
| cos x -sin x |
| | dreht einen Vektor um den Winkel x
| sin x cos x |
` '
... kann ich hintereinanderschalten.
Sammlung von Drehmatrizen: Drehung um pi/2, pi/4, pi/8, pi/16, ...
--> Winkel x liegt binär vor --> aus abgespeicherten Matrizen schnell eine zusammensetzen,
die um den Winkel x dreht
--> CORDIC-Algorithmus
#include <stdio.h>
struct Hello
{
void hello ();
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
int main ()
{
Hello h;
h.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ();
Hello ();
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
Hello::Hello ()
{
printf ("constructor: Hello::Hello\n");
}
int main ()
{
Hello h;
h.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ();
Hello ();
virtual ~Hello ();
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
Hello::Hello ()
{
printf ("constructor: Hello::Hello\n");
}
Hello::~Hello ()
{
printf ("destructor: Hello::~Hello\n");
}
int main ()
{
Hello h;
h.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ();
Hello ();
virtual ~Hello ();
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
Hello::Hello ()
{
printf ("constructor: Hello::Hello\n");
}
Hello::~Hello ()
{
printf ("destructor: Hello::~Hello\n");
}
int main ()
{
Hello h1;
Hello h2 = h1;
h2.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ();
Hello ();
Hello (const Hello &h);
virtual ~Hello ();
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
Hello::Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello::Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
Hello::~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
int main ()
{
Hello h1;
Hello h2 = h1;
h2.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ();
Hello ();
Hello (const Hello &h);
virtual ~Hello ();
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
Hello::Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello::Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
Hello::~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
int main ()
{
Hello h1;
Hello h2;
h2 = h1;
h2.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ();
Hello ();
Hello (const Hello &h);
virtual ~Hello ();
operator = (const Hello &h);
};
void Hello::hello ()
{
printf ("Hello, world!\n");
}
Hello::Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello::Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
operator Hello::= (const Hello &h)
{
printf ("operator: Hello = Hello\n");
}
Hello::~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
int main ()
{
Hello h1;
Hello h2;
h2 = h1;
h2.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ()
{
printf ("Hello, world!\n");
}
Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
virtual ~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
Hello &operator = (const Hello &h)
{
printf ("operator: Hello = Hello\n");
return *this;
}
};
int main ()
{
Hello h1;
Hello h2;
h2 = h1;
h2.hello ();
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ()
{
printf ("Hello, world!\n");
}
Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
virtual ~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
Hello &operator = (const Hello &h)
{
printf ("operator: Hello = Hello\n");
return *this;
}
};
int main ()
{
Hello *h1 = new Hello ();
Hello *h2 = new Hello (h1);
h2->hello ();
delete h1;
delete h2;
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ()
{
printf ("Hello, world!\n");
}
Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
virtual ~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
Hello &operator = (const Hello &h)
{
printf ("operator: Hello = Hello\n");
return *this;
}
};
int main ()
{
Hello *h1 = new Hello ();
Hello *h2 = new Hello (*h1);
h2->hello ();
delete h1;
delete h2;
return 0;
}
#include <stdio.h>
struct Hello
{
void hello ()
{
printf ("Hello, world!\n");
}
Hello ()
{
printf ("constructor: Hello::Hello ()\n");
}
Hello (const Hello &h)
{
printf ("constructor: Hello::Hello (const Hello &h)\n");
}
virtual ~Hello ()
{
printf ("destructor: Hello::~Hello ()\n");
}
Hello &operator = (const Hello &h)
{
printf ("operator: Hello = Hello\n");
return *this;
}
};
int main ()
{
Hello *h1 = new Hello ();
Hello *h2 = new Hello (*h1);
h2->hello ();
return 0;
}
#include <iostream>
int main ()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello, world!" << endl;
return 0;
}
#include <iostream>
using std::cout, std::endl;
int main ()
{
cout << "Hello, world!" << endl;
return 0;
}
#include <iostream>
using std::cout, std::endl;
namespace greeting
{
const char *hello = "Hello, world!";
}
int main ()
{
cout << hello << endl;
return 0;
}
#include <iostream>
using std::cout, std::endl;
namespace greeting
{
const char *hello = "Hello, world!";
}
int main ()
{
cout << greeting::hello << endl;
return 0;
}
#include <iostream>
using std::cout, std::endl;
class greeting
{
public:
const char *hello = "Hello, world!";
};
int main ()
{
cout << greeting::hello << endl;
return 0;
}
#include <iostream>
using std::cout, std::endl;
class greeting
{
public:
const char *hello = "Hello, world!";
};
int main ()
{
greeting Hello;
cout << Hello.hello << endl;
return 0;
}
#include <iostream>
using std::cout, std::endl;
class greeting
{
public:
static const char *hello = "Hello, world!";
};
int main ()
{
cout << greeting::hello << endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment