From f070383d21eadb9037c5e897a59613b4a5ece4a1 Mon Sep 17 00:00:00 2001 From: ~hedy Date: Tue, 27 Aug 2024 08:02:10 +0800 Subject: [PATCH] Add `Abs` --- src/builtins.c | 1 + src/runtime.h | 6 ++++++ tests/maths.cog | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/src/builtins.c b/src/builtins.c index 0f20917d..bf2ea83d 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -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}, diff --git a/src/runtime.h b/src/runtime.h index 44b6133e..d385bbb8 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -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); @@ -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); diff --git a/tests/maths.cog b/tests/maths.cog index e1c381cd..e31921ef 100644 --- a/tests/maths.cog +++ b/tests/maths.cog @@ -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