Ignore:
Timestamp:
10/09/14 11:15:59 (8 years ago)
Author:
mkommend
Message:

#2259: Changed ValueGenerator to work with decimals instead of doubles to achieve a higher accuracy and adapted all problem instance providers accordingly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/ValueGenerator.cs

    r11348 r11434  
    3131
    3232    /// <summary>
    33     /// Generates a sequence of evenly spaced points between start and end (inclusive!).
     33    /// Generates a sequence of evenly spaced points by returning the start value and adding the stepwidth until the end is reached or surpassed.
     34    ///
    3435    /// </summary>
    3536    /// <param name="start">The smallest and first value of the sequence.</param>
    3637    /// <param name="end">The largest and last value of the sequence.</param>
    3738    /// <param name="stepWidth">The step size between subsequent values.</param>
    38     /// <returns>An sequence of values from start to end (inclusive)</returns>
     39    /// <returns>A sequence of values from start to end (inclusive)</returns>
     40    [Obsolete("It is recommended to use the decimal overload to achieve a higher numerical accuracy.")]
    3941    public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth) {
    40       if (stepWidth.IsAlmost(0))
     42      //mkommend: IEnumerable.Cast fails due to boxing and unboxing of the involved types
     43      // http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#27bb217a6d5457ec
     44      // http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx     
     45
     46      return GenerateSteps((decimal)start, (decimal)end, (decimal)stepWidth).Select(x => (double)x);
     47    }
     48
     49    /// <summary>
     50    /// Generates a sequence of evenly spaced points by returning the start value and adding the stepwidth until the end is reached or surpassed.
     51    /// </summary>
     52    /// <param name="start">The smallest and first value of the sequence.</param>
     53    /// <param name="end">The largest and last value of the sequence.</param>
     54    /// <param name="stepWidth">The step size between subsequent values.</param>
     55    /// <returns>A sequence of values from start to end</returns>
     56    public static IEnumerable<decimal> GenerateSteps(decimal start, decimal end, decimal stepWidth) {
     57      if (stepWidth == 0)
    4158        throw new ArgumentException("The step width cannot be zero.");
    4259      if (start < end && stepWidth < 0)
     
    4461      if (start > end && stepWidth > 0)
    4562        throw new ArgumentException("The step width must be smaller than zero for decreasing sequences (start > end).");
    46       double x = start;
    47       // x<=end could skip the last value because of numerical problems
    48       while (x < end || x.IsAlmost(end)) {
     63
     64      decimal x = start;
     65      while (x <= end) {
    4966        yield return x;
    5067        x += stepWidth;
    5168      }
    52     }
    53 
    54     /// <summary>
    55     /// Generate a logarithmic sequence between start and end by applying a power-of-10 function to an underlying evenly spaced sequence
    56     /// </summary>
    57     /// <param name="start">The start of the sequence</param>
    58     /// <param name="end">The end of the sequence</param>
    59     /// <param name="stepWidth">The stepwidth for the original sequence before the points are transformed</param>
    60     /// <returns>A logarithmic sequence from start to end (inclusive)</returns>
    61     public static IEnumerable<double> GenerateLogarithmicSteps(double start, double end, double stepWidth) {
    62       return GenerateSteps(start, end, stepWidth, x => Math.Pow(10, x));
    6369    }
    6470
     
    7177    /// <param name="transform">The transform function</param>
    7278    /// <returns></returns>
     79    [Obsolete("It is recommended to use the decimal overload to achieve a higher numerical accuracy.")]
    7380    public static IEnumerable<double> GenerateSteps(double start, double end, double stepWidth, Func<double, double> transform) {
     81      return GenerateSteps(start,end, stepWidth).Select(transform);
     82    }
     83
     84    /// <summary>
     85    /// Generates a sequence of points between start and end according to given transformation
     86    /// </summary>
     87    /// <param name="start">The smallest and first value of the sequence.</param>
     88    /// <param name="end">The largest and last value of the sequence.</param>
     89    /// <param name="stepWidth">The step size between subsequent values (before transform)</param>
     90    /// <param name="transform">The transform function</param>
     91    /// <returns></returns>
     92    public static IEnumerable<decimal> GenerateSteps(decimal start, decimal end, decimal stepWidth, Func<decimal, decimal> transform) {
    7493      return GenerateSteps(start, end, stepWidth).Select(transform);
    7594    }
Note: See TracChangeset for help on using the changeset viewer.