You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/libs/stdlib.ts
+22-1Lines changed: 22 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -32,4 +32,25 @@ inline function getDigit(a, b) {
32
32
//Returns 1 if the input is an integer, and 0 otherwise.
33
33
inline function isInt(a) {
34
34
state = a == floor(a);
35
-
}`;
35
+
}
36
+
37
+
//Safely divides a number (a/b), such that if b is 0 (a/0), it will return 0 instead of undefined.
38
+
//Normally, if you divide a number by 0 (even if inside a statement that doesn't run) it will return
39
+
//undefined for the entire function. This solves that issue. Use this anywhere that the divisor may
40
+
//be 0.
41
+
inline function safeDivide(a, b) {
42
+
if(b == 0) {
43
+
state = 0;
44
+
} else {
45
+
//This double check is needed because it will run, and needs to divide by a non-zero number. 1 is a "safe" value, but it's being used arbitrarily here.
46
+
//No matter what number is used, it won't ever appear, unless it results in undefined (which will only happen if the value is 0 or infinity).
0 commit comments