From b64edd77652665b312aa2fd693d507ad3a81dc69 Mon Sep 17 00:00:00 2001 From: Matheus Date: Thu, 24 Nov 2022 18:27:12 -0300 Subject: [PATCH] math.h: intpow --- src/include/Makefile | 4 ++-- src/include/math.h | 6 ++++++ src/include/math/Makefile | 12 ++++++++++++ src/include/math/pow.c | 20 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/include/math.h create mode 100644 src/include/math/Makefile create mode 100644 src/include/math/pow.c diff --git a/src/include/Makefile b/src/include/Makefile index 4b5f4feb..cd68e74e 100644 --- a/src/include/Makefile +++ b/src/include/Makefile @@ -1,6 +1,6 @@ objects := $(patsubst %.c,%.o,$(wildcard *.c)) -subdirs := stdio string hashtable ringbuffer -OBJPATH=$(OBJDIR)/include/ringbuffer/ +subdirs := stdio string hashtable ringbuffer math +OBJPATH=$(OBJDIR)/include/ export all: $(subdirs) $(objects) diff --git a/src/include/math.h b/src/include/math.h new file mode 100644 index 00000000..ce4db273 --- /dev/null +++ b/src/include/math.h @@ -0,0 +1,6 @@ +#ifndef _MATH_H_INCLUDE +#define _MATH_H_INCLUDE + +long intpow(long base, long exp); + +#endif diff --git a/src/include/math/Makefile b/src/include/math/Makefile new file mode 100644 index 00000000..e2c521ee --- /dev/null +++ b/src/include/math/Makefile @@ -0,0 +1,12 @@ +objects := $(patsubst %.c,%.o,$(wildcard *.c)) +subdirs := +OBJPATH=$(OBJDIR)/include/math/ +export + +all: $(subdirs) $(objects) + for subdir in $(subdirs) ; do $(MAKE) -C $${subdir} ; done + +%.o: %.c + $(CC) $(CFLAGS) -o $@ $< + mkdir -p $(OBJPATH) + cp $@ $(OBJPATH)/$@ diff --git a/src/include/math/pow.c b/src/include/math/pow.c new file mode 100644 index 00000000..2bcdbced --- /dev/null +++ b/src/include/math/pow.c @@ -0,0 +1,20 @@ +#include + +long intpow(long base, long exp){ + if(exp == 0) + return 1; + + long mult = base; + + // this is kinda inefficient but works for now I guess + + // i = 1 because value only really changes when the exponent is 2 + + for(int i = 1; i < exp; ++i){ + base *= mult; + } + + return base; + + +}