Autor Beitrag
n-regen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Mi 02.01.08 18:15 
Hallo!

Ich habe ein Programm geschrieben, dass Pi berechnen soll.
Dafür berechnet es eine Unmenge an Werten, zählt sie zusammen und verrechnet sie dann.
Jetzt ist das Problem: ab einer bestimmten Anzahl an Werten ist Pi auf einmal negativ - die Int64-Variable ist übergelaufen (ich habe das auch mit der Direktive {$OVERFLOWCHECKS ON} überprüft).
Was kann ich machen, um doch n brauchbare Ergebnisse heranzukommen.
Die Funktion sieht - leicht vereinfacht - so aus:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
  var dotx,doty:double;
  tropfenzahl,innerhalb,gesamt:int64;
{...}
  tropfenzahl := 10000000;  
  innerhalb := 0;
  gesamt := tropfenzahl;

  while (tropfenzahl > 0) do begin 
    dotx := 2 * random - 1;
    doty := 2 * random - 1;

    if (dotx*dotx + doty*doty <= 1) then
      // Punkt liegt innerhalb des Kreises
      inc(innerhalb);
    dec(tropfenzahl);
  end;
 listbox1.items.add(inttostr(innerhalb));

Dieser Quelltext ist die Delphi-Entsprechung (von mir umgeschrieben) zu diesem Java-Quelltext.

Die Ergebnisse werden in einer Textdatei gespeichert und von einem anderen Programm wieder geladen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 var innerhalb,gesamt:int64;
 i:integer;
{...}
 for i := 0 to listbox2.Items.Count-2 do begin
  innerhalb:=innerhalb+strtoint(listbox2.Items.Strings[i]);
 end;
 gesamt := 10000000 * (listbox2.Items.Count-2);
 edit.Text := floattostr(4*innerhalb/gesamt);


Ich hoffe, dass ihr aus diesem Quelltext-Durcheinander schlau werdet und eine Idee habt, wie ich an ein vernünftiges Ergebnis ohne Überlauf komme.

Danke schon im Voraus.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mi 02.01.08 18:23 
Versuch es mal mit UInt64

_________________
Markus Kinzler.
n-regen Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Mi 02.01.08 20:33 
{$OVERFLOWCHECKS ON} meldet auch da einen Integerüberlauf.
Popov
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1655
Erhaltene Danke: 13

WinXP Prof.
Bei Kleinigkeiten D3Pro, bei größeren Sachen D6Pro oder D7
BeitragVerfasst: Mi 02.01.08 20:45 
user profile iconn-regen hat folgendes geschrieben:
Int64 läuft über

Dann stell ein Eimer drunter. Mache ich auch immer bei Buffer-Overflow.

Sorry, konnte nicht widerstehen.

_________________
Popov
n-regen Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 202
Erhaltene Danke: 2



BeitragVerfasst: Mi 02.01.08 20:50 
Mein Eimer hat leider ein Loch!
Also hab ich das ganze gelöst, indem ich aus "innerhalb" einen Extended gemacht habe.
Dann habe ich hinter das "(listbox2.Items.Strings[i])" ein "/1000000" gesetzt und dem "gesamt := 10000000" sechs Nullen geklaut.
Jetzt funktioniert es ohne Überlauf!