![]() |
Eine ganzzahl herrausfinden?
Heyho,
ich möchte das eine funktion als ergbnis eine Ganzzahl hat und habe das bisher folgendermaßen umgesetzt:
Delphi-Quellcode:
Es soll die funktion kalkuliert werden und durch den gerundeten wert geteilt werden. wenn beide werte gleichgroß sind und man diese dividiert kommt 0 raus --> ganze zahl
var
Form1: TForm1; v:extended; v2:integer; i:real; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin i:=-100; v:=1; v2:=2; while v/v2<>1 do begin i:=i+0.0001; v:=(-8/5)*i+sqrt((39/25)*(i*i)-19); v2:=round(v); // label1.caption:=floattostr(v2); // label2.caption:=floattostr(v); end; label1.caption:=floattostr(v2); label2.caption:=floattostr(v); showmessage('YEAH'); end; jedoch bringt er nach dem Klick die Meldung 'EInvalidOp' - 'Ungültige Gleitkommastelle' was habe ich falsch gemacht?!? vielen dank für eure hilfe |
Re: Eine ganzzahl herrausfinden?
In welcher Zeile kommt der Fehler und bei welchen Werten?
|
Re: Eine ganzzahl herrausfinden?
v:=(-8/5)*i+sqrt((39/25)*(i*i)-19);
direkt wenn ich auf den button klicke |
Re: Eine ganzzahl herrausfinden?
Welchen Wert hat i, v, v2 dann?
|
Re: Eine ganzzahl herrausfinden?
interessant...
Delphi-Quellcode:
wenn ich ne showmessage reinmache läuft er ohne problme und gibt werte immer aus
begin
i:=i+0.0001; v:=(-8/5)*i+sqrt((39/25)*(i*i)-19); v2:=round(v); showmessage('v2 : '+floattostr(v2)); showmessage('v : '+floattostr(v)); //label1.caption:=floattostr(v2); //label2.caption:=floattostr(v); end; nach dem klick: v = 248,8235... v2 = 285 i = -99,999 |
Re: Eine ganzzahl herrausfinden?
Delphi-Quellcode:
label1.caption:=IntToStr(v2);
|
Re: Eine ganzzahl herrausfinden?
hab mehere varianten ausprobiert... er gibt die werte nur mit einem showmessage aus
|
Re: Eine ganzzahl herrausfinden?
Herzlich willkommen in der Delphi-PRAXiS, heru1990.
Du suchst iterativ in Schritten von 1E-4 nach einer ganzzahligen Lösung einer Funktion. Dein Abbruchkriterium ist aber unzuverlässig, da dein Rechner ganzzahlige Ergebnisse oft nach Art der ersten Taschenrechner nur näherungsweise als Fließkommazahl wiedergeben kann. Notwendig ist da ein Epsilon-Test:
Delphi-Quellcode:
Getippt und nicht getestet.
const
EPSILON = 1E-8; function F(x: Double): Double; begin Result := (-8/5) * x + Sqrt(39 / 25 * x * x - 19); end; procedure TDemoForm.ButtonClick(Sender: TObject); var x, y: Double; begin x := -100; repeat x := x + 0.0001; y := F(x); until IsZero(y - Round(y), EPSILON); ShowMessage(Format('%0.4f %.0n', [x, y])); end; Freundliche Grüße vom marabu |
Re: Eine ganzzahl herrausfinden?
Wow vielen dank =)
jedoch kommt die gleiche meldung mit der kommastelle in folgender zeile:
Delphi-Quellcode:
:?
Result := (-8/5) * x + Sqrt(39 / 25 * x * x - 19);
|
Re: Eine ganzzahl herrausfinden?
Deine Funktion ist nicht für alle x aus R definiert.
Sobald der Ausdruck 39 / 25 * x * x - 19 negativ wird, siehst du die Ausnahme "Ungültige Gleitkommaoperation". Du solltest das zu durchsuchende Intervall geeignet begrenzen. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:31 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz