Autor Beitrag
Hazer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54

Win XP Pro SP2
Turbo Delphi 2006
BeitragVerfasst: Di 15.02.05 17:52 
Hi, nettes Board :wink:
nun zum Topic :D
----

Tscha, wie es auch anders sein soll kiegt man in der Schule nur komische Aufgaben :(
wie zum Beispiel diese hier:
'Zeige anhand eines Delphieprogramms, was im Computer abläuft, wenn man die Wurzel einer Zahl ausgeben möchte!'

Na super, voll unnütz :evil:

Na aj habe mir schonma so viel zurecht gemacht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
program myprogram;

{$APPTYPE CONSOLE}

uses
  SysUtils;
  var
  zahl,a,a2,b,b2:real;
  trueheit:boolean;
begin
  write('wurzelzahl: ');
  readln(zahl);
  a:=1;
  b:=zahl;
  repeat
    b2:=(a+b)/2;
    a2:=zahl/b2;
    a:=a2;
    b:=b2;
    if abs(zahl-(a*b))<0.00000001 then trueheit:=true;
  until trueheit;
  writeln('Die wurzel von ',zahl:9:9,' = ',a:9:9);
  readln;
end.


Doch kommt nicht 1,4... raus, sondern nur 1.3333 (periode)
Aber müsste doch der Code richtig sein, oder?
CenBells
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1547

Win 7
Delphi XE5 Pro
BeitragVerfasst: Di 15.02.05 18:22 
Hi

Von welcher zahl willst du überhaupt die wurzel haben?
1,4... ich tippe mal auf zwei, ist dem so?

Edit: Aber auch sonst ist der code nicht ganz richtig. Ich vermute, du willst die intervallschachtelungsmethode machen, oder?
Deine Auswertung enthält if abs(zahl-(a*b))<0.00000001 then trueheit:=true;
Wenn du nun aber mal einsetzt, was a und b enthält, dann steht da
ausblenden Delphi-Quelltext
1:
if abs(zahl-(zahl/b2*b2))<0.00000001 then trueheit:=true;					

Wenn du das da kürzt, bekommst du
ausblenden Delphi-Quelltext
1:
if abs(zahl-zahl)<0.00000001 then trueheit:=true;					

und dann willst du also wissen, ob 0 < 0.00000001 ist.

seltsam...


Gruß
Ken

_________________
Eine Klasse beschreibt die Struktur und das Verhalten einer Menge gleichartiger Objekte.


Zuletzt bearbeitet von CenBells am Di 15.02.05 18:27, insgesamt 1-mal bearbeitet
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Di 15.02.05 18:26 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
  var
  zahl,a,a2,b,b2: real;
  trueheit: boolean = false;
begin
  write('wurzelzahl: ');
  readln(zahl);
  a := 1;
  b := zahl;
  repeat
    b2:=(a+b)/2;
    a2:=zahl/b2;
    a:=a2;
    b:=b2;
    if abs(a-b)< 0.00000001 then trueheit:=true;
  until trueheit;
  writeln('Die wurzel von ',zahl:9:9,' = ',a:9:9);
  readln;
end.


so ist es richtig, du musst schaun ob a-b gleich ist weil a*b immer = zahl ist auch beim ersten druchlauf ;>
zemy
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 207

Win XP Prof.
D7
BeitragVerfasst: Di 15.02.05 18:38 
wieso nicht das (vereinfachte) newtonsche Näherungsverfahren:
ausblenden Delphi-Quelltext
1:
2:
for i:=0 to 10000 do
  y:=(y*y+x)/(2*y);


auf y legst du ein Startwert, x ist der Wert unter der Wurzel. Klappt einwandfrei^^

Ergebnis = exp(ln(x)/2) würde auch funzen^^

Bedarf an weiteren Ansätzen? de.wikipedia.org/wik...zel_%28Mathematik%29

MfG Zemy

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
Hazer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 54

Win XP Pro SP2
Turbo Delphi 2006
BeitragVerfasst: Mi 16.02.05 07:30 
jo, danke und tschuldigung, das mit der 2 hatte ich vergessen zu erwähnen :?

Die for schleife dürfen wir noch nicht verwenden (frag mich nicht, warum :cry: )

Danke erstma an alle.
Funtzt jetzt ;)
minnime
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 171

Win 7
Delphi Prism 2011, C# (VS 2010)
BeitragVerfasst: Mi 16.02.05 10:34 
Soviel macht der Rechner wenn man eine Wurzel zieht? Dann ist das doch eine ziemlich performancelastige Funktion, oder? Wie krieg ich eigentlich raus welche Algorithmen wieviel Rechenleistung verbrauchen (ich weiß gehöhrt nicht zum Thema)?