Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12293


Ignore:
Timestamp:
04/07/15 15:38:56 (10 years ago)
Author:
pfleck
Message:

#2301 Use decimal instead of double for the DefineArithmeticProgressionDialog and in the CreateExperimentDialog.
Note, when using decimal only during the generate process but not during some calculations (e.g. determining meaningful step sizes), numeric inaccuracies occur.
Therefore some calculations now uses decimal instead of double values.

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Dialogs/DefineArithmeticProgressionDialog.cs

    r12012 r12293  
    2323using System.ComponentModel;
    2424using System.Globalization;
    25 using System.Linq;
    2625using System.Windows.Forms;
    2726using HeuristicLab.Common;
     
    3130    private bool allowOnlyInteger;
    3231
    33     public double Minimum { get; private set; }
    34     public double Maximum { get; private set; }
    35     public double Step { get; private set; }
     32    public decimal Minimum { get; private set; }
     33    public decimal Maximum { get; private set; }
     34    public decimal Step { get; private set; }
    3635
    37     public IEnumerable<double> Values {
    38       get { return EnumerateProgression().Reverse(); }
     36    public IEnumerable<decimal> Values {
     37      get { return SequenceGenerator.GenerateSteps(Minimum, Maximum, Step, includeEnd: true); }
    3938    }
    4039
     
    4847      this.allowOnlyInteger = allowOnlyInteger;
    4948    }
    50     public DefineArithmeticProgressionDialog(bool allowOnlyInteger, double minimum, double maximum, double step)
     49    public DefineArithmeticProgressionDialog(bool allowOnlyInteger, decimal minimum, decimal maximum, decimal step)
    5150      : this(allowOnlyInteger) {
    5251      Minimum = minimum;
     
    6362    private void textBox_Validating(object sender, CancelEventArgs e) {
    6463      var textBox = (TextBox)sender;
    65       double value = 0;
     64      decimal value = 0;
    6665      if (allowOnlyInteger) {
    6766        int intValue;
     
    7574        }
    7675      } else {
    77         if (!double.TryParse(textBox.Text, NumberStyles.Float, CultureInfo.CurrentCulture.NumberFormat, out value)) {
     76        if (!decimal.TryParse(textBox.Text, NumberStyles.Float, CultureInfo.CurrentCulture.NumberFormat, out value)) {
    7877          errorProvider.SetError(textBox, "Please enter a valid double value.");
    7978          e.Cancel = true;
     
    9089      return Minimum <= Maximum && Step >= 0;
    9190    }
    92 
    93     private IEnumerable<double> EnumerateProgression() {
    94       double value = Maximum;
    95       bool minimumIncluded = false;
    96       int i = 1;
    97       while (value >= Minimum) {
    98         if (value.IsAlmost(Minimum)) {
    99           yield return Minimum;
    100           minimumIncluded = true;
    101         } else yield return value;
    102 
    103         if (Step == 0) break; // a step size of 0 will only output maximum and minimum
    104         if (allowOnlyInteger) {
    105           value = (int)Maximum - i * (int)Step;
    106         } else {
    107           value = Maximum - i * Step;
    108         }
    109         i++;
    110       }
    111       if (!minimumIncluded) {
    112         yield return Minimum;
    113       }
    114     }
    11591  }
    11692}
  • trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.cs

    r12012 r12293  
    264264      var parameter = (IValueParameter)generateButton.Tag;
    265265      bool integerOnly = intParameters.ContainsKey(parameter);
    266       double min = 0, max = 1, step = 1;
     266      decimal min = 0, max = 1, step = 1;
    267267      #region Try to calculate some meaningful values
    268268      if (integerOnly) {
     
    276276        int len = doubleParameters[parameter].Length;
    277277        if (len > 0) {
    278           min = doubleParameters[parameter].Min();
    279           max = doubleParameters[parameter].Max();
    280           step = len >= 2 ? Math.Abs((doubleParameters[parameter][len - 1] - doubleParameters[parameter][len - 2])) : 1;
     278          min = (decimal)doubleParameters[parameter].Min();
     279          max = (decimal)doubleParameters[parameter].Max();
     280          step = len >= 2 ? Math.Abs(((decimal)doubleParameters[parameter][len - 1] - (decimal)doubleParameters[parameter][len - 2])) : 1m;
    281281        }
    282282      }
     
    292292          } else {
    293293            doubleParameters[parameter].Reset -= new EventHandler(ValuesArray_Reset);
    294             doubleParameters[parameter] = new DoubleArray(values.ToArray());
     294            doubleParameters[parameter] = new DoubleArray(values.Select(x => (double)x).ToArray());
    295295            doubleParameters[parameter].Reset += new EventHandler(ValuesArray_Reset);
    296296            stringConvertibleArrayView.Content = doubleParameters[parameter];
Note: See TracChangeset for help on using the changeset viewer.