It is currently May 21st, 2024, 12:37 pm

## Some useful function in lua

Discuss the use of Lua in Script measures.
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

### Re: Some useful function in lua

smurfier wrote: September 12th, 2021, 6:47 pm Just for posterity's sake, here's my scary math version of the AutoScale function. I'm retyping this by converting an old Rainmeter post in Lua, so bear with me if there are typos.

Code: Select all

``````function AutoScale(num, idp)
local scales = {'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'}
local scale = (num == 0) ? 1 : math.ceil(math.log(num) / (10 * math.log(2)))
local scaled = Round((num / 1024 ^ (scale - 1)), idp or 0)
return scaled .. ' ' .. scales[scale]
end
``````
Thank you smurfier.

I will soon update the function file with all the functions provided here.
from the Realm of Death
jsmorley
Developer
Posts: 22634
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

### Re: Some useful function in lua

smurfier wrote: September 12th, 2021, 6:47 pm Just for posterity's sake, here's my scary math version of the AutoScale function. I'm retyping this by converting an old Rainmeter post in Lua, so bear with me if there are typos.

Code: Select all

``````function AutoScale(num, idp)
local scales = {'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'}
local scale = (num == 0) ? 1 : math.ceil(math.log(num) / (10 * math.log(2)))
local scaled = Round((num / 1024 ^ (scale - 1)), idp or 0)
return scaled .. ' ' .. scales[scale]
end
``````
Don't forget though, that there is no built-in Round() function in Lua:

Code: Select all

``````function Round(num, idp)
assert(tonumber(num), 'Round expects a number.')
local mult = 10 ^ (idp or 0)
if num >= 0 then
return math.floor(num * mult + 0.5) / mult
else
return math.ceil(num * mult - 0.5) / mult
end
end
``````
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

### Re: Some useful function in lua

jsmorley wrote: September 12th, 2021, 8:33 pm Don't forget though, that there is no built-in Round() function in Lua:
Once again, for posterity's sake, there's always my version of the Round function.

Code: Select all

``````function Round(input, idp)
end
``````
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Yincognito
Rainmeter Sage
Posts: 7296
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

### Re: Some useful function in lua

smurfier wrote: September 12th, 2021, 6:47 pm Just for posterity's sake, here's my scary math version of the AutoScale function. I'm retyping this by converting an old Rainmeter post in Lua, so bear with me if there are typos.

Code: Select all

``````function AutoScale(num, idp)
local scales = {'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'}
local scale = (num == 0) ? 1 : math.ceil(math.log(num) / (10 * math.log(2)))
local scaled = Round((num / 1024 ^ (scale - 1)), idp or 0)
return scaled .. ' ' .. scales[scale]
end
``````
Ah, so it's you I have to thank for the math behind this autoscaled method (which I use, in a slightly improved form, in almost all my skins) - that thread is quite old and I lost track of who was the original author.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

### Re: Some useful function in lua

Yincognito wrote: September 27th, 2021, 6:15 pm
Missed you so much lol... Where have you been?
from the Realm of Death
Yincognito
Rainmeter Sage
Posts: 7296
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

### Re: Some useful function in lua

death.crafter wrote: September 27th, 2021, 6:26 pm Missed you so much lol... Where have you been?
As you already saw in the other thread, I ordered a new laptop (had to, the old one was going really hot), so had to take my time to test it, install stuff, adapt myself to get the best out of it, and all that (had to disassemble it as well, to put a bigger SSD and my old HDD inside, have to talk with the service to make sure I - retroactively - don't lose the warranty on it, since I didn't damage anything while doing it). Glad I hit the jackpot in terms of the hardware modules at a great price, but I'm a bit concerned when it comes to the CPU temp - although Ryzens in a HP laptop are apparently known to get a bit warmer and the CPU's technical max temp is a healthy 105C, I still feel 45C-65C could be better on idle.

The other reason I missed was that I had some health issues related to some swollen tooth, been on antibiotics for some time in order to (hopefully) extract that tooth on Thursday if the electrocardiogram / blood pressure measurements I have to take tomorrow allow adrenalin based anesthetics, so as you can imagine I wasn't in such a good mood for other stuff all this time.

Anyway, if I (again, LOL) survive this one as well, you'll probably see me more often around here, just like the usual. I'll probably ask and pose some other questions / problems too, like, for example, Rainmeter working on my integrated GPU instead of the dedicated one, despite setting the latter in the Windows 10 settings...
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
death.crafter
Rainmeter Sage
Posts: 1398
Joined: April 24th, 2021, 8:13 pm

### Re: Some useful function in lua

Yincognito wrote: September 27th, 2021, 7:06 pm
Ahh I see. Good luck about your teeth. Hope you get well soon.
from the Realm of Death
Yincognito
Rainmeter Sage
Posts: 7296
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

### Re: Some useful function in lua

death.crafter wrote: September 27th, 2021, 7:11 pm Ahh I see. Good luck about your teeth. Hope you get well soon.
Thanks!
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth