Browse Source

precision: multiple increment/decrement

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/36/head
Christoph Ruegg 17 years ago
parent
commit
0cae26eda7
  1. 52
      src/Numerics/Precision.cs

52
src/Numerics/Precision.cs

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

Loading…
Cancel
Save