Jump to content

Doing Maths calcuations in LSL - help!


Emilee Edenflower
 Share

You are about to reply to a thread that has been inactive for 311 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

OK, hopefully someone can help me here as whatever I do always results in a 0 (zero) output! :( 

I'm just trying to do a simple calculation to work out the percentage of memory. It really shouldn't be so difficult, should it?? I thought maybe there was limitation with how much maths could be done in one line, so I split the two calculations up and it still doesn't work as expected. 

The calculation should look something like: 34968 / 65536 * 100 which rounded up should be about 53%. I wasn't sure if you could use parenthesis in it or not, but it didn't work anyway (eg: (34968 / 65536) * 100)

integer used_memory = llGetUsedMemory();
integer total_memory = llGetMemoryLimit();
float percentageMaths = used_memory / total_memory;
float percentageMemory = percentageMaths * 100;
llOwnerSay("Memory used as percentage: " + (string)llRound(percentageMemory)); //result as: "Memory used as percentage: 0"

Any insights?

Link to comment
Share on other sites

You are doing integer division there. 34968 / 65536 equals 0. If you cast one or both of them as floats it should work: (float) 34968 / (float) 65536 would give 0.533... I'm not online to test it, but that applies in lots of programming languages.

  • Like 2
Link to comment
Share on other sites

17 minutes ago, angeoco said:

You are doing integer division there. 34968 / 65536 equals 0. If you cast one or both of them as floats it should work: (float) 34968 / (float) 65536 would give 0.533... I'm not online to test it, but that applies in lots of programming languages.

Thanks, that worked now! I'm more of a PHP coder so it didn't even occur to me to cast them as floats if they were already integers, as this calculation works fine in PHP (I checked to make before I posted the OP to make sure my maths was correct!).

Anyway, this now gives the right result:

integer percentageMemory = llRound(((float)used_memory / (float)total_memory) * 100);

:) 

  • Like 2
Link to comment
Share on other sites

  • 4 years later...
On 8/24/2018 at 9:57 PM, angeoco said:

You are doing integer division there. 34968 / 65536 equals 0. If you cast one or both of them as floats it should work: (float) 34968 / (float) 65536 would give 0.533... I'm not online to test it, but that applies in lots of programming languages.

Helped me as well.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 311 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...