summaryrefslogtreecommitdiff
path: root/npc/functions
diff options
context:
space:
mode:
authorFedja Beader <fedja@protonmail.ch>2025-01-29 01:11:10 +0100
committerFedja Beader <fedja@protonmail.ch>2025-01-29 01:11:10 +0100
commitc6652f6a21d254dc502fb290f5771bcb58020d6d (patch)
treedb4d14f022fc3c863d3ff3260a2a138f6c3824cd /npc/functions
parent18f2b5d9162bedb886cd6206c130dfdb76c5b667 (diff)
downloadserverdata-simplify_logarithm.tar.gz
serverdata-simplify_logarithm.tar.bz2
serverdata-simplify_logarithm.tar.xz
serverdata-simplify_logarithm.zip
Simplify base 2 logarithm computationsimplify_logarithm
Diffstat (limited to 'npc/functions')
-rw-r--r--npc/functions/math.txt32
1 files changed, 9 insertions, 23 deletions
diff --git a/npc/functions/math.txt b/npc/functions/math.txt
index 22cf88159..d54558352 100644
--- a/npc/functions/math.txt
+++ b/npc/functions/math.txt
@@ -40,31 +40,17 @@ function script lognbaselvl {
}
// log2(<int>)
-// returns the log base 2 of the passed integer, up to 20 (2**20=1.048.576) (round down always)
-
+// returns the base 2 logarithm of the passed integer
+// Note: Negative values are first converted to positive.
+// For 0, -1 is returned instead
+// (as if passed value == 0.5, but that is not possible for integers).
function script log2 {
- .@v=abs(getarg(0));
- .@ok=0;
- .@i=0;
- if (.@v < 1)
- return -1;
-
- freeloop(true);
- while (!.@ok) {
- // exact match
- if (2**.@i == .@v) {
- .@ok=1;
- // inexact match, or limit exceeded
- } else if (2**.@i >= .@v || .@i > 20) {
- .@ok=1;
- .@i-=1; // round down
- // not yet
- } else {
- .@i+=1;
- }
+ .@v = abs(getarg(0));
+ .@i = -1;
+ while (.@v > 0) {
+ .@i += 1;
+ .@v >>= 1;
}
- freeloop(false);
-
return .@i;
}