Skip to content

Commit

Permalink
standard lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 27, 2023
1 parent 8c9bd55 commit e1733fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 75 deletions.
12 changes: 6 additions & 6 deletions std/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include <stdlib.h>
#include <string.h>

void wait(long long x) {
void wait(int x) {
sleep(x);
}

long long slen(char *str) {
return (long long) strlen(str);
int slen(char *str) {
return (int) strlen(str);
}

char charAt(char *str, long long x) {
char charAt(char *str, int x) {
return str[x];
}

long long parse_int(char *str) {
return (long long) atoi(str);
int parse_int(char *str) {
return (int) atoi(str);
}
2 changes: 1 addition & 1 deletion std/stdmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <time.h>


long long randGen(long long min, long long max) {
int randGen(int min, int max) {
srandom((unsigned) time(NULL));
return random() % (max - min + 1) + min;
}
39 changes: 20 additions & 19 deletions std/stdmath.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ fn fmin(x: Float, y: Float) Float {
return y;
}

fn pow(x: Int, y: Int) Int {
while y > 1 {
x = x*x;
y = y-1;
fn pow(x: Float, y: Float) Float {
var power: Float = x;
while y > 1.0 {
x = power*x;
y = y-1.0;
}
return x;
}

fn floor(x: Float) Float {
return x - fabs(x % 1.0);
}
#fn floor(x: Float) Int {
# return x - fabs(x % 1);
#}

fn ceil(x: Float) Float {
if x < 0.0 {
return floor(x) - 1.0;
}
return floor(x) + 1.0;
}
#fn ceil(x: Float) Int {
# if x < 0.0 {
# return floor(x) - 1;
# }
# return floor(x) + 1;
#}

fn round(x: Float) Float {
if fabs(x % 1.0) >= 0.5 {
return ceil(x);
}
return floor(x);
}
#fn round(x: Float) Float {
# if fabs(x % 1.0) >= 0.5 {
# return ceil(x);
# }
# return floor(x);
#}
70 changes: 21 additions & 49 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
#include <stdio.h>

typedef struct {
int size;
int cap;
int* inner;
} IntVec;

IntVec* test();
int testtwo(IntVec*);
int testthree(IntVec*);
int testfour(IntVec*);

void testback(int x) {
printf("%d, ", x);
}
float pow(float, float);
int abs(int);
float fabs(float);
int max(int, int);
int min(int, int);
float fmax(float, float);
float fmin(float, float);

int main() {
IntVec* v = test();

int size = (*v).size;
int cap = (*v).cap;
int* inner = (*v).inner;

printf("%d\n", size);
printf("%d\n", cap);

for (int i = 0; i < size; ++i) {
int value = inner[i];
printf("%d ", value);
}
puts("\n\n");

testtwo(v);

size = (*v).size;
cap = (*v).cap;
inner = (*v).inner;

printf("%d\n", size);
printf("%d\n", cap);

for (int i = 0; i < size; ++i) {
int value = inner[i];
printf("%d ", value);
}
puts("\n\n");
int i = testthree(v);
printf("%d ", i);
puts("\n\n");
testfour(v);
puts("");
int x = pow(5, 3);
printf("pow %d\n", x);
int y = abs(-5);
printf("abs %d\n", y);
double z = fabs(-5.0);
printf("fabs %f\n", z);
int w = max(5, 3);
printf("max %d\n", w);
int n = min(5, 3);
printf("min %d\n", n);
int p = fmax(5.0, 3.0);
printf("fmax %d\n", p);
int g = fmin(5.0, 3.0);
printf("fmin %d\n", g);
}

0 comments on commit e1733fc

Please sign in to comment.