Autor Beitrag
Xearox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 10:44 
Ich möchte gerne eine Einfache Mathe Formel in Delphi implemintieren.(hoffe das wort ist richtig...
Jedoch finde ich nicht die passenden Befehle dafür, die Formel sieht so aus:

Kosten=['Abrunden' Basiskosten*['Abrunden'(Stufe+1)*1,5^Stufe]]

Dabei ist es wichtig, das es immer nur abgerunden wird, also bei 1,9 auf 1,0



Kann jemand mir die Formel in Delphi so umschreiben, damit ich die Übernehmen kann?

bisher habe ich nur das hier:

ausblenden Delphi-Quelltext
1:
Kosten:=Round((Metall*Round((Stufe+1)*1,5^Stufe; //das geht jedoch nicht...mir fehlen die Operatoren dafür					



Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Mi 13.01.2010 um 12:01
Mike19
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 256

Win XP, Vista, Win 7
Delphi 2005, Turbo Delphi
BeitragVerfasst: Mi 13.01.10 10:53 
ein wenig mehr Code wäre bestimmt Hilfreich, zeige doch mal die gesamte Unit.
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 11:01 
Bin noch am Anfang des Programms, aber diese Formel ist der Kernpunkt des Rechners...


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:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    RadioButton6: TRadioButton;
    RadioButton7: TRadioButton;
    RadioButton8: TRadioButton;
    RadioButton9: TRadioButton;
    RadioButton10: TRadioButton;
    RadioButton11: TRadioButton;
    RadioButton12: TRadioButton;
    RadioButton13: TRadioButton;
    RadioButton14: TRadioButton;
    RadioButton15: TRadioButton;
    RadioButton16: TRadioButton;
    RadioButton17: TRadioButton;
    RadioButton18: TRadioButton;
    RadioButton19: TRadioButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    Memo3: TMemo;
    Label7: TLabel;
    MStufe: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure StufeAnzeigen;
    procedure Metallmine;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

const
  Stufe40=40;


implementation

{$R *.dfm}

procedure TForm1.Metallmine;
const
  Metall=125;
  Kristall=12;
  Zivis=50;

var
  MetKosten,KrisKosten,ZiviKosten:integer;
  basiskosten:integer;
  Stufe:integer;

begin
  Stufe:=1;
  //----------------------Metall Kosten-----------------------------------------
  MetKosten:=Round((Metall*Round((Stufe+1)*1,5^Stufe
end;

procedure TForm1.StufeAnzeigen;
var
  I:integer;
  Anzahl:integer;
  AnzahlStr:string;

begin
  Anzahl:=0;
  for I := 0 to Stufe40 - 1 do
  begin
    Anzahl:=Anzahl+1;
    AnzahlStr:=inttostr(Anzahl);
    MStufe.Text:=MStufe.Text+AnzahlStr+#13#10;

  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  StufeAnzeigen;
end;

end.


Zuletzt bearbeitet von Xearox am Mi 13.01.10 11:49, insgesamt 1-mal bearbeitet
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 480

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 13.01.10 11:15 
Abrunden geht mit Trunc(), Potenzieren mit Power() aus der Unit Math.

D.h. Trunc schneidet die Nachkommastellen ab. Wenn man nur mit positiven Werten arbeitet, passt das so.

_________________
We are, we were and will not be.
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 11:19 
ausblenden Delphi-Quelltext
1:
MetKosten:=Trunc((Metall*Trunc((Stufe+1)*Power(1.5,Stufe))))					


die geht irgendwie nicht...komme da nicht hinter...


so, und nun hab ich den hier...

ausblenden Delphi-Quelltext
1:
MetKosten:=Trunc(Metall*(Stufe+1)*Power(1.5,Stufe))					


Ist das mit Power richtig? will ja die 1,5 und die Stufe mit einander Potenzieren
Nersgatt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1581
Erhaltene Danke: 279


Delphi 10 Seattle Prof.
BeitragVerfasst: Mi 13.01.10 11:37 
Die Fehlermeldung wäre durchaus interessant zu erfahren.

Ansonsten: Versuch mal die Formel in die einzelnen Rechenschritte zu zerlegen. So versteht man es meistens besser.

_________________
Gruß, Jens
Zuerst ignorieren sie dich, dann lachen sie über dich, dann bekämpfen sie dich und dann gewinnst du. (Mahatma Gandhi)
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 11:48 
Also in der Theorie, wie ich das mit Excel ausgerechnet habe müsste nach dem ersten durchlauf die MetKosten bei 375 Liegen.
Ist aber nicht der fall, er liegt bei 2,9618630521e-307...
irgendwie ist da was falsch...


Okay...hab den Fehler gefunden, ich rechne ja nur mit Ganzenzahlen...Hab real Variablen gehabt...
Es kommt aber immer noch ein falschen ergebnis raus...
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 480

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 13.01.10 11:58 
Dann führst du anderen Code aus als den, den du uns hier präsentiert hast. Dein Ergebnis ist übrigens (bis auf Rundungsfehler) Null. ;-)

_________________
We are, we were and will not be.
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 12:10 
also, mir ist zu nächst einmal auf gefallen, das der Normale Integer nicht reicht, also der Platz, da ich zahlen bis 22.114.664.625 habe...

das heißt, ich müsste real nehmen, richtig?

das ist mein Momentaner Code:

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:
procedure TForm1.Metallmine;
const
  Metall=125;
  Kristall=12;
  Zivis=50;

var
  MetKosten,KrisKosten,ZiviKosten:longint;
  basiskosten:integer;
  Stufe:integer;
  I:integer;
  MetKostenStr:String;
  KrisKostenStr:String;
  ZiviKostenStr:string;

begin
  Stufe:=1;
  MetKosten:=Metall;
  KrisKosten:=Kristall;
  ZiviKosten:=Zivis;
  MetKostenStr:=inttostr(metkosten);
  KrisKostenStr:=inttostr(Kriskosten);
  ZiviKostenStr:=inttostr(ZiviKosten);
  MMetKosten.Text:=MMetKosten.Text+MetKostenStr+#13#10;
  MKrisKosten.Text:=MKrisKosten.Text+KrisKostenStr+#13#10;
  MZiviKosten.Text:=MZiviKosten.Text+ZiviKostenStr+#13#10;
//------------------------Metall Kosten-----------------------------------------
  for I:=0 to 40 + 1 do
  begin
    Stufe:=Stufe+1;
    MetKosten:=Trunc(Metall*(Stufe+1)*Power(1.5,Stufe));
    MetKostenStr:=inttostr(metkosten);
    MMetKosten.Text:=MMetKosten.Text+MetKostenStr+#13#10;
  end;
//------------------------Kristall Kosten---------------------------------------
  Stufe:=1;
  for I := 0 to 40 + 1 do
    begin
      Stufe:=Stufe+1;
      KrisKosten:=Trunc(Kristall*(Stufe+1)*Power(1.5,Stufe));
      KrisKostenStr:=inttostr(Kriskosten);
      MKrisKosten.Text:=MKrisKosten.Text+KrisKostenStr+#13#10;
    end;
//------------------------Zivi Kosten-------------------------------------------
  Stufe:=1;
  for I := 0 to 40 + 1 do
    begin
      Stufe:=Stufe+1;
      ZiviKosten:=Trunc(Zivis*(Stufe+1)*Power(1.5,Stufe));
      ZiviKostenStr:=inttostr(ZiviKosten);
      MZiviKosten.Text:=MZiviKosten.Text+ZiviKostenStr+#13#10;
    end;
end;


Edit: Hab den Code Bisschen überarbeitet...
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mi 13.01.10 12:44 
wenn du ganzzahlentypen brauchst und integer nicht ausreicht, warum nimmst du dann nicht Int64? das reicht von -9223372036854775808 bis 9223372036854775807.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Mi 13.01.10 12:52 
Wenn man das mal zurückrechnet, stellt man Fest: 843.25 ist Stufe 2.

Was auch klar ist: du initialisiert Stufe auf 1, und addierst am Anfang der Schleife 1. Der erste Wert mit dem gerechnet wird ist also 1+1=2. Stimmt doch alles 8)

Du machst dir das aber auch sinnlos kompliziert: immerhin hast du schon die Durchlaufvariable der for-Schleife, also kannst du auch direkt mit dieser rechnen. Bzw. die Variable mit der du rechnen willst als Schleifenvariable nehmen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
  for Stufe:=1 to 40 do
  begin
    MetKosten:=Trunc(Metall*(Stufe+1)*Power(1.5,Stufe));
    MetKostenStr:=inttostr(metkosten);
    MMetKosten.Text:=MMetKosten.Text+MetKostenStr+#13#10;
  end;

Sieht doch gleich viel schöner aus ;)

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 12:56 
dann bekomme ich: Üngültige Gleitkommaoperation


das Problem ist einfach:
in Excel habe ich folgendes ergebnis, nach der Formel, die ich ganz oben stehen habe:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
125,00
375,00
750,00
1.625,00
3.125,00
5.625,00
9.875,00
17.000,00
28.750,00
48.000,00


bei Delphi hab ich ab Stufe 10

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
125
843
1687
3164
5695
9966
17085
28832
48054
79289

merkt ihr den Unterschied?
Ich weiß einfach nicht, wo der Fehler im Code ist....

Edit: Nun mit Double Variablen


Zuletzt bearbeitet von Xearox am Mi 13.01.10 13:26, insgesamt 1-mal bearbeitet
Gammatester
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 328
Erhaltene Danke: 101



BeitragVerfasst: Mi 13.01.10 13:22 
Du scheinst immer noch integer zu rechnen:

ausblenden Delphi-Quelltext
1:
2:
MetKosten:=Trunc(Metall*(Stufe+1)*Power(1.5,Stufe));
 MetKostenStr:=inttostr(metkosten);


Mach' MetKosten double und benutze MetKostenStr:=floattostr(...)
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 13:25 
so, das mit dem Double war eine gute idee, ist wohl mein Delphi 3 buch nicht mehr aktuell... *lach mich weg*

hmm, dennoch will ich verstehen können, wieso 843 raus kommt, und keine 375

Edit: Ahh, kommen wir die sache schon mal näher...
Jetzt ist Stufe 3 und aufwärts Falsch

Stufe 3 muss 750 sein, da kommt aber nun 843 raus
Gammatester
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 328
Erhaltene Danke: 101



BeitragVerfasst: Mi 13.01.10 13:37 
Da paßt einiges nicht zusammen: wenn bei Metall*(stufe+1)*1.5^stufe für stufe=1 das Ergebnis 125 sein soll, muß Metall = 41 2/3 sein (Metall = 125/3). Dann ergibt sich für stufe=2 der Wert = (41 2/3) * 3 * 1.5^2 = 125 * 2.25 = 281.25. Das gibt's aber weder bei Excel noch bei Delphi.

Was also ist der Wert von Metall?
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 13:43 
Ich habs hinbekommen...
die lösung nach langem ausprobieren...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
  for Stufe:=1 to 39 do
  begin
    MetKosten:=Trunc(Metall)*Trunc((Stufe+1)*Power(1.5,Stufe));
    MetKostenStr:=floattostr(metkosten);
    MMetKosten.Text:=MMetKosten.Text+MetKostenStr+#13#10;
  end;


ich musste, wie oben in der Formel auch, einfach 2mal Trunc einfügen, das hatte ich aber auch am anfang, nur da hatte ich vergessen, die klamma zu, zu machen...
Also: Trunc(Metall)*Trunc(....)
Und nicht: Trunc (Metall*Trunc(....))
so hatte ich das weiter oben gehabt...



user profile iconGammatester hat folgendes geschrieben Zum zitierten Posting springen:
Da paßt einiges nicht zusammen: wenn bei Metall*(stufe+1)*1.5^stufe für stufe=1 das Ergebnis 125 sein soll, muß Metall = 41 2/3 sein (Metall = 125/3). Dann ergibt sich für stufe=2 der Wert = (41 2/3) * 3 * 1.5^2 = 125 * 2.25 = 281.25. Das gibt's aber weder bei Excel noch bei Delphi.

Was also ist der Wert von Metall?


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:
procedure TForm1.Metallmine;
const
  Metall=125;
  Kristall=12;
  Zivis=50;

var
  MetKosten,KrisKosten,ZiviKosten:double;
  kosten:integer;
  Stufe:integer;
  I:integer;
  Metall2:integer;
  MetKostenStr:String;
  KrisKostenStr:String;
  ZiviKostenStr:string;

begin
  Stufe:=1;
  Metall2:=Metall; //einmalig mit der Constante Metall belegen
  MetKosten:=Metall;
  KrisKosten:=Kristall;
  ZiviKosten:=Zivis;
  MetKostenStr:=floattostr(metkosten);
  KrisKostenStr:=floattostr(Kriskosten);
  ZiviKostenStr:=floattostr(ZiviKosten);
  MMetKosten.Text:=MMetKosten.Text+MetKostenStr+#13#10;  //hier wird die Stufe 1 gesetzt, und in der Schleife wird Stufe 2 bearbeitet
  MKrisKosten.Text:=MKrisKosten.Text+KrisKostenStr+#13#10;
  MZiviKosten.Text:=MZiviKosten.Text+ZiviKostenStr+#13#10;
//------------------------Metall Kosten-----------------------------------------
  for Stufe:=1 to 39 do
  begin
    MetKosten:=Trunc(Metall)*Trunc((Stufe+1)*Power(1.5,Stufe));
    MetKostenStr:=floattostr(metkosten);
    MMetKosten.Text:=MMetKosten.Text+MetKostenStr+#13#10;
  end;
//------------------------Kristall Kosten---------------------------------------
  Stufe:=1;
  for Stufe:=1 to 39 do
    begin
      KrisKosten:=Trunc(Kristall)*Trunc((Stufe+1)*Power(1.5,Stufe));
      KrisKostenStr:=floattostr(Kriskosten);
      MKrisKosten.Text:=MKrisKosten.Text+KrisKostenStr+#13#10;
    end;
//------------------------Zivi Kosten-------------------------------------------
  Stufe:=1;
  for Stufe:=1 to 39 do
    begin
      ZiviKosten:=Trunc(Zivis)*Trunc((Stufe+1)*Power(1.5,Stufe));
      ZiviKostenStr:=floattostr(ZiviKosten);
      MZiviKosten.Text:=MZiviKosten.Text+ZiviKostenStr+#13#10;
    end;
end;
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mi 13.01.10 15:20 
[OT]
Rein aus Interesse: Um welches (Browser-)Spiel geht es? Hab früher auch verschiedene gespielt, und würd mich mal interessieren, ob ich das kenne :)
[/OT]

_________________
PROGRAMMER: A device for converting coffee into software.
Xearox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 260
Erhaltene Danke: 3



BeitragVerfasst: Mi 13.01.10 17:12 
Evil-Empire.biz

hoffe das ist keine Werbung...