It is currently March 28th, 2024, 7:13 pm

Division by 0

Get help with creating, editing & fixing problems with skins
User avatar
pul53dr1v3r
Posts: 442
Joined: July 30th, 2014, 10:30 am

Division by 0

Post by pul53dr1v3r »

Hi. After I have added option to change the bar color after certain value of usage, getting the error msg on refresh named Division by 0.
Everything seeems work well, for now.

The code that causes the issue:

Code: Select all

[BarWarn]
Measure=Calc
Formula=DriveUsagePercent> 75 ? -1 : -2
Substitute="-1":"220,20,60,200","-2":"#colorBar#"

[DriveUsagePercent]
Measure=Calc
Formula=measureUsedHD1 * 100 / measureTotalHD1
MinValue=0
MaxValue=100
Percentual=1
Whole the code:

Code: Select all

[Rainmeter]
Background=#@#Background.png
; #@# is equal to Rainmeter\Skins\illustro\@Resources
BackgroundMode=3
BackgroundMargins=0,34,0,14
Update=1000

[Metadata]
; Contains basic information of the skin.
Description=Displays disk usage.

[Variables]
; Variables declared here can be used later on between two # characters (e.g. #MyVariable#).
fontName=Trebuchet MS
textSize=8
colorBar=255,140,0,200
ColorR=255,140,0,200
colorText=255,255,255,205
EmptyBarColor=255,255,255,25
HD1=F:
; UpdateInterval is set to divide update time of Disk Drive Label, Used and Total space
UpdateInterval=10

; ----------------------------------
; MEASURES return some kind of value
; ----------------------------------

[measureTotalHD1]
; This measure returns the total disk space
Measure=FreeDiskSpace
Drive=#HD1#
Total=1
IgnoreRemovable=0
UpdateDivider=#UpdateInterval#

[measureUsedHD1]
; Returns inverted value of free disk space (i.e. used disk space)
Measure=FreeDiskSpace
Drive=#HD1#
InvertMeasure=1
IgnoreRemovable=0
IfCondition=measureUsedHD1=0
IfTrueAction=[!HideMeter "meterBarHD1"][!ShowMeter "meterBarEmptyHD1"]
IfFalseAction=[!ShowMeter "meterBarHD1"][!HideMeter "meterBarEmptyHD1"]
UpdateDivider=#UpdateInterval#

; ----------------------------------
; STYLES are used to "centralize" options
; ----------------------------------

[styleTitle]
StringAlign=Center
;StringCase=UPPER
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,50
FontColor=#colorText#
FontFace=#fontName#
FontSize=10
AntiAlias=1
ClipString=1

[styleLeftText]
StringAlign=LEFT
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

[styleRightText]
StringAlign=RIGHT
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1

; ----------------------------------
; METERS display images, text, bars, etc.
; ----------------------------------

[meterTitle]
Meter=STRING
MeterStyle=styleTitle
MeasureName=MeasureDiskLabel
X=104
Y=12
W=197
H=48
Text=%1(#HD1#)
LeftMouseUpAction=[#HD1#]
MouseOverAction= [!SetOption "meterTitle" "FontColor" "#ColorR#"][!SetOption "meterTitle" "Text" "OPEN"] [!UpdateMeter "meterTitle"][!Redraw]
MouseLeaveAction= [!SetOption "meterTitle" "FontColor" "#colorText#"][!SetOption "meterTitle" "Text"  "%1(#HD1#)"] [!UpdateMeter "meterTitle"][!Redraw]
DynamicVariables=1

;ToolTipText=Open The Disk
; Even though the text is set to Disks, Rainmeter will display
; it as DISKS, because styleTitle contains StringCase=UPPER.

[meterLabelHD1]
Meter=STRING
MeterStyle=styleLeftText
MeasureName=MeasureDiskLabel
MeasureName2=measureUsedHD1
X=10
Y=40
W=90
H=14
Text="Used: %2B"
Hidden=0
NumOfDecimals=1
Autoscale=1

[MeasureDiskLabel]
Measure=FreeDiskSpace
Drive=#HD1#
Label=1
IgnoreRemovable=0
UpdateDivider=#UpdateInterval#

[meterValueHD1]
Meter=STRING
MeterStyle=styleRightText
MeasureName=measureUsedHD1
MeasureName2=measureTotalHD1
X=200
Y=0r
H=14
Text="Total: %2B"
NumOfDecimals=1
AutoScale=1

;Bar
[meterBarHD1]
Meter=BAR
MeasureName=measureUsedHD1
BarColor=[BarWarn]
SolidColor=255,255,255,25
X=10
Y=52
W=190
H=1
BarOrientation=HORIZONTAL
DynamicVariables=1

[meterBarEmptyHD1]
Meter=Image
SolidColor=#EmptyBarColor#
X=0r
Y=0r
W=190
H=1
Hidden=1

[BarWarn]
Measure=Calc
Formula=DriveUsagePercent> 75 ? -1 : -2
Substitute="-1":"220,20,60,200","-2":"#colorBar#"

[DriveUsagePercent]
Measure=Calc
Formula=measureUsedHD1 * 100 / measureTotalHD1
MinValue=0
MaxValue=100
Percentual=1

;_____________________________________________Reads

;For the graph
[MeasureHD1DiskReads]
Measure=Plugin
Plugin=PerfMon
PerfMonObject=LogicalDisk
PerfMonCounter="Disk Reads/sec"
PerfMonInstance="#HD1#"
PerfMonDifference=1
Interface=#Interface#
IfEqualValue=0
IfEqualAction=[!SetOption meterReadCircle1 ImageTint 255,255,255,100][!SetOption meterReadCircle2 ImageTint 255,255,255,100]
IfAboveValue=0
IfAboveAction=[!SetOption meterReadCircle1 ImageTint 204,0,0,255][!SetOption meterReadCircle2 ImageTint 204,0,0,255]

;for the string
[MeasureHD1DiskReadsSec]
Measure=Plugin
Plugin=PerfMon
PerfMonObject=LogicalDisk
PerfMonCounter="Disk Read Bytes/sec"
PerfMonInstance=#HD1#

[MeterHD1ReadSpeedLabel]
Meter=String
X=68
Y=58
MeterStyle=StringStyle
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=255,255,255,50
FontFace=#fontName#
FontSize=#textSize#
StringAlign=RIGHT
AntiAlias=1
ClipString=1
AlwaysOnTop=1
Text="Read:"

[MeterHD1ReadSpeed]
Meter=String
MeasureName=MeasureHD1DiskReadsSec
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=255,255,255,50
FontFace=#fontName#
FontSize=#textSize#
StringAlign=RIGHT
AntiAlias=1
ClipString=1
X=199
Y=r
Autoscale=1
MeterStyle=StringStyle
FontColor=255,255,255,255

[MeterReadLine]
Meter=Histogram
MeasureName=MeasureHD1DiskReads
GraphStart=Left
X=37
Y=60
W=160
H=25
Autoscale=1
Antialias=1
Flip=0
PrimaryColor=176,196,222,150
SolidColor=255,255,255,15

;_____________________________________________Writes

;For the graph
[MeasureHD1DiskWrites]
Measure=Plugin
Plugin=PerfMon
PerfMonObject=LogicalDisk
PerfMonCounter="Disk Writes/sec"
PerfMonInstance="#HD1#"
PerfMonDifference=1
Interface=#Interface#
IfEqualValue=0
IfEqualAction=[!SetOption meterWriteCircle1 ImageTint 255,255,255,100][!SetOption meterWriteCircle2 ImageTint 255,255,255,100]
IfAboveValue=0
IfAboveAction=[!SetOption meterWriteCircle1 ImageTint 204,0,0,255][!SetOption meterWriteCircle2 ImageTint 255,0,0,255]

;for the string
[MeasureHD1DiskWritesSec]
Measure=Plugin
Plugin=PerfMon
PerfMonObject=LogicalDisk
PerfMonCounter="Disk Write Bytes/sec"
PerfMonInstance=#HD1#

[MeterHD1WritesSpeedLabel]
Meter=String
X=72
Y=84
MeterStyle=StringStyle
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=255,255,255,50
FontFace=#fontName#
FontSize=#textSize#
StringAlign=RIGHT
AntiAlias=1
ClipString=1
AlwaysOnTop=1
Text="Write:"

[MeterHD1WritesSpeed]
Meter=String
MeasureName=MeasureHD1DiskWritesSec
StringCase=NONE
StringStyle=BOLD
StringEffect=SHADOW
FontEffectColor=0,0,0,20
FontColor=255,255,255,50
FontFace=#fontName#
FontSize=#textSize#
StringAlign=RIGHT
AntiAlias=1
ClipString=1
X=199
Y=r
Autoscale=1
MeterStyle=StringStyle
FontColor=255,255,255,255

[MeterWriteLine]
Meter=Histogram
MeasureName=MeasureHD1DiskWrites
GraphStart=Left
LineWidth=1
X=37
Y=86
W=160
H=25
Flip=0
Autoscale=1
Antialias=1
PrimaryColor=176,196,222,150
SolidColor=255,255,255,15

;_____________________________________________Indicators

[meterReadCircle1]
Meter=image
ImageName=#@#Images\Arrow.png
X=16
Y=67

[meterWriteCircle1]
Meter=image
ImageName=#@#Images\Arrow.png
X=r
Y=21r
ImageFlip=VERTICAL
Picture:
Image
Last edited by pul53dr1v3r on October 14th, 2017, 9:48 am, edited 1 time in total.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Division by 0

Post by balala »

I get the error message, only if that drive isn't plugged in. If it is, the message is gone. This is caused by the fact that in the Formula option of the [DriveUsagePercent] measure, you're using the [measureTotalHD1] measure in the denominator and as such, if the drive isn't plugged in, the [measureTotalHD1] measure returns 0.
The simplest fix would be to add an insignificant, but not zero value to the denominator of that formula: Formula=( measureUsedHD1 * 100 / ( measureTotalHD1 + 0.000001 )). This won't change too much the result, but will avoid the error message.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Division by 0

Post by jsmorley »

Although I haven't dug through the entire code, I suspect that on the first refresh, the value of measureTotalHD1 is equal to zero, and that causes the one-time error. You should be able to avoid that with:

Code: Select all

[DriveUsagePercent]
Measure=Calc
Formula=measureTotalHD1 > 0 ? (measureUsedHD1 * 100 / measureTotalHD1)  : 0
MinValue=0
MaxValue=100
P.S. Percentual is an option for String meters, and can't be used on measures.
User avatar
pul53dr1v3r
Posts: 442
Joined: July 30th, 2014, 10:30 am

Re: Division by 0

Post by pul53dr1v3r »

balala wrote:I get the error message, only if that drive isn't plugged in. If it is, the message is gone. This is caused by the fact that in the Formula option of the [DriveUsagePercent] measure, you're using the [measureTotalHD1] measure in the denominator and as such, if the drive isn't plugged in, the [measureTotalHD1] measure returns 0.
The simplest fix would be to add an insignificant, but not zero value to the denominator of that formula: Formula=( measureUsedHD1 * 100 / ( measureTotalHD1 + 0.000001 )). This won't change too much the result, but will avoid the error message.
Yes, it only appears when no drive inserted. This fix causes no more error. Ah, lower accuracy for 0.000001 means nothing. Thx!
jsmorley wrote:Although I haven't dug through the entire code, I suspect that on the first refresh, the value of measureTotalHD1 is equal to zero, and that causes the one-time error. You should be able to avoid that with:

Code: Select all

[DriveUsagePercent]
Measure=Calc
Formula=measureTotalHD1 > 0 ? (measureUsedHD1 * 100 / measureTotalHD1) : 0
MinValue=0
MaxValue=100
Regrettably, this formula doens't help.
P.S. Percentual is an option for String meters, and can't be used on measures.
Adopted and corrected. Thanks for the note!
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Division by 0

Post by balala »

Pul53dr1v3r wrote:This fix causes no more error. Ah, lower accuracy for 0.000001 means nothing.
In fact doesn't matter too much the used value, important is to above zero, but small enough to doesn't change the result.
Glad to help.