Page 3 of 3

Re: Some useful function in lua

Posted: September 12th, 2021, 8:33 pm
by death.crafter
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.

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.

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

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.

Code: Select all

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.

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. :???:
I use your method in native Rainmeter like this:

Code: Select all

[Variables]
Scale=1024
Decimals=2

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[Value]
Measure=Calc
Formula=1024
UpdateDivider=-1
DynamicVariables=1

[ValueAutoScaled]
Measure=Calc
Formula=Sgn(Value=0?1:Value)*(Round(Abs(Value)/#Scale#**Trunc(Log(Abs(Value)<1?1:Abs(Value))/Log(#Scale#)),#Decimals#)+(Trunc(Log(Abs(Value)<1?1:Abs(Value))/Log(#Scale#))+1)*(10**(-#Decimals#-2)))
UpdateDivider=-1
RegExpSubstitute=1
Substitute="^(.*\..{#Decimals#}).{1}(.{1}).*$":"\1\2","1$":"  ","2$":" K","3$":" M","4$":" G","5$":" T","6$":" P","7$":" E","8$":" Z","9$":" Y"
DynamicVariables=1

---Meters---

[MeterTest]
Meter=String
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
Text="Value = [Value] ([ValueAutoScaled])"
UpdateDivider=-1
DynamicVariables=1
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.

Re: Some useful function in lua

Posted: September 27th, 2021, 6:26 pm
by death.crafter
Yincognito wrote: September 27th, 2021, 6:15 pm
Missed you so much lol... Where have you been?
Don't tell me you were in London to Look at the Queen :rofl:

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 :rofl:
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. :D

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... ;-)

Re: Some useful function in lua

Posted: September 27th, 2021, 7:11 pm
by death.crafter
Yincognito wrote: September 27th, 2021, 7:06 pm
Ahh I see. Good luck about your teeth. Hope you get well soon. :D

Re: Some useful function in lua

Posted: September 27th, 2021, 7:38 pm
by Yincognito
death.crafter wrote: September 27th, 2021, 7:11 pm Ahh I see. Good luck about your teeth. Hope you get well soon. :D
Thanks! :great: