Autor Beitrag
volvox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 11:24 
Hi, ich wollte mir diese Funktion schreiben, weils etwas kürzer ist als immer StrToFloat(IntToStr(x)) aber irgendwie klappt es nicht. Sind die Variablentypen richtig, oder ist da was vertauscht?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
function IntToFloat(x: Integer): real;
begin
  StrToFloat(IntToStr(x));
end;
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 27.01.08 11:26 
:shock: Ein Integer passt komplett in einen Real, das solltest Du einfach zuweisen können :gruebel:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 11:29 
Stimmt das dann also so wie es da steht? (Dann hätte ich nur einen Fehler irgendwo anderes im Programm)
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 27.01.08 11:30 
Wie user profile iconChristian S. schon sagte: Diese Funktion ist vollkommen unnötig.

Wenn du sie aber trotzdem verwenden willst, solltest du den Wert auch irgendwo hinspeichern; zum Beispiel in Result. :wink:
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: So 27.01.08 11:30 
Im Zweifel einfach so :mrgreen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function IntToReal(i: Integer): Real;
begin
  result := i;
end;

_________________
We are, we were and will not be.
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 11:35 
Warum soll die Funktion unnötig sein. Es ist doch kürzer. Oder gibt es in Delphi schon so eine funktion?
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 27.01.08 11:37 
user profile iconChristian S. hat folgendes geschrieben:
Ein Integer passt komplett in einen Real, das solltest Du einfach zuweisen können

Tust du lesen, tust du wissen :les: :arrow: :think:


Zuletzt bearbeitet von Yogu am So 27.01.08 11:52, insgesamt 1-mal bearbeitet
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 11:43 
?? Erklärs mal für Dumme^^ Ich kenn mich mit Delphi noch nicht sonderlich gut aus...
Ich habe allerdings das Problem, dass ich Realzahlen (zwischen 0 und 1), die ich graphisch darstellen will (y-Koordinate). Allerdings gibt es ja probleme wenn ich z.B. LineTo verwende, weil man da ja immer ganze Zahlen für die Pixel braucht. Deswegen, würe ich das auch gerne umwandeln. Nur Trunc ist da halt sinnlos, weil ich dann ja immer nur 0 rausbekomme.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 27.01.08 11:46 
Dann hast du es die ganze Zeit falsch rum probiert. Du willst doch Kommazahlen in ganze Zahlen umwandeln.

Das geht am einfachsten mit Round. Wenn dir das zu ungenau ist, musst du den Wert erst mit einem Faktor muliplizieren, bevor du ihn rundest.
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 11:47 
jo, klar. thx
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 12:06 
Ich bekomm, aber ne Fehlermeldung "ungültiger Gleitkommawert". Da stimmt doch dann was mit der Umwaldlung in Integer nicht, oder? (Die Werte in dem Array sind jetzt mal willkürlich gewählt.)


ausblenden 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:
procedure ZeichneGraph;
var
  Wert: array[0..9999] of real;
  i:integer;
  x:integer;
begin
  x:=25;
  
    Wert[0] := 47.1;
    Wert[1] := 5.0;
    Wert[2] := 77.4;
    Wert[3] := 160.0;
    Wert[4] := 111.64;
    Wert[5] := 67.0;
    Wert[6] := 1.0;

  frm1.imgGraph.canvas.MoveTo(x,110);   

  for i:= 0 to 9999 do begin
    frm1.imgGraph.canvas.LineTo(x,Round(Wert[i])); 
    x:=x+10;
    end;
end;
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 27.01.08 12:11 
Wenn der Quelltext ähnlich dem richtigen ist, dann könnte das Problem sein, dass du nur 7 von den 10000 Werten etwas zuweist. Ich denke, die haben dann einen undefinierten Wert (NaN, der sich nicht in ein Integer umwandeln lässt.
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 12:17 
Hmm, was schlägst du vor? Mit ner 2. for-Schleife erst allen einen Wert (z.B. 0) zuweisen?
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: So 27.01.08 12:21 
user profile iconvolvox hat folgendes geschrieben:
Mit ner 2. for-Schleife erst allen einen Wert (z.B. 0) zuweisen?

Das könnte klappen. Aber wozu hast du ein Array mit Platz für 10000 Werte, wenn du nur 7 brauchst? Du verwendest doch Delphi 7, da kannst du im Zweifelsfall dynamische Arrays verwenden.
volvox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 112

WIN XP
Delphi 7 Personal
BeitragVerfasst: So 27.01.08 12:26 
Dynamische Array sagen mir gar nichts. Müsste ich mir mal angucken. Aber das Programm muss auf jeden Fall auch unter Delphi 6 laufen können.

Das Array ist so groß, weil das Programm eine stochastische Simulation werden soll, bei der sichZufallswerte um einen bestimmten Wert stabilisieren
icho2099
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 101
Erhaltene Danke: 12

WIN XP, WIN 7, WIN 10
Delphi 6 Prof, Delphi 2005, FPC
BeitragVerfasst: So 27.01.08 12:40 
einen Datenblock mit konstanten Bytewerten füllen:

fillchar(Var x; Count : Integer; Value byte);

FillChar(Wert,Sizeof(Werte),0);

Skalieren auf die y-Achse:

yValue := Round(Wert[i] * ScaleY);