Does a lot of work towards the PIT
Added a math library, with clamp and POW. added printf support for printing doubles added a few helper functions in PIT for calcaulting the time in seconds of a clock cycle based on divisor
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
static void reverse(char* buf, int len)
|
||||
{
|
||||
@ -273,3 +275,35 @@ char* utoa(unsigned int num, char* buf, int base)
|
||||
reverse(buf, i); // reverse, since we did it backwards!
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int dbl_to_str(int x, char* buf, int d)
|
||||
{
|
||||
int i = 0;
|
||||
while (x) {
|
||||
buf[i++] = (x % 10) + '0';
|
||||
x = x / 10;
|
||||
}
|
||||
|
||||
while (i < d)
|
||||
buf[i++] = '0';
|
||||
|
||||
reverse(buf, i);
|
||||
buf[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
char* dtoa(double n, char* buf, int afterpoint)
|
||||
{
|
||||
int whole = (int) n;
|
||||
|
||||
double decimals = n - (double) whole;
|
||||
|
||||
int i = dbl_to_str(whole, buf, 0);
|
||||
if (afterpoint != 0) {
|
||||
buf[i] = '.'; // add the decimal
|
||||
|
||||
decimals = decimals * pow(10, afterpoint);
|
||||
dbl_to_str((int) decimals, buf + i + 1, afterpoint + 1);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
Reference in New Issue
Block a user