Autor Beitrag
GericasS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Di 04.03.08 13:58 
Hey,

folgendes Problem

ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
function Summ(n: integer): Integer;
begin
  Result := 0;
  while n >= 1 do
  begin
    Inc(Result, n);
    Dec(n);
  end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
  i,jahre,njahre : Integer ;
  rb,ab,z1,z2 : Real ;
begin
  rb := StrToFloat(edit1.text);
  jahre := StrToInt(edit2.text);
   if RadioButton1.Checked then
    begin
    ab := rb/jahre ;
    i := 0 ;
    for i := 1 to Jahre+1 do begin
      StringGrid1.RowCount:=(i+1);
      StringGrid1.Cells[0,i]:=IntToStr(i);
      StringGrid1.Cells[1,i]:=FloatToStr(ab);
      StringGrid1.Cells[2,i]:=FloatToStr(rb);
      rb := rb-ab ;
    end;
   end;
   if RadioButton2.Checked then
    begin
    i := 0 ;
    for i := 1 to Jahre+1 do begin
      ab := rb*(2/Jahre) ;
      StringGrid1.RowCount:=(i+1);
      StringGrid1.Cells[0,i]:=IntToStr(i);
      StringGrid1.Cells[1,i]:=FloatToStrF(ab,ffFixed,10,2);
      StringGrid1.Cells[2,i]:=FloatToStrF(rb,ffFixed,10,2);
      rb := rb-ab ;
    end;
   end;
   if RadioButton3.Checked then
    begin
    i := 0 ;
    for i := 1 to Jahre+1 do begin
      njahre:=Summ(njahre);
ab :=(rb/njahre);
      StringGrid1.RowCount:=(i+1);
      StringGrid1.Cells[0,i]:=IntToStr(i);
      StringGrid1.Cells[1,i]:=FloatToStrF(ab,ffFixed,10,2);
      StringGrid1.Cells[2,i]:=FloatToStrF(rb,ffFixed,10,2);
      rb := rb-ab ;
    end;
   end;
end;
.....


Hier bekomme ich den Fehler "Gleitkommadivision durch Null" wo liegt der Fehler ?

:oops:

ich steh mal wieder aufm schlauch... :?


LG

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Di 04.03.08 14:06 
Ich würde nJahre erstmal einen Wert zuweißen, bevor Du der Funktion die Variable übergibst.

Mfg,
Marc.
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Di 04.03.08 14:13 
user profile iconMarc. hat folgendes geschrieben:
Ich würde nJahre erstmal einen Wert zuweißen, bevor Du der Funktion die Variable übergibst.

Mfg,
Marc.

Macht er doch, nur ist das Ergebnis der Funktion Summ=0 und damit kommt es zur Fehlermeldung!

Teste, ob njahre=0 ist wenn ja -> Fehler, wenn nein -> dividiere

PS: Weder jetzt noch irgendwann schafft es jemand einen Euro auf niemanden aufzuteilen!

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
nagel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 708

Win7, Ubuntu 10.10

BeitragVerfasst: Di 04.03.08 14:20 
user profile iconKroko hat folgendes geschrieben:
user profile iconMarc. hat folgendes geschrieben:
Ich würde nJahre erstmal einen Wert zuweißen, bevor Du der Funktion die Variable übergibst.

Mfg,
Marc.

Macht er doch, ...


Macht er nicht.
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Di 04.03.08 14:39 
Okay, flasch interpretiert, der Fehler liegt aber darin, dass njahre 0 ist und damit die Summ-func. 0 zurück gibt!

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Di 04.03.08 14:44 
Und das liegt daran, dass bereits beim ersten Aufruf von Summ die Integer-Größe überschritten wird. Folge: Zahl wird negativ dargstellt und die Funktion gibt Null zurück.
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Di 04.03.08 14:45 
Hm user profile iconKroko muss recht haben. Wobei ich nicht sehe, wo er Null übergibt und deshalb auch Null zurückgegeben wird.
Ganz andere Sache von wegen Laufzeit: Addition der natürlichen Zahlen bis n kann man auch als n*(n+1)/2 schreiben das ist dann Laufzeit O(0) statt O(1)
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Di 04.03.08 14:48 
user profile iconMarc. hat folgendes geschrieben:
Und das liegt daran, dass bereits beim ersten Aufruf von Summ die Integer-Größe überschritten wird. Folge: Zahl wird negativ dargstellt und die Funktion gibt Null zurück.

njahre ist unbestimmt, das kann von -maxint bis +maxint irgendetwas sein!

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Di 04.03.08 15:05 
[quote="user profile iconAllesquarks"]Hm user profile iconKroko muss recht haben. Wobei ich nicht sehe, wo er Null übergibt und deshalb auch Null zurückgegeben wird.
Ganz andere Sache von wegen Laufzeit: Addition der natürlichen Zahlen bis n kann man auch als n*(n+1)/2 schreiben das ist dann Laufzeit O(0) statt O(1)[/quote0]

hab ich jetzt auch so gemacht =) also problem gelöst !

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Di 04.03.08 15:07 
[quote="user profile iconGericasS"]
user profile iconAllesquarks hat folgendes geschrieben:
Hm user profile iconKroko muss recht haben. Wobei ich nicht sehe, wo er Null übergibt und deshalb auch Null zurückgegeben wird.
Ganz andere Sache von wegen Laufzeit: Addition der natürlichen Zahlen bis n kann man auch als n*(n+1)/2 schreiben das ist dann Laufzeit O(0) statt O(1)




hab ich jetzt auch so gemacht =) Danke an alle anderen beim helfen =)

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 04.03.08 15:46 
[OT] Soso...das Verfahren von Gauß (der hat das ja erfunden) hat also eine Laufzeit von O(0), was? Das heißt, die Berechnung geht von alleine. Interessante Idee. :gruebel: Ich würde sagen, die Laufzeit sinkt von O(n) (linearer Aufwand) bei der Addition in einer Schleife auf O(1) (konstanter Aufwand) bei der Gauß-Formel. ;-)

Soll jetzt nicht heißen, dass alles Quark ist, was user profile iconAllesquarks erzählt, aber in diesem Fall wars das schon. :mrgreen:
[/OT]

_________________
We are, we were and will not be.
Allesquarks
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 510

Win XP Prof
Delphi 7 E
BeitragVerfasst: Di 04.03.08 16:06 
Hm zuviel in Potenzen gedacht O(2)=n^2 etc. (Achtung das ist falsch, wie ich gerade gelernt habe). Aber jetzt steht es ja wenigstens richtig hier drin.