[chronojump] Util DivideSafe methods moved to UtilAll



commit 7baaa69727a76a01f2e38317059595865a5d8790
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Jan 8 16:17:44 2020 +0100

    Util DivideSafe methods moved to UtilAll

 src/encoder.cs                 |  8 ++++----
 src/encoderRhythm.cs           |  6 +++---
 src/execute/event.cs           |  2 +-
 src/forceSensor.cs             | 24 ++++++++++++------------
 src/gui/app1/forceSensor.cs    |  2 +-
 src/gui/cairo/jumpsProfile.cs  |  2 +-
 src/gui/cairo/xy.cs            |  2 +-
 src/gui/encoder.cs             |  8 ++++----
 src/gui/encoderGraphObjects.cs |  2 +-
 src/gui/preferences.cs         |  4 ++--
 src/gui/webcam.cs              |  4 ++--
 src/json/compujump.cs          |  4 ++--
 src/sqlite/main.cs             |  8 ++++----
 src/util.cs                    | 36 ------------------------------------
 src/utilDate.cs                |  5 +++++
 15 files changed, 43 insertions(+), 74 deletions(-)
---
diff --git a/src/encoder.cs b/src/encoder.cs
index ce0e5693..61ea7d78 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -670,11 +670,11 @@ public class EncoderSignal
                                lowest = compareTo;
 
                        //LogB.Information(string.Format("Loss ecc/con (by con) of {0}; i: {1} is: {2}", 
variable, i++,
-                       //                      Convert.ToInt32(Util.DivideSafe(100.0 * (highest - lowest), 
highest))));
+                       //                      Convert.ToInt32(UtilAll.DivideSafe(100.0 * (highest - 
lowest), highest))));
 
                        ecc = true;
                }
-               return Convert.ToInt32(Util.DivideSafe(100.0 * (highest - lowest), highest));
+               return Convert.ToInt32(UtilAll.DivideSafe(100.0 * (highest - lowest), highest));
 
        }
        /*
@@ -705,11 +705,11 @@ public class EncoderSignal
                                        lowest = (eccValue + conValue) / 2;
                        }
                        //LogB.Information(string.Format("Loss ecc/con (ecc?: {0}) of {1}; i: {2} is: {3}", 
ecc.ToString(), variable, i++,
-                       //                      Convert.ToInt32(Util.DivideSafe(100.0 * (highest - lowest), 
highest))));
+                       //                      Convert.ToInt32(UtilAll.DivideSafe(100.0 * (highest - 
lowest), highest))));
 
                        ecc = ! ecc;
                }
-               return Convert.ToInt32(Util.DivideSafe(100.0 * (highest - lowest), highest));
+               return Convert.ToInt32(UtilAll.DivideSafe(100.0 * (highest - lowest), highest));
 
        }
        */
diff --git a/src/encoderRhythm.cs b/src/encoderRhythm.cs
index f19c0798..22653419 100644
--- a/src/encoderRhythm.cs
+++ b/src/encoderRhythm.cs
@@ -328,15 +328,15 @@ public class EncoderRhythmExecute
                        textRest = "";
 
                        if(currentPhase == phases.REPETITION)
-                               fraction = Util.DivideSafeFraction(phaseSeconds, encoderRhythm.RepSeconds);
+                               fraction = UtilAll.DivideSafeFraction(phaseSeconds, encoderRhythm.RepSeconds);
                        if(currentPhase == phases.ECC)
                        {
-                               fraction =  1 - Util.DivideSafeFraction(phaseSeconds, 
encoderRhythm.EccSeconds);
+                               fraction =  1 - UtilAll.DivideSafeFraction(phaseSeconds, 
encoderRhythm.EccSeconds);
                                textRepetition = "Excentric";
                        }
                        if(currentPhase == phases.CON)
                        {
-                               fraction = Util.DivideSafeFraction(phaseSeconds, encoderRhythm.ConSeconds);
+                               fraction = UtilAll.DivideSafeFraction(phaseSeconds, encoderRhythm.ConSeconds);
                                textRepetition = "Concentric";
                        }
                        if(encoderRhythm.UseClusters())
diff --git a/src/execute/event.cs b/src/execute/event.cs
index 55b95da2..6ea3008f 100644
--- a/src/execute/event.cs
+++ b/src/execute/event.cs
@@ -632,7 +632,7 @@ public class PhaseTime
                        strMode = "OUT (TF)";
 
                //TODO: use a printf mode to have always same digits
-               return "\n" + Math.Round(Util.DivideSafe(duration, 1000.0), 3) + " - " + strMode;
+               return "\n" + Math.Round(UtilAll.DivideSafe(duration, 1000.0), 3) + " - " + strMode;
        }
 
        public bool IsContact
diff --git a/src/forceSensor.cs b/src/forceSensor.cs
index 6cac4f14..021c91fe 100644
--- a/src/forceSensor.cs
+++ b/src/forceSensor.cs
@@ -769,16 +769,16 @@ public class ForceSensorCapturePoints
                 * simmetrical positive / negative
                return Convert.ToInt32(
                                (heightG/2)
-                               - ( Util.DivideSafe((force * heightG), (1.0 * RealHeightG)) )
+                               - ( UtilAll.DivideSafe((force * heightG), (1.0 * RealHeightG)) )
                                );
                                */
                return Convert.ToInt32(
                                heightG
-                               - Util.DivideSafe(
+                               - UtilAll.DivideSafe(
                                                (force * (heightG - (marginTop + marginBottom))),
                                                (1.0 * (RealHeightG + RealHeightGNeg))
                                                )
-                               - Util.DivideSafe(
+                               - UtilAll.DivideSafe(
                                                RealHeightGNeg * (heightG - (marginTop + marginBottom)),
                                                (1.0 * (RealHeightG + RealHeightGNeg))
                                                )
@@ -851,7 +851,7 @@ public class ForceSensorCapturePoints
                }
 
                double elapsedSeconds = times[countB]/1000000.0 - times[countA]/1000000.0;
-               return sum * Util.DivideSafe(elapsedSeconds, samples);
+               return sum * UtilAll.DivideSafe(elapsedSeconds, samples);
        }
        public void GetVariabilityAndAccuracy(int countA, int countB, int feedbackF, out double variability, 
out double feedbackDifference)
        {
@@ -878,7 +878,7 @@ public class ForceSensorCapturePoints
                for(int i = countA; i <= countB; i ++)
                        sum += Math.Abs(forces[i]-avg);
 
-               variability = Util.DivideSafe(sum, numSamples);
+               variability = UtilAll.DivideSafe(sum, numSamples);
 
                // 3) Calculate difference.
                // Average of the differences between force and average
@@ -888,7 +888,7 @@ public class ForceSensorCapturePoints
                for(int i = countA; i <= countB; i ++)
                        sum += Math.Abs(forces[i]-feedbackF);
 
-               feedbackDifference = Util.DivideSafe(sum, numSamples);
+               feedbackDifference = UtilAll.DivideSafe(sum, numSamples);
        }
 
        public int MarginLeft
@@ -1500,7 +1500,7 @@ public class ForceSensorAnalyzeInstant
 
                /*
                 * note samples don't come at same time separation, so this does not work:
-               double px = Util.DivideSafe(
+               double px = UtilAll.DivideSafe(
                                (graphWidth - leftMargin - rightMargin) * currentPos,
                                totalPos -1); //-1 ok
                                //fscAIPoints.RealWidthG);
@@ -1509,7 +1509,7 @@ public class ForceSensorAnalyzeInstant
                double currentTime = fscAIPoints.GetTimeAtCount(currentPos);
                double lastTime = fscAIPoints.GetLastTime();
 
-               double px = Util.DivideSafe(
+               double px = UtilAll.DivideSafe(
                                (graphWidth - leftMargin - rightMargin) * currentTime,
                                lastTime);
 
@@ -1626,7 +1626,7 @@ public class ForceSensorAnalyzeInstant
                y0 = height - y0;
                y = height -y;
 
-               return Convert.ToInt32(Util.DivideSafe(y - y0 + x0*RFD, RFD));
+               return Convert.ToInt32(UtilAll.DivideSafe(y - y0 + x0*RFD, RFD));
        }
        */
 
@@ -1641,7 +1641,7 @@ public class ForceSensorAnalyzeInstant
                int pointYAfter = GetPxAtForce(GetForceAtCount(countRFDMax +1));
 
                // 2) calculate the slope of the line that could pass across this points
-               double slope = Math.Abs( Util.DivideSafe( pointYAfter - pointYBefore,
+               double slope = Math.Abs( UtilAll.DivideSafe( pointYAfter - pointYBefore,
                                        (1.0 * (pointXAfter- pointXBefore)) ) );
 
                // 3) get the RFD point
@@ -1649,10 +1649,10 @@ public class ForceSensorAnalyzeInstant
                int pointYRFD = GetPxAtForce(GetForceAtCount(countRFDMax));
 
                // 4) calculate line that cross RFD point with calculated slope
-               lineXStart = pointXRFD - Convert.ToInt32(Util.DivideSafe(
+               lineXStart = pointXRFD - Convert.ToInt32(UtilAll.DivideSafe(
                                        (graphHeight - pointYRFD),
                                        slope));
-               lineXEnd = pointXRFD + Convert.ToInt32(Util.DivideSafe(
+               lineXEnd = pointXRFD + Convert.ToInt32(UtilAll.DivideSafe(
                                        (pointYRFD - 0),
                                        slope));
                lineYStart = graphHeight;
diff --git a/src/gui/app1/forceSensor.cs b/src/gui/app1/forceSensor.cs
index 5e89c5ac..1f682f47 100644
--- a/src/gui/app1/forceSensor.cs
+++ b/src/gui/app1/forceSensor.cs
@@ -1971,7 +1971,7 @@ LogB.Information(" fs R ");
                        absoluteMaxForce = Math.Abs(minForce);
 
                //show 5 steps positive, 5 negative (if possible)
-               int temp = Convert.ToInt32(Util.DivideSafe(absoluteMaxForce, 5.0));
+               int temp = Convert.ToInt32(UtilAll.DivideSafe(absoluteMaxForce, 5.0));
                int step = temp;
 
                //to have values multiples than 10, 100 ...
diff --git a/src/gui/cairo/jumpsProfile.cs b/src/gui/cairo/jumpsProfile.cs
index 9f678777..077fffce 100644
--- a/src/gui/cairo/jumpsProfile.cs
+++ b/src/gui/cairo/jumpsProfile.cs
@@ -113,7 +113,7 @@ public static class JumpsProfileGraph
                //g.SelectFontFace("Helvetica", Cairo.FontSlant.Normal, Cairo.FontWeight.Bold);
                foreach(JumpsProfileIndex jpi in l_jpi)
                {
-                       double percent = 100 * Util.DivideSafe(jpi.Result, sum);
+                       double percent = 100 * UtilAll.DivideSafe(jpi.Result, sum);
                        printText(legendX,  y, 24, textHeight, Util.TrimDecimals(percent, 1) + jpi.Text, g, 
false);
                        if(percent != 0)
                                drawRoundedRectangle (legendX,  y+30 , Convert.ToInt32(2 * percent), 20, 4, 
g, jpi.Color);
diff --git a/src/gui/cairo/xy.cs b/src/gui/cairo/xy.cs
index 9bf564aa..a996aad0 100644
--- a/src/gui/cairo/xy.cs
+++ b/src/gui/cairo/xy.cs
@@ -321,7 +321,7 @@ public abstract class CairoXY
        private double getGridStep(double min, double max, int seps)
        {
                //show 5 steps positive, 5 negative (if possible)
-               double temp = Util.DivideSafe(max - min, seps);
+               double temp = UtilAll.DivideSafe(max - min, seps);
                double step = temp;
 
                //to have values multiples than 10, 100 ...
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index a25a81d2..e40bf115 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -6228,7 +6228,7 @@ public partial class ChronoJumpWindow
                        if(action == encoderActions.CAPTURE_IM)
                                selectedTime = preferences.encoderCaptureTimeIM;
 
-                       encoder_pulsebar_capture.Fraction = Util.DivideSafeFraction(
+                       encoder_pulsebar_capture.Fraction = UtilAll.DivideSafeFraction(
                                        (selectedTime - eCapture.Countdown), selectedTime);
                        encoder_pulsebar_capture.Text = eCapture.Countdown + " s";
        
@@ -6247,7 +6247,7 @@ public partial class ChronoJumpWindow
 
                                //-48: ascii 0 char
                                if(System.Char.IsDigit(contents[1]) && System.Char.IsDigit(contents[3]))
-                                       fraction = Util.DivideSafeFraction(
+                                       fraction = UtilAll.DivideSafeFraction(
                                                        Convert.ToInt32(contents[1]-48), 
Convert.ToInt32(contents[3]-48) );
                        }
                        */
@@ -6289,14 +6289,14 @@ public partial class ChronoJumpWindow
                                if(fraction == -1)
                                        encoder_pulsebar_capture.Pulse();
                                else
-                                       encoder_pulsebar_capture.Fraction = Util.DivideSafeFraction(fraction, 
6); 
+                                       encoder_pulsebar_capture.Fraction = 
UtilAll.DivideSafeFraction(fraction, 6);
                                
                                encoder_pulsebar_capture.Text = contents;
                        } else {
                                if(fraction == -1)
                                        encoder_pulsebar_analyze.Pulse();
                                else
-                                       encoder_pulsebar_analyze.Fraction = Util.DivideSafeFraction(fraction, 
6);
+                                       encoder_pulsebar_analyze.Fraction = 
UtilAll.DivideSafeFraction(fraction, 6);
 
                                encoder_pulsebar_analyze.Text = contents;
                        }
diff --git a/src/gui/encoderGraphObjects.cs b/src/gui/encoderGraphObjects.cs
index dd9ed4e9..bc2dc2f3 100644
--- a/src/gui/encoderGraphObjects.cs
+++ b/src/gui/encoderGraphObjects.cs
@@ -588,7 +588,7 @@ public class EncoderGraphDoPlot
                        if(dataSecondary.Count > 0)
                        {
                                double dSecondary = Convert.ToDouble(dataSecondary[count]);
-                               int dSecondaryHeight = Util.DivideSafeAndGetInt(graphHeightSafe * dSecondary, 
maxAbsoluteSecondary * 1.0);
+                               int dSecondaryHeight = UtilAll.DivideSafeAndGetInt(graphHeightSafe * 
dSecondary, maxAbsoluteSecondary * 1.0);
                                int dSecondaryTop = dBottom - dSecondaryHeight;
                                Gdk.Point dSecondaryCurrentPoint = new Gdk.Point(Convert.ToInt32(dLeft + 
(dWidth /2)), dSecondaryTop);
                                //LogB.Information(string.Format("dSecondaryHeight: {0}; dSecondaryTop: {1}", 
dSecondaryHeight, dSecondaryTop));
diff --git a/src/gui/preferences.cs b/src/gui/preferences.cs
index fb3cc34a..7f0794aa 100644
--- a/src/gui/preferences.cs
+++ b/src/gui/preferences.cs
@@ -1634,9 +1634,9 @@ public class PreferencesWindow
                }
        
                pulsebarBackupActivity.Pulse();
-               pulsebarBackupDirs.Fraction = Util.DivideSafeFraction(uc.BackupMainDirsCount, 6); //6 for: 
database, encoder, forceSensor, logs, multimedia, raceAnalyzer
+               pulsebarBackupDirs.Fraction = UtilAll.DivideSafeFraction(uc.BackupMainDirsCount, 6); //6 for: 
database, encoder, forceSensor, logs, multimedia, raceAnalyzer
                pulsebarBackupDirs.Text = uc.LastMainDir;
-               pulsebarBackupSecondDirs.Fraction = Util.DivideSafeFraction(uc.BackupSecondDirsCount, 
uc.BackupSecondDirsLength);
+               pulsebarBackupSecondDirs.Fraction = UtilAll.DivideSafeFraction(uc.BackupSecondDirsCount, 
uc.BackupSecondDirsLength);
                pulsebarBackupSecondDirs.Text = uc.LastSecondDir;
 
                Thread.Sleep (30);
diff --git a/src/gui/webcam.cs b/src/gui/webcam.cs
index 34660ea9..6121cecf 100644
--- a/src/gui/webcam.cs
+++ b/src/gui/webcam.cs
@@ -216,7 +216,7 @@ public partial class ChronoJumpWindow
                        return false;
                }
 
-               pulsebar_webcam.Fraction = Util.DivideSafeFraction(swWebcamStart.Elapsed.TotalSeconds, 10);
+               pulsebar_webcam.Fraction = UtilAll.DivideSafeFraction(swWebcamStart.Elapsed.TotalSeconds, 10);
                Thread.Sleep (50);
                //LogB.Debug(webcamStartThread.ThreadState.ToString());
                return true;
@@ -382,7 +382,7 @@ public partial class ChronoJumpWindow
                        if(swWebcamStop.Elapsed.TotalSeconds < preferences.videoStopAfter)
                        {
                                //progressbar_video_generating.Pulse();
-                               progressbar_video_generating.Fraction = 
Util.DivideSafeFraction(swWebcamStop.Elapsed.TotalMilliseconds, preferences.videoStopAfter * 1000);
+                               progressbar_video_generating.Fraction = 
UtilAll.DivideSafeFraction(swWebcamStop.Elapsed.TotalMilliseconds, preferences.videoStopAfter * 1000);
                                return true;
                        }
 
diff --git a/src/json/compujump.cs b/src/json/compujump.cs
index 4d94b066..4a4755fb 100644
--- a/src/json/compujump.cs
+++ b/src/json/compujump.cs
@@ -978,9 +978,9 @@ public class UploadEncoderDataObject
                        if(compareTo > highest)
                                highest = compareTo;
 
-                       //LogB.Information(string.Format("Loss (con) of {0}; i: {1} is: {2}", by.ToString(), 
i++, Convert.ToInt32(Util.DivideSafe(100.0 * (highest - lowest), highest))));
+                       //LogB.Information(string.Format("Loss (con) of {0}; i: {1} is: {2}", by.ToString(), 
i++, Convert.ToInt32(UtilAll.DivideSafe(100.0 * (highest - lowest), highest))));
                }
-               return Convert.ToInt32(Util.DivideSafe(100.0 * (highest - lowest), highest));
+               return Convert.ToInt32(UtilAll.DivideSafe(100.0 * (highest - lowest), highest));
        }
 }
 
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index a24ab813..331d57cb 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -571,19 +571,19 @@ class Sqlite
 
        //for splashWin progressbars
        public static double PrintCreation() {
-               return Util.DivideSafeFraction(creationRate, creationTotal);
+               return UtilAll.DivideSafeFraction(creationRate, creationTotal);
        }
        public static double PrintConversionVersion() {
-               return Util.DivideSafeFraction(
+               return UtilAll.DivideSafeFraction(
                                Convert.ToDouble(Util.ChangeDecimalSeparator(currentVersion)), 
                                Convert.ToDouble(Util.ChangeDecimalSeparator(lastChronojumpDatabaseVersion))
                                );
        }
        public static double PrintConversionRate() {
-               return Util.DivideSafeFraction(conversionRate, conversionRateTotal);
+               return UtilAll.DivideSafeFraction(conversionRate, conversionRateTotal);
        }
        public static double PrintConversionSubRate() {
-               return Util.DivideSafeFraction(conversionSubRate, conversionSubRateTotal);
+               return UtilAll.DivideSafeFraction(conversionSubRate, conversionSubRateTotal);
        }
 
        public static bool ConvertToLastChronojumpDBVersion() {
diff --git a/src/util.cs b/src/util.cs
index 6a5790ad..16295c4b 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -2045,42 +2045,6 @@ public class Util
        }
 
 
-       //avoids divide by zero
-       //thought for being between 0, 1
-       //ideal for progressBars
-       public static double DivideSafeFraction (double num, double denom)
-       {
-               if(num == 0 || denom == 0)
-                       return 0;
-
-               double result = num / denom;
-               
-               if(result > 1)
-                       result = 1;
-               else if(result < 0)
-                       result = 0;
-
-               return result;
-       }
-       
-       public static double DivideSafeFraction (int val1, int val2) {
-               return DivideSafeFraction(Convert.ToDouble(val1), Convert.ToDouble(val2));
-       }
-
-       //Not restricted to values 0-1
-       public static double DivideSafe (double val1, double val2)
-       {
-               if(val1 == 0 || val2 == 0)
-                       return 0;
-
-               return val1 / val2;
-       }
-
-       public static int DivideSafeAndGetInt (double val1, double val2)
-       {
-               return Convert.ToInt32(DivideSafe(val1, val2));
-       }
-
 
        /*
        //converts all values to positive
diff --git a/src/utilDate.cs b/src/utilDate.cs
index c9aa7740..a20b346f 100644
--- a/src/utilDate.cs
+++ b/src/utilDate.cs
@@ -105,4 +105,9 @@ public class UtilDate
                return UtilAll.DigitsCreate(dt.Year,4) + "-" + Catalog.GetString(dt.ToString("MMMM"));
        }
 
+       public static double DateTimeYearDayAsDouble(DateTime dt)
+       {
+               return dt.Year + UtilAll.DivideSafe(dt.DayOfYear, 360);
+       }
+
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]