baddc

integer-precision dc(1)
git clone git://bvnf.space/baddc.git
Log | Files | Refs

commit 3256b6dd5a2121382cdbf88b07ff744e21f5fb4d
parent 13357276ee223086c62609c65b33112ff986c4d2
Author: phoebos <ben@bvnf.space>
Date:   Mon, 31 Jan 2022 01:31:21 +0000

add mod, power, root; don't divide by 0

Diffstat:
MMakefile | 2+-
Mbaddc.c | 29+++++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -2,7 +2,7 @@ BIN = baddc SRCS = baddc.c -XCFLAGS = -Wall -Wextra -pedantic -g +XCFLAGS = -Wall -Wextra -pedantic -g -lm $(BIN): $(SRCS) $(CC) $(XCFLAGS) -o $(BIN) $(SRCS) diff --git a/baddc.c b/baddc.c @@ -1,5 +1,6 @@ #define _XOPEN_SOURCE 700 #include <ctype.h> +#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -85,6 +86,12 @@ void multiply(void) { void divide(void) { int div = pop(); + if (div == 0) { + fprintf(stderr, "division by 0!\n"); + pop(); + push(0); + return; + } push(pop() / div); } @@ -104,6 +111,25 @@ void O_base(void) { push(obase); } +void mod(void) { + int a = pop(); + if (a == 0) { + fprintf(stderr, "division by 0!\n"); + pop(); + push(0); + return; + } + push(pop() % a); +} + +void power(void) { + int a = pop(); + push((int)pow((double)pop(), (double)a)); +} + +void root(void) { + push((int)sqrt((double)pop())); +} struct map { char op; @@ -120,6 +146,9 @@ struct map { { 'I', I_base }, { 'o', o_base }, { 'O', O_base }, + { '%', mod }, + { '^', power }, + { 'v', root }, }; #define NUM_OPS ((sizeof(ops)) / (sizeof(*ops)))