Skip to content

Commit

Permalink
Merge pull request #27 from hedyhli/abs
Browse files Browse the repository at this point in the history
Add `Abs`
  • Loading branch information
StavromulaBeta authored Aug 27, 2024
2 parents e246922 + f070383 commit 2e36e2b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
{.name="floor", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="round", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="ceiling", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="abs", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="error", .calltype=call, .argc=1, .args={string}},
{.name="list", .calltype=call, .argc=1, .args={block}, .returns=true, .rettype=list},
{.name="number", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=number},
Expand Down
6 changes: 6 additions & 0 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ static STRING ___character(NUMBER);
static NUMBER ___floor(NUMBER);
static NUMBER ___round(NUMBER);
static NUMBER ___ceiling(NUMBER);
static NUMBER ___abs(NUMBER);
static void ___error(STRING);
//static BLOCK ___precompute(BLOCK);
static void ___wait(NUMBER);
Expand Down Expand Up @@ -1570,6 +1571,11 @@ static NUMBER ___ceiling(NUMBER a)
return ceil(a);
}

static NUMBER ___abs(NUMBER a)
{
return fabs(a);
}

static void ___error(STRING str)
{
throw_error(str);
Expand Down
5 changes: 5 additions & 0 deletions tests/maths.cog
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Print If == 5 Round 4.7
else
"FAIL: Round";

Print If And == 5 Abs -5 == 2.17 Abs Abs -2.17
"PASS: Abs"
else
"FAIL: Abs";

Print If == 0.3 + 0.1 0.2
"PASS: Floating point error"
else
Expand Down

0 comments on commit 2e36e2b

Please sign in to comment.