diff --git a/Stopwatch.h b/Stopwatch.h index 66fd329..adbb859 100644 --- a/Stopwatch.h +++ b/Stopwatch.h @@ -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] //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* 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 //Decide what the best way to get time is. Looking for a XXX.XXX format if(getNanoStopwatch(timer) < 1000){ dur = getNanoStopwatch(timer); - uint64_t 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 - sprintf(num, "%d.%03d nanoseconds", intDur, fractDur); + int intDur = dur; //Holds the whole number integer representation of the duration + int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration + snprintf(num, 35, "%d.%03d nanoseconds", intDur, fractDur); } else if(getMicroStopwatch(timer) < 1000){ dur = getMicroStopwatch(timer); - uint64_t 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 - sprintf(num, "%d.%03d microseconds", intDur, fractDur); + int intDur = dur; //Holds the whole number integer representation of the duration + int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration + snprintf(num, 35, "%d.%03d microseconds", intDur, fractDur); } else if(getMilliStopwatch(timer) < 1000){ dur = getMilliStopwatch(timer); - uint64_t 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 - sprintf(num, "%d.%03d milliseconds", intDur, fractDur); + int intDur = dur; //Holds the whole number integer representation of the duration + int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration + snprintf(num, 35, "%d.%03d milliseconds", intDur, fractDur); } else if(getSecondStopwatch(timer) < 1000){ dur = getSecondStopwatch(timer); - uint64_t 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 - sprintf(num, "%d.%03d seconds", intDur, fractDur); + int intDur = dur; //Holds the whole number integer representation of the duration + int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration + snprintf(num, 35, "%d.%03d seconds", intDur, fractDur); } else if(getMinuteStopwatch(timer) < 1000){ dur = getMinuteStopwatch(timer); - uint64_t 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 - sprintf(num, "%d.%03d minutes", intDur, fractDur); + int intDur = dur; //Holds the whole number integer representation of the duration + int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration + snprintf(num, 35, "%d.%03d minutes", intDur, fractDur); } else{ dur = getHourStopwatch(timer); - uint64_t 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 - sprintf(num, "%d.%03d hours", intDur, fractDur); + int intDur = dur; //Holds the whole number integer representation of the duration + int fractDur = ((dur - intDur) * 1000); //Holds the integer representation of the fractional part of the duration + snprintf(num, 21, "%d.%03d hours", intDur, fractDur); } return num; diff --git a/testStopwatch.c b/testStopwatch.c index d77bb0d..c3250af 100644 --- a/testStopwatch.c +++ b/testStopwatch.c @@ -47,7 +47,7 @@ int main(){ char* timerStr = getStrStopwatch(&timer); //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 milliseconds to run this program\n", getMilliStopwatch(&timer)); printf("It took %f seconds to run this program\n", getSecondStopwatch(&timer));