Jump to content
Emilee Edenflower

Doing Maths calcuations in LSL - help!

You are about to reply to a thread that has been inactive for 685 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?

Share this post


Link to post
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 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
You are about to reply to a thread that has been inactive for 685 days.

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...