Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Invalid floating Point operation (https://www.delphipraxis.net/160545-invalid-floating-point-operation.html)

Matz4000 18. Mai 2011 17:03

Delphi-Version: 5

Invalid floating Point operation
 
Hey Leute..
Kleines Problem:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var Wuerfel,Wurfzahl,Augenzahl,W1,W2,W3,W4,W5,W6,W7,W8,i:integer;
    Warsch:real;
    Feld:array[1..48] of integer;
begin
listbox1.clear;
Wuerfel:=strtoint(edit1.text);
Wurfzahl:=strtoint(edit2.Text);

For i:=1 to 60 DO
 Feld[i]:=0;
Warsch:=0;
 Case Wuerfel OF
  1:Begin
     label1.caption:=inttostr(Wurfzahl);
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        inc(Feld[W1]);
      end;
     For i:=1 to 6 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;
  2:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        Augenzahl:=W1+W2;
        inc(Feld[Augenzahl]);
      end;
     For i:=2 to 12 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;

  3:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        W3:=random(6)+1;
        Augenzahl:=W1+W2+W3;
        inc(Feld[Augenzahl]);
      end;
     For i:=3 to 18 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;
  4:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        W3:=random(6)+1;
        W4:=random(6)+1;
        Augenzahl:=W1+W2+W3+W4;
        inc(Feld[Augenzahl]);
      end;
     For i:=4 to 24 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;
  5:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        W3:=random(6)+1;
        W4:=random(6)+1;
        W5:=random(6)+1;
        Augenzahl:=W1+W2+W3+W4+W5;
        inc(Feld[Augenzahl]);
      end;
     For i:=5 to 30 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;
  6:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        W3:=random(6)+1;
        W4:=random(6)+1;
        W5:=random(6)+1;
        W6:=random(6)+1;
        Augenzahl:=W1+W2+W3+W4+W5+W6;
        inc(Feld[Augenzahl]);
      end;
     For i:=6 to 36 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;
  7:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        W3:=random(6)+1;
        W4:=random(6)+1;
        W5:=random(6)+1;
        W6:=random(6)+1;
        W7:=random(6)+1;
        Augenzahl:=W1+W2+W3+W4+W5+W6+W7;
        inc(Feld[Augenzahl]);
      end;
     For i:=7 to 42 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;
  8:Begin
     For i:= 1 to Wurfzahl Do
      Begin
        W1:=random(6)+1;
        W2:=random(6)+1;
        W3:=random(6)+1;
        W4:=random(6)+1;
        W5:=random(6)+1;
        W6:=random(6)+1;
        W7:=random(6)+1;
        W8:=random(6)+1;
        Augenzahl:=W1+W2+W3+W4+W5+W6+W7+W8;
        inc(Feld[Augenzahl]);
      end;
     For i:=8 to 48 Do
      Begin
        Warsch:=Feld[i]/Wurfzahl;
        listbox1.items.add('Zahl '+inttostr(i)+' = '+floattostr(Warsch));
      end;
    End;

 end;




end;
end.

Das soll son Prog. sein, das die Warscheinlichkeit der gewürfelten Zahlen bei 1-8 Würfeln und x Würfen ausgibt.
Als Fehlermeldung kommt allerdings immer "Invalid floating Point operation"
Hab mich mal informiert, aber die Gründe die ich gelesen haben treffen hier nicht zu. (z.B. durch 0 teilen oder so)...

Help!

Gruß
Michael

DeddyH 18. Mai 2011 17:07

AW: Invalid floating Point operation
 
Und wo kommt die Meldung (Zeile)? Du solltest aber unabhängig davon dringendst einmal Deinen Code aufräumen (DRY) und die Bereichsprüfung einschalten.

shmia 18. Mai 2011 17:30

AW: Invalid floating Point operation
 
Ein kleiner Tipp zu folgendem Code:
Delphi-Quellcode:
W1:=random(6)+1;
W2:=random(6)+1;
W3:=random(6)+1;
W4:=random(6)+1;
W5:=random(6)+1;
W6:=random(6)+1;
W7:=random(6)+1;
W8:=random(6)+1;
Augenzahl:=W1+W2+W3+W4+W5+W6+W7+W8;
Die augenzahl lässt sich viel besser mit einer Funktion ausrechnen:
Delphi-Quellcode:
function BerechneSummeAugenzahl(AnzahlWuerfel:integer):integer;
begin
  result := 0;
  for i := 1 to AnzahlWuerfel do
    Result := Result + random(6)+1;
end;
Und damit kannst du die vielen Variablen W1 bis W8 rauswerfen und schreibst z.B. nur:
Delphi-Quellcode:
Augenzahl := BerechneSummeAugenzahl(8);

Matz4000 18. Mai 2011 17:47

AW: Invalid floating Point operation
 
Also die Fehlermeldung kommt in der Zeile "Warsch:=Feld[i]/Wurfzahl;"
d.h. in der jeweiligen case-anweisung bei der anzahl der würfel..

Danke für den Tipp ;)
Hab halt erst seit einem halben Jahr informatik..wir haben noch keine Functions...

Gruß

Matz4000 18. Mai 2011 19:09

AW: Invalid floating Point operation
 
Hat denn keiner ne Idee :(
Ich verzweifle hier gerade...

mkinzler 18. Mai 2011 19:12

AW: Invalid floating Point operation
 
Starte es mal im Debugger, dann siehst du in welcher Zeile der Fehler auftritt.

Hawkeye219 18. Mai 2011 20:16

AW: Invalid floating Point operation
 
Hallo,
Zitat:

Zitat von DeddyH (Beitrag 1101600)
und die Bereichsprüfung einschalten.


Wenn man den Quelltext genauer betrachtet, ist das ein guter Ratschlag!
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var Feld:array[1..48] of integer;
begin
  :
For i:=1 to 60 DO
 Feld[i]:=0;
Gruß Hawkeye

DeddyH 18. Mai 2011 20:18

AW: Invalid floating Point operation
 
Eben wegen dieser Stelle habe ich ja den Vorschlag gemacht. Wer weiß, wo das noch überall so aussieht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:35 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz