Added math library

pull/38/head
E. Almqvist 3 years ago
parent 40b8aa7a3d
commit 45ff8a854f
  1. 5
      lib/conv.c
  2. 10
      lib/math.c
  3. 3
      lib/math.h
  4. 3
      lib/util.c

@ -4,10 +4,11 @@ void int_to_str(int i, char* buf) {
ulong num = (ulong)i; // convert to ulong
uint len = ulong_len(num); // number of digits
&buf[len] = '\0'; // add a "end-of-string" at the end
*(buf+len) = '\0'; // add a "end-of-string" at the end
int j;
for(j = 0; j < len; j++) // iterate over each digit and assign it to the buffer
&buf[j] = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char
// super dangerous memory write
*(buf+j) = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char
}

@ -0,0 +1,10 @@
#include "math.h"
long pow(int num, uint expon) {
long prod = 1;
while(expon > 0)
prod *= num;
--expon;
return prod;
}

@ -0,0 +1,3 @@
#include "types.h"
long pow(int, uint);

@ -10,5 +10,6 @@ uint ulong_len(ulong n) { // get the digit length of a number
}
uint ndigit(ulong n, uint i) { // OBS: index order is reversed
return (n/(10**i)) % 10
long den = pow(10, i);
return (n/den) % 10;
}

Loading…
Cancel
Save