diff --git a/src/Numerics/Precision.cs b/src/Numerics/Precision.cs index 718f4b3c..afb506bc 100644 --- a/src/Numerics/Precision.cs +++ b/src/Numerics/Precision.cs @@ -243,11 +243,31 @@ namespace MathNet.Numerics /// The next larger floating point value. public static double Increment(this double value) { - if (double.IsInfinity(value) || double.IsNaN(value)) + return Increment(value, 1); + } + + /// + /// Increments a floating point number to the next bigger number representable by the data type. + /// + /// The value which needs to be incremented. + /// How many times the number should be incremented. + /// + /// The incrementation step length depends on the provided value. + /// Increment(double.MaxValue) will return positive infinity. + /// + /// The next larger floating point value. + public static double Increment(this double value, int count) + { + if (double.IsInfinity(value) || double.IsNaN(value) || count == 0) { return value; } + if (count < 0) + { + Decrement(value, -count); + } + // Translate the bit pattern of the double to an integer. // Note that this leads to: // double > 0 --> long > 0, growing as the double value grows @@ -257,11 +277,11 @@ namespace MathNet.Numerics long intValue = GetLongFromDouble(value); if (intValue < 0) { - intValue--; + intValue -= count; } else { - intValue++; + intValue += count; } // Note that long.MinValue has the same bit pattern as -0.0. @@ -286,11 +306,31 @@ namespace MathNet.Numerics /// The next smaller floating point value. public static double Decrement(this double value) { - if (double.IsInfinity(value) || double.IsNaN(value)) + return Decrement(value, 1); + } + + /// + /// Decrements a floating point number to the next smaller number representable by the data type. + /// + /// The value which should be decremented. + /// How many times the number should be decremented. + /// + /// The decrementation step length depends on the provided value. + /// Decrement(double.MinValue) will return negative infinity. + /// + /// The next smaller floating point value. + public static double Decrement(this double value, int count) + { + if (double.IsInfinity(value) || double.IsNaN(value) || count == 0) { return value; } + if (count < 0) + { + Decrement(value, -count); + } + // Translate the bit pattern of the double to an integer. // Note that this leads to: // double > 0 --> long > 0, growing as the double value grows @@ -309,11 +349,11 @@ namespace MathNet.Numerics if (intValue < 0) { - intValue++; + intValue += count; } else { - intValue--; + intValue -= count; } // Note that not all long values can be translated into double values. There's a whole bunch of them