It is currently October 19th, 2020, 9:18 pm

[EXPECTED BEHAVIOR?] Decimals in Variables

Report bugs with the Rainmeter application and suggest features.
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

[EXPECTED BEHAVIOR?] Decimals in Variables

Post by Yincognito »

I made an accurate solar system skin and as you can imagine, I'm using very large and very small numbers in it. While trying to make a "zoom effect", I came across this particular (yet annoying) issue: variables don't "remember" what's past the 5th decimal, as you can see in the below code, which manages to keep the measure and the variable equal only if you uncomment setting the Change variable to a 5 decimal value.

Code: Select all

[Variables]
Factor=0.00000007
Change=0.00000001
;Change=0.00001

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

---Measures---

[Factor]
Measure=Calc
Formula=#Factor#
DynamicVariables=1

---Meters---

[MeterTest]
Meter=STRING
X=0
Y=0
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
MeasureName=Factor
NumOfDecimals=8
Text="Change (variable) = #Change##CRLF#Factor (measure)  = %1#CRLF#Factor (variable) = #Factor#"
MouseScrollUpAction=[!SetOption Factor Formula ([Factor:]+#Change#)][!SetVariable Factor (#Factor#+#Change#)][!UpdateMeasure *][!UpdateMeter *][!Redraw]
MouseScrollDownAction=[!SetOption Factor Formula ([Factor:]-#Change#)][!SetVariable Factor (#Factor#-#Change#)][!UpdateMeasure *][!UpdateMeter *][!Redraw]
DynamicVariables=1
Preview:
Variable Decimals.jpg
Any solution to this, other than turning the variable into a measure and replacing lots of its occurences with the measure syntax? Also, is this the expected behavior or some kind of thing that's missing? I know that numerical strings are displayed up to the 5th decimal in the log and there are probably some internal reasons for this (like using less bytes when holding internal variables in the Rainmeter source code?), but it's affecting the actual result of operations...
You do not have the required permissions to view the files attached to this post.
User avatar
Active Colors
Moderator
Posts: 618
Joined: February 16th, 2012, 3:32 am

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Active Colors »

Is your number of decimals changeable or you have exactly seven decimals all the time? I suppose it is fixed since you have precies Change variable. Just asking...
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Yincognito »

Active Colors wrote: September 30th, 2020, 10:44 pm Is your number of decimals changeable or you have exactly seven decimals all the time? I suppose it is fixed since you have precies Change variable. Just asking...
In my actual code, it is changeable - I can edit it or set it to anything. Here, for simplicity, it doesn't appear to change.
The reason why it's set to 8 decimals is because 0.00000007 is precisely the right factor to multiply the solar system distances (in km) to fit the orbit of the last (accepted) planet, i.e. Neptune, with 4540000000 km, in a 640x640 rectangle "viewport". Can't use a larger viewport as my screen is "only" 1366x768.

By the way, I eventually changed the variable to a measure (had 3 factors in the actual code: one for distance, one for body radius, and one for orbit angle, so 3 additional measures), but I would still be interested in the answer to this question.
User avatar
Brian
Developer
Posts: 2067
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Brian »

This lies within a grey area of "expected behavior". When converting between strings and numbers, Rainmeter makes certain assumptions about "how" the number is being used. In 99.99% of cases (possibly higher than that), a floating point precision of 5 decimal places is good enough. It basically comes down to a trade off between accuracy and speed.

In this particular case, the "issue" lies with the !SetVariable bang. Rainmeter explicitly only uses up to 5 decimal places after parsing a formula with this bang. Here is the code that deals with this. Changing its precision from "5" to something larger is easy...but is doesn't solve any other areas where this "precision assumption" is used (like in the About dialog).

Ideally it would be better to provide some way of defining precision per skin or "on demand" for certain calculations. Finding the best solution that gives the most flexibility while not having much negative performance is somewhat tricky. :D

-Brian
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Yincognito »

Brian wrote: October 1st, 2020, 6:30 amThis lies within a grey area of "expected behavior".
Aren't they all? :lol:
Brian wrote: October 1st, 2020, 6:30 amIt basically comes down to a trade off between accuracy and speed.
Yep, that's what I thought. In this particular case, I'll go with speed, since my simulator already takes a bit (too much) of CPU due to the actual (or virtual, even though they are inside a Container) size of the meters (e.g. orbital ellipses and such, redrawn every 25 ms despite their negative UpdateDivider, just because they belong to the same skin as the actual animated tiny Shape meters of the celestial bodies).
Brian wrote: October 1st, 2020, 6:30 amHere is the code that deals with this. Changing its precision from "5" to something larger is easy...but is doesn't solve any other areas where this "precision assumption" is used (like in the About dialog). Ideally it would be better to provide some way of defining precision per skin or "on demand" for certain calculations. Finding the best solution that gives the most flexibility while not having much negative performance is somewhat tricky. :D
I understand. As I said, I go with performance on this one, since fortunately inside a Calc measure the issue seems to be somewhat alleviated (by the way, is there such a limit in Calc measures?), and I already had to use those measures to resolve potential formulas to actual numbers that can be shown to the user, so no big deal. As for a way of defining precision by skin, a simple DecimalPrecision=N option in the [Rainmeter] section will probably be enough. Not sure about the negative performance impact, that's your call to weigh in if it would be bearable or not.

As always, thanks for answering and providing the technical details of this - much appreciated. :thumbup:
User avatar
Active Colors
Moderator
Posts: 618
Joined: February 16th, 2012, 3:32 am

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Active Colors »

Yincognito wrote: October 1st, 2020, 1:49 am In my actual code, it is changeable - I can edit it or set it to anything. Here, for simplicity, it doesn't appear to change.
The reason why it's set to 8 decimals is because 0.00000007 is precisely the right factor to multiply the solar system distances (in km) to fit the orbit of the last (accepted) planet, i.e. Neptune, with 4540000000 km, in a 640x640 rectangle "viewport". Can't use a larger viewport as my screen is "only" 1366x768.

By the way, I eventually changed the variable to a measure (had 3 factors in the actual code: one for distance, one for body radius, and one for orbit angle, so 3 additional measures), but I would still be interested in the answer to this question.
I see. I thought to suggest to split the number into leading zeros and non zeros:

Code: Select all

FactorZeros=0.0000000
FactorEnding=7
FactorChange=1
Factor=#FactorZeros##FactorEnding#
but that would require some sort of variable updating and of course this is not elegant workaround. Still thinking about what simpler solution could look like.
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Yincognito »

Active Colors wrote: October 1st, 2020, 12:03 pm I see. I thought to suggest to split the number into leading zeros and non zeros:

Code: Select all

FactorZeros=0.0000000
FactorEnding=7
FactorChange=1
Factor=#FactorZeros##FactorEnding#
but that would require some sort of variable updating and of course this is not elegant workaround. Still thinking about simpler solution.
Yes, thought about something similar as well, like applying some sort of "E notation", e.g.

Code: Select all

FactorBase=7
FactorChange=1
PowerOfTen=-8
and then doing all variable calculations with the first 2 variables and apply the power only when the result of computing is obviously a less than 5 decimals number (so that the result is stored properly). But then, as you said, it's quite inconvenient and you'd have to always consider whether to use the power or not based on the expected result... :???:
User avatar
Active Colors
Moderator
Posts: 618
Joined: February 16th, 2012, 3:32 am

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Active Colors »

Yincognito wrote: October 1st, 2020, 12:32 pm Yes, thought about something similar as well, like applying some sort of "E notation", e.g.

Code: Select all

FactorBase=7
FactorChange=1
PowerOfTen=-8
and then doing all variable calculations with the first 2 variables and apply the power only when the result of computing is obviously a less than 5 decimals number (so that the result is stored properly). But then, as you said, it's quite inconvenient and you'd have to always consider whether to use the power or not based on the expected result... :???:
Yeah it is indeed creating an extra friction in the process. I will try to play around again later, I haven't tried out all the variants yet!
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: [EXPECTED BEHAVIOR?] Decimals in Variables

Post by Yincognito »

Active Colors wrote: October 1st, 2020, 3:58 pm Yeah it is indeed creating an extra friction in the process. I will try to play around again later, I haven't tried out all the variants yet!
Ok - I would be curious to find out what you'll try and the results. ;-)