User:Acheron Gloom
IntegerLength
<lsl> integer getDigitsStrLen(integer x){//Low memory usage, but horrible performance. Don't use this, its just an example.
return llStringLength((string)x);
}
integer getDigitsLog(integer x){//Low memory usage (35 bytes), good performance. Use this if memory is your main concern. You can probably in-line it for a small performance gain.
return (integer)llLog10(x);
}
//The two functions below only work up to 8 digits. I use them for text prims. integer get8DigitsNoBranch(integer x){ //Decent memory usage (123 bytes), Better performance. Use this if you want a balanced method.
return -~((x >= 10) + (x >= 100) + (x >= 1000) + (x >= 10000) + (x >= 100000) + (x >= 1000000) + (x >= 10000000));
}
integer get8DigitsTree(integer x){//High memory usage (239 bytes), best performance. Use this if you need speed. Inline if you need even more speed.
if (x >= 10000) {
if (x >= 10000000) return 8;
if (x >= 100000){
if (x >= 1000000) return 7;
return 6;
}
return 5;
}
if (x >= 100){
if (x >= 1000) return 4;
return 3;
}
if (x >= 10) return 2;
return 1;
}
//The two functions below work up to the integer max. I don't need them, but someone might. They're just minor modifications. integer getDigitsNoBranch(integer x){ //Decent memory usage (123 bytes), Better performance. Use this if you want a balanced method.
return -~((x >= 10) + (x >= 100) + (x >= 1000) + (x >= 10000) + (x >= 100000) + (x >= 1000000) + (x >= 10000000) + (x >= 100000000) + (x >= 1000000000));
}
integer getDigitsTree(integer x){//High memory usage (239 bytes), best performance. Use this if you need speed. Inline if you need even more speed.
if(x >= 10000) {
if(x >= 10000000){
if(x >= 100000000){
if(x >= 1000000000) return 10;
return 9;
}
return 8;
}
if(x >= 100000){
if(x >= 1000000) return 7;
return 6;
}
return 5;
}
if(x >= 100){
if(x >= 1000) return 4;
return 3;
}
if(x >= 10) return 2;
return 1;
} </lsl>