Wert ändert sich nach mathematischer operation nicht
-
Hallo ihr Lieben,
ich habe ein kleines progrämmchen geschrieben, dass mir einen von cosinus abhängigen Wert liefern soll. Der erste Durchlauf scheint zu funktionieren, da das erwartete Ergebnis 1560 dabei herauskommt. Alle weiteren Durchläufe funktionieren wohl nicht, da immer noch 1560 herauskommt. Wo könnte das Problem liegen?
#define INTERVALL 1 // Delay Intervall
#define MAXNUMBER 499 // Maximum number of values
#define STARTPOSITION 1560 // Enter the start position in µs
#define ENDPOSITION 1500 // Enter the end position in µs
#define OFFSET 1530 // (STARTPOSITION+ENDPOSITION)/2
#define AMPLITUDE 30 // (STARTPOSITION-ENDPOSITION)/2
#define FABTAST 1/600 // Intervall time of Alphaguard
#define FMAX 1/(2*600) // FABTAST / 2
#define POWER 0.99957 // FREQ^Power
#define DIV 400 // Frequency divider#include <math.h> // Needed here for cosine function
#include <stdio.h>
#include <stdlib.h>int main(int argc, char *argv[])
{char j;
int i, n, a, Reg;
double Time;
float Frequency, Value;// Initialize variables
Frequency = FMAX / DIV;
a = 0;
Time = 0;
i = 0;// Main while loop
while(1)
{// Set new value until MAXNUMBER is not reached
if (i <= MAXNUMBER)
{
Value = OFFSET + AMPLITUDE * cos(2 * M_PI * Frequency * Time);
Value +=0.5;
a = (int)Value;
printf("%d\n",a);
Frequency = pow(Frequency,POWER);
Time += 600;
i++;
} // i != MAXNUMBER// Set last value after MAXNUMBER is reached
else if (i == (MAXNUMBER + 1))
{
Reg = ENDPOSITION-50;
} // i == MAXNUMBER
}system("PAUSE");
return 0;
}
-
Hoffe, der Code ist nun etwas übersichtlicher geworden. Ich freue mich über jeden Tipp! - Vielen Dank im voraus!
#define MAXNUMBER 499 // Maximum number of values
#define POWER 0.99957 // FREQ^Power#include <math.h> // Needed here for cosine function
#include <stdio.h>
#include <stdlib.h>int main(int argc, char *argv[])
{
char j;
int i, n;
double Time;
float Frequency, Value;// Initialize variables
Frequency = 1/(600*2*400);
Time = 0;
i = 0;// Set new value until MAXNUMBER is not reached
while(i <= MAXNUMBER)
{
Value = 1530 + 30 * cos(2 * M_PI * Frequency * Time);
printf("%lf\n",Value);
Frequency = pow(Frequency,POWER);
Time += 600;
i++;
} // While i <= MAXNUMBER// Set last value after MAXNUMBER is reached
Value = 1450;system("PAUSE");
return 0;
}
-
Mark00 schrieb:
// Initialize variables Frequency = 1/(600*2*400);
Mach mal das:
Frequency = (float) 1 / (600 * 2 * 400);
-
Ah, vielen Dank für den Hinweis. Ich werde es sofort ausprobieren.
-
Oder besser:
Frequency = 1.0f / (600.0f * 2.0f * 400.0f);
Das Gleiche gilt für alle anderen float-Literale.
-
Super Hinweise! Vielen vielen Dank. Ihr habt mir den Freitag gerettet! Jetzt läuft alles prima!