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.
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.
Re: Some useful function in lua
Posted: September 12th, 2021, 8:33 pm
by jsmorley
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.
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
Re: Some useful function in lua
Posted: September 12th, 2021, 9:07 pm
by smurfier
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.
function Round(input, idp)
return tonumber(string.format('%.' .. (idp or 0) .. 'f', input))
end
Re: Some useful function in lua
Posted: September 27th, 2021, 6:15 pm
by Yincognito
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.
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.
I believe it works for negative numbers as well, if I recall correctly. I had to add a numerical "ID" at the end of the fractional number, in order to convert it to a string suffix inside the same naive Rainmeter measure.
Don't tell me you were in London to Look at the Queen
Re: Some useful function in lua
Posted: September 27th, 2021, 7:06 pm
by Yincognito
death.crafter wrote: ↑September 27th, 2021, 6:26 pm
Missed you so much lol... Where have you been?
Don't tell me you were in London to Look at the Queen
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...