Updated to remove fmod and string length warnings

This commit is contained in:
2019-03-11 13:07:03 -04:00
parent 0006517e74
commit 628cf67754
2 changed files with 20 additions and 20 deletions

View File

@@ -112,45 +112,45 @@ double getHourStopwatch(struct Stopwatch* timer){
//Gets the time from a stopwatch at the "Best" resolution. The goal is XXX.XXX [Resolution] //Gets the time from a stopwatch at the "Best" resolution. The goal is XXX.XXX [Resolution]
//While it would be possible to use this function to get the time while still running it is not recomended because of the time it takes to return the string //While it would be possible to use this function to get the time while still running it is not recomended because of the time it takes to return the string
char* getStrStopwatch(struct Stopwatch* timer){ char* getStrStopwatch(struct Stopwatch* timer){
char* num = (char*)malloc(21 * sizeof(char)); //Holds the string that will be created in the end char* num = (char*)malloc(35 * sizeof(char)); //Holds the string that will be created in the end
double dur; //Holds the duration the stopwatch has run double dur; //Holds the duration the stopwatch has run
//Decide what the best way to get time is. Looking for a XXX.XXX format //Decide what the best way to get time is. Looking for a XXX.XXX format
if(getNanoStopwatch(timer) < 1000){ if(getNanoStopwatch(timer) < 1000){
dur = getNanoStopwatch(timer); dur = getNanoStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration int intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d nanoseconds", intDur, fractDur); snprintf(num, 35, "%d.%03d nanoseconds", intDur, fractDur);
} }
else if(getMicroStopwatch(timer) < 1000){ else if(getMicroStopwatch(timer) < 1000){
dur = getMicroStopwatch(timer); dur = getMicroStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration int intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d microseconds", intDur, fractDur); snprintf(num, 35, "%d.%03d microseconds", intDur, fractDur);
} }
else if(getMilliStopwatch(timer) < 1000){ else if(getMilliStopwatch(timer) < 1000){
dur = getMilliStopwatch(timer); dur = getMilliStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration int intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d milliseconds", intDur, fractDur); snprintf(num, 35, "%d.%03d milliseconds", intDur, fractDur);
} }
else if(getSecondStopwatch(timer) < 1000){ else if(getSecondStopwatch(timer) < 1000){
dur = getSecondStopwatch(timer); dur = getSecondStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration int intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d seconds", intDur, fractDur); snprintf(num, 35, "%d.%03d seconds", intDur, fractDur);
} }
else if(getMinuteStopwatch(timer) < 1000){ else if(getMinuteStopwatch(timer) < 1000){
dur = getMinuteStopwatch(timer); dur = getMinuteStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration int intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d minutes", intDur, fractDur); snprintf(num, 35, "%d.%03d minutes", intDur, fractDur);
} }
else{ else{
dur = getHourStopwatch(timer); dur = getHourStopwatch(timer);
uint64_t intDur = dur; //Holds the whole number integer representation of the duration int intDur = dur; //Holds the whole number integer representation of the duration
uint64_t fractDur = (fmod(dur, 1.0) * 1000); //Holds the integer representation of the fractional part of the duration int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration
sprintf(num, "%d.%03d hours", intDur, fractDur); snprintf(num, 21, "%d.%03d hours", intDur, fractDur);
} }
return num; return num;

View File

@@ -47,7 +47,7 @@ int main(){
char* timerStr = getStrStopwatch(&timer); char* timerStr = getStrStopwatch(&timer);
//Print the results //Print the results
printf("It took %u nanoseconds to run this program\n", getNanoStopwatch(&timer)); printf("It took %lu nanoseconds to run this program\n", getNanoStopwatch(&timer));
printf("It took %f microseconds to run this program\n", getMicroStopwatch(&timer)); printf("It took %f microseconds to run this program\n", getMicroStopwatch(&timer));
printf("It took %f milliseconds to run this program\n", getMilliStopwatch(&timer)); printf("It took %f milliseconds to run this program\n", getMilliStopwatch(&timer));
printf("It took %f seconds to run this program\n", getSecondStopwatch(&timer)); printf("It took %f seconds to run this program\n", getSecondStopwatch(&timer));