Autor Beitrag
ManuelGS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: So 07.11.04 22:01 
Hi!
Erstmal ein bissl Code:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Button1Click(Sender: TObject);
var zahl:extended;
begin
zahl:=strtofloat(edit1.text); //243,242341
zahl:=frac(zahl); //0,242341
while (frac(zahl)<>0) do zahl:=zahl*10;
showmessage(floattostr(zahl)); //2,4E19 oder sowas (???)
end;


Hier Die Frage für die Profis:
Irgendwann hat zahl ja den Inhalt von 242341. Das ist das Ergebnis, das ich will.
Dann aber macht das Programm etwas seltsames. Es bastelt sich irgendwelche Nachkommastellen 1E-15 und solche Spässe.
Wo ist das Problem, wie kann ich es beheben, oder wie geht es anders?
Ich vermute, dass strtofloat irgendwelches blödes Zeugs übergibt.

Ich hoffe auf viele Antworten.
Gruß, Manuel

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
.Chef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1112



BeitragVerfasst: So 07.11.04 22:07 
Ich würde eher sagen, dass es mit Signatur 5. zusammenhängt. Zahl enthält nicht genau die 6 Nachkommastellen, die du eingibst, sondern noch eine Darstellungsungenauigkeit, entstanden durch das Abbilden von Gleitkommazahlen im binären System.

Gruß,
Jörg

_________________
Die Antworten auf die 5 häufigsten Fragen:
1. Copy(), Pos(), Length() --- 2. DoubleBuffered:=True; --- 3. Application.ProcessMessages bzw. TThread --- 4. ShellExecute() --- 5. Keine Vergleiche von Real-Typen mit "="!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 07.11.04 22:41 
Tipp:
While(Frac(Zahl)>1E-10) do Zahl := Zahl*10;
Dann sollte es eigentlich gehen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
ManuelGS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 173

Win XP HE, Suse Linux
D6, D7, D2005 Personal
BeitragVerfasst: Mo 08.11.04 00:13 
Hi Ben!
Aber das würde doch voraussetzen, dass meine Zahl nur eine festgelegt Anzahl von Nachkommastellen haben darf.
Gibt's da nicht vielleicht doch was besseres?

Gruß, Manuel
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 08.11.04 09:30 
Wie Chef das schon so schön erklärt hat: Nein, nicht wirklich. Du kannst aber je nach Datentyp unterschiedlich viele Nachkommastellen nutzen. Bei Extended müsste 1E-10 theoretisch gute Ergebnisse liefern. Ansonsten musst du ausprobieren. Aber um den Rundungsfehler von Extended gibt's keinen Ausweg. Außer du schreibst all die Gleitkomma-Operationen vollkommen per Hand (Viel Spaß).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.