Skip to content

add a few macros and functions #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions math.gs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@
# Return `BASE` raised to the power of `EXP`.
%define POW(BASE,EXP) antiln(ln(BASE)*(EXP))

# Return the sign of `VALUE` (-1 when negative, 1 when positive, and 0 if 0)
%define SIGN(VALUE) ((VALUE > 0) - (VALUE < 0))

func safepow(base, exp) {
return POW(abs($base), $exp) * (2 * not($base < 0 and $exp % 2) - 1);
}

func atan2(y, x) {
# It is better to make it a function since it uses the arguments multiple times
# formula from https://en.wikipedia.org/wiki/Atan2
return 2 * atan((MAG($x, $y) - $x) / $y);
}

# Get the direction to (`X`, `Y`) from (`CX`, `CY`)
%define DIR(X,Y,CX,CY) atan (((X)-(CX)) / ((Y)-(CY))) + 180 * ((CY) > (Y))

# Gamma correct `VALUE` with power 2.2
%define GAMMA(VALUE) antiln(ln(VALUE)/2.2)

Expand Down
9 changes: 9 additions & 0 deletions string.gs
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,13 @@ func countchars(text, chars) {
return count;
}

func zfill(text, zeroes) {
local ret = $text;

repeat $zeroes - length $text {
ret = "0" & ret;
}
return ret;
}

%undef strbuf
10 changes: 10 additions & 0 deletions test/test.gs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ proc test {
ASSERT_EQ(MAP(0, 1, 50, 100, 0.5), 75, "MAP(0, 1, 50, 100, 0.5)");
ASSERT_EQ(RAD("90"), PI/2, "RAD(90)");
ASSERT_EQ(DEG(PI/"2"), 90, "DEG(PI/2)");
ASSERT_EQ(SIGN(123), 1, "SIGN(123)")
ASSERT_EQ(SIGN(0), 0, "SIGN(0)")
ASSERT_EQ(SIGN(-456), -1, "SIGN(-456)")
ASSERT_EQ(safepow(3, 4), 81, "safepow(3, 4)")
ASSERT_EQ(safepow(-3, 3), -27, "safepow(-3, 3)")
ASSERT_EQ(safepow(4, -2), 0.0625, "safepow(4, -2)")
ASSERT_EQ(atan2(5, 4), 51.34019174590991, "ATAN2(5, 4)")
ASSERT_EQ(DIR(50, 50, 25, 25), 45, "DIR(50, 50, 25, 25)")

# string
ASSERT_EQ(countchar("AAA", "A"), 3, "countchar(...)");
ASSERT_EQ(countchar("AAA", "B"), 0, "countchar(...)");
Expand Down Expand Up @@ -96,6 +105,7 @@ proc test {
ASSERT_EQ(truncate("Hello world!", 12), "Hello world!", "1 shorten(...)");
ASSERT_EQ(truncate("Hello world!", 11), "Hello ...", "2 shorten(...)");
ASSERT_EQ(truncate("Hello world!", 10), "Hello...", "3 shorten(...)");
ASSERT_EQ(zfill("ABC", 6), "000ABC", "zfill(\"ABC\", 6)")
# list
split "5,3,1,2,4", ",";
INSERTION_SORT(split);
Expand Down