summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
}