Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,9 @@ bool IFloatingPoint<double>.TryWriteSignificandLittleEndian(Span<byte> destinati
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ILogB(TSelf)" />
public static int ILogB(double x) => Math.ILogB(x);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Lerp(TSelf, TSelf, TSelf)" />
public static double Lerp(double value1, double value2, double amount) => (value1 * (1.0 - amount)) + (value2 * amount);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalEstimate(TSelf)" />
public static double ReciprocalEstimate(double x) => Math.ReciprocalEstimate(x);

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Half.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,9 @@ public static Half BitIncrement(Half x)
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ILogB(TSelf)" />
public static int ILogB(Half x) => MathF.ILogB((float)x);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Lerp(TSelf, TSelf, TSelf)" />
public static Half Lerp(Half value1, Half value2, Half amount) => (Half)float.Lerp((float)value1, (float)value2, (float)amount);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalEstimate(TSelf)" />
public static Half ReciprocalEstimate(Half x) => (Half)MathF.ReciprocalEstimate((float)x);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public interface IFloatingPointIeee754<TSelf>
/// <returns>The integer logarithm of <paramref name="x" />.</returns>
static abstract int ILogB(TSelf x);

/// <summary>Performs a linear interpolation between two values based on the given weight.</summary>
/// <param name="value1">The first value, which is intended to be the lower bound.</param>
/// <param name="value2">The second value, which is intended to be the upper bound.</param>
/// <param name="amount">A value, intended to be between 0 and 1, that indicates the weight of the interpolation.</param>
/// <returns>The interpolated value.</returns>
/// <remarks>This method presumes inputs are well formed and does not validate that <c>value1 &lt; value2</c> nor that <c>0 &lt;= amount &lt;= 1</c>.</remarks>
static virtual TSelf Lerp(TSelf value1, TSelf value2, TSelf amount) => (value1 * (TSelf.One - amount)) + (value2 * amount);

/// <summary>Computes an estimate of the reciprocal of a value.</summary>
/// <param name="x">The value whose estimate of the reciprocal is to be computed.</param>
/// <returns>An estimate of the reciprocal of <paramref name="x" />.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,9 @@ bool IFloatingPoint<NFloat>.TryWriteSignificandLittleEndian(Span<byte> destinati
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ILogB(TSelf)" />
public static int ILogB(NFloat x) => NativeType.ILogB(x._value);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Lerp(TSelf, TSelf, TSelf)" />
public static NFloat Lerp(NFloat value1, NFloat value2, NFloat amount) => new NFloat(NativeType.Lerp(value1._value, value2._value, amount._value));

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalEstimate(TSelf)" />
public static NFloat ReciprocalEstimate(NFloat x) => new NFloat(NativeType.ReciprocalEstimate(x._value));

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@ bool IFloatingPoint<float>.TryWriteSignificandLittleEndian(Span<byte> destinatio
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ILogB(TSelf)" />
public static int ILogB(float x) => MathF.ILogB(x);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Lerp(TSelf, TSelf, TSelf)" />
public static float Lerp(float value1, float value2, float amount) => (value1 * (1.0f - amount)) + (value2 * amount);

/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalEstimate(TSelf)" />
public static float ReciprocalEstimate(float x) => MathF.ReciprocalEstimate(x);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ public static void Free(void* ptr) { }
public static bool IsPow2(System.Runtime.InteropServices.NFloat value) { throw null; }
public static bool IsRealNumber(System.Runtime.InteropServices.NFloat value) { throw null; }
public static bool IsSubnormal(System.Runtime.InteropServices.NFloat value) { throw null; }
public static System.Runtime.InteropServices.NFloat Lerp(System.Runtime.InteropServices.NFloat value1, System.Runtime.InteropServices.NFloat value2, System.Runtime.InteropServices.NFloat amount) { throw null; }
public static System.Runtime.InteropServices.NFloat Log(System.Runtime.InteropServices.NFloat x) { throw null; }
public static System.Runtime.InteropServices.NFloat Log(System.Runtime.InteropServices.NFloat x, System.Runtime.InteropServices.NFloat newBase) { throw null; }
public static System.Runtime.InteropServices.NFloat Log10(System.Runtime.InteropServices.NFloat x) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2469,5 +2469,81 @@ public static void TanPiTest64(double value, double expectedResult, double allow
AssertExtensions.Equal(-expectedResult, NFloat.TanPi((NFloat)(-value)), allowedVariance);
AssertExtensions.Equal(+expectedResult, NFloat.TanPi((NFloat)(+value)), allowedVariance);
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.Is32BitProcess))]
[InlineData(float.NegativeInfinity, float.NegativeInfinity, 0.5f, float.NegativeInfinity)]
[InlineData(float.NegativeInfinity, float.NaN, 0.5f, float.NaN)]
[InlineData(float.NegativeInfinity, float.PositiveInfinity, 0.5f, float.NaN)]
[InlineData(float.NegativeInfinity, 0.0f, 0.5f, float.NegativeInfinity)]
[InlineData(float.NegativeInfinity, 1.0f, 0.5f, float.NegativeInfinity)]
[InlineData(float.NaN, float.NegativeInfinity, 0.5f, float.NaN)]
[InlineData(float.NaN, float.NaN, 0.5f, float.NaN)]
[InlineData(float.NaN, float.PositiveInfinity, 0.5f, float.NaN)]
[InlineData(float.NaN, 0.0f, 0.5f, float.NaN)]
[InlineData(float.NaN, 1.0f, 0.5f, float.NaN)]
[InlineData(float.PositiveInfinity, float.NegativeInfinity, 0.5f, float.NaN)]
[InlineData(float.PositiveInfinity, float.NaN, 0.5f, float.NaN)]
[InlineData(float.PositiveInfinity, float.PositiveInfinity, 0.5f, float.PositiveInfinity)]
[InlineData(float.PositiveInfinity, 0.0f, 0.5f, float.PositiveInfinity)]
[InlineData(float.PositiveInfinity, 1.0f, 0.5f, float.PositiveInfinity)]
[InlineData(1.0f, 3.0f, 0.0f, 1.0f)]
[InlineData(1.0f, 3.0f, 0.5f, 2.0f)]
[InlineData(1.0f, 3.0f, 1.0f, 3.0f)]
[InlineData(1.0f, 3.0f, 2.0f, 5.0f)]
[InlineData(2.0f, 4.0f, 0.0f, 2.0f)]
[InlineData(2.0f, 4.0f, 0.5f, 3.0f)]
[InlineData(2.0f, 4.0f, 1.0f, 4.0f)]
[InlineData(2.0f, 4.0f, 2.0f, 6.0f)]
[InlineData(3.0f, 1.0f, 0.0f, 3.0f)]
[InlineData(3.0f, 1.0f, 0.5f, 2.0f)]
[InlineData(3.0f, 1.0f, 1.0f, 1.0f)]
[InlineData(3.0f, 1.0f, 2.0f, -1.0f)]
[InlineData(4.0f, 2.0f, 0.0f, 4.0f)]
[InlineData(4.0f, 2.0f, 0.5f, 3.0f)]
[InlineData(4.0f, 2.0f, 1.0f, 2.0f)]
[InlineData(4.0f, 2.0f, 2.0f, 0.0f)]
public static void LerpTest32(float value1, float value2, float amount, float expectedResult)
{
AssertExtensions.Equal(+expectedResult, (float)NFloat.Lerp(+value1, +value2, amount), 0);
AssertExtensions.Equal((expectedResult == 0.0f) ? expectedResult : -expectedResult, (float)NFloat.Lerp(-value1, -value2, amount), 0);
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]
[InlineData(double.NegativeInfinity, double.NegativeInfinity, 0.5, double.NegativeInfinity)]
[InlineData(double.NegativeInfinity, double.NaN, 0.5, double.NaN)]
[InlineData(double.NegativeInfinity, double.PositiveInfinity, 0.5, double.NaN)]
[InlineData(double.NegativeInfinity, 0.0, 0.5, double.NegativeInfinity)]
[InlineData(double.NegativeInfinity, 1.0, 0.5, double.NegativeInfinity)]
[InlineData(double.NaN, double.NegativeInfinity, 0.5, double.NaN)]
[InlineData(double.NaN, double.NaN, 0.5, double.NaN)]
[InlineData(double.NaN, double.PositiveInfinity, 0.5, double.NaN)]
[InlineData(double.NaN, 0.0, 0.5, double.NaN)]
[InlineData(double.NaN, 1.0, 0.5, double.NaN)]
[InlineData(double.PositiveInfinity, double.NegativeInfinity, 0.5, double.NaN)]
[InlineData(double.PositiveInfinity, double.NaN, 0.5, double.NaN)]
[InlineData(double.PositiveInfinity, double.PositiveInfinity, 0.5, double.PositiveInfinity)]
[InlineData(double.PositiveInfinity, 0.0, 0.5, double.PositiveInfinity)]
[InlineData(double.PositiveInfinity, 1.0, 0.5, double.PositiveInfinity)]
[InlineData(1.0, 3.0, 0.0, 1.0)]
[InlineData(1.0, 3.0, 0.5, 2.0)]
[InlineData(1.0, 3.0, 1.0, 3.0)]
[InlineData(1.0, 3.0, 2.0, 5.0)]
[InlineData(2.0, 4.0, 0.0, 2.0)]
[InlineData(2.0, 4.0, 0.5, 3.0)]
[InlineData(2.0, 4.0, 1.0, 4.0)]
[InlineData(2.0, 4.0, 2.0, 6.0)]
[InlineData(3.0, 1.0, 0.0, 3.0)]
[InlineData(3.0, 1.0, 0.5, 2.0)]
[InlineData(3.0, 1.0, 1.0, 1.0)]
[InlineData(3.0, 1.0, 2.0, -1.0)]
[InlineData(4.0, 2.0, 0.0, 4.0)]
[InlineData(4.0, 2.0, 0.5, 3.0)]
[InlineData(4.0, 2.0, 1.0, 2.0)]
[InlineData(4.0, 2.0, 2.0, 0.0)]
public static void LerpTest64(double value1, double value2, double amount, double expectedResult)
{
AssertExtensions.Equal(+expectedResult, NFloat.Lerp((NFloat)(+value1), (NFloat)(+value2), (NFloat)(amount)), 0);
AssertExtensions.Equal((expectedResult == 0.0) ? expectedResult : -expectedResult, NFloat.Lerp((NFloat)(-value1), (NFloat)(-value2), (NFloat)(amount)), 0);
}
}
}
4 changes: 4 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,7 @@ public DivideByZeroException(string? message, System.Exception? innerException)
public static bool IsPow2(double value) { throw null; }
public static bool IsRealNumber(double value) { throw null; }
public static bool IsSubnormal(double d) { throw null; }
public static double Lerp(double value1, double value2, double amount) { throw null; }
public static double Log(double x) { throw null; }
public static double Log(double x, double newBase) { throw null; }
public static double Log10(double x) { throw null; }
Expand Down Expand Up @@ -2801,6 +2802,7 @@ public enum GCNotificationStatus
public static bool IsPow2(System.Half value) { throw null; }
public static bool IsRealNumber(System.Half value) { throw null; }
public static bool IsSubnormal(System.Half value) { throw null; }
public static System.Half Lerp(System.Half value1, System.Half value2, System.Half amount) { throw null; }
public static System.Half Log(System.Half x) { throw null; }
public static System.Half Log(System.Half x, System.Half newBase) { throw null; }
public static System.Half Log10(System.Half x) { throw null; }
Expand Down Expand Up @@ -4809,6 +4811,7 @@ public SerializableAttribute() { }
public static bool IsPow2(float value) { throw null; }
public static bool IsRealNumber(float value) { throw null; }
public static bool IsSubnormal(float f) { throw null; }
public static float Lerp(float value1, float value2, float amount) { throw null; }
public static float Log(float x) { throw null; }
public static float Log(float x, float newBase) { throw null; }
public static float Log10(float x) { throw null; }
Expand Down Expand Up @@ -10383,6 +10386,7 @@ public partial interface IFloatingPointIeee754<TSelf> : System.IComparable, Syst
static abstract TSelf FusedMultiplyAdd(TSelf left, TSelf right, TSelf addend);
static abstract TSelf Ieee754Remainder(TSelf left, TSelf right);
static abstract int ILogB(TSelf x);
static virtual TSelf Lerp(TSelf value1, TSelf value2, TSelf amount) { throw null; }
static virtual TSelf ReciprocalEstimate(TSelf x) { throw null; }
static virtual TSelf ReciprocalSqrtEstimate(TSelf x) { throw null; }
static abstract TSelf ScaleB(TSelf x, int n);
Expand Down
38 changes: 38 additions & 0 deletions src/libraries/System.Runtime/tests/System/DoubleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,5 +1617,43 @@ public static void TanPiTest(double value, double expectedResult, double allowed
AssertExtensions.Equal(-expectedResult, double.TanPi(-value), allowedVariance);
AssertExtensions.Equal(+expectedResult, double.TanPi(+value), allowedVariance);
}

[Theory]
[InlineData(double.NegativeInfinity, double.NegativeInfinity, 0.5, double.NegativeInfinity)]
[InlineData(double.NegativeInfinity, double.NaN, 0.5, double.NaN)]
[InlineData(double.NegativeInfinity, double.PositiveInfinity, 0.5, double.NaN)]
[InlineData(double.NegativeInfinity, 0.0, 0.5, double.NegativeInfinity)]
[InlineData(double.NegativeInfinity, 1.0, 0.5, double.NegativeInfinity)]
[InlineData(double.NaN, double.NegativeInfinity, 0.5, double.NaN)]
[InlineData(double.NaN, double.NaN, 0.5, double.NaN)]
[InlineData(double.NaN, double.PositiveInfinity, 0.5, double.NaN)]
[InlineData(double.NaN, 0.0, 0.5, double.NaN)]
[InlineData(double.NaN, 1.0, 0.5, double.NaN)]
[InlineData(double.PositiveInfinity, double.NegativeInfinity, 0.5, double.NaN)]
[InlineData(double.PositiveInfinity, double.NaN, 0.5, double.NaN)]
[InlineData(double.PositiveInfinity, double.PositiveInfinity, 0.5, double.PositiveInfinity)]
[InlineData(double.PositiveInfinity, 0.0, 0.5, double.PositiveInfinity)]
[InlineData(double.PositiveInfinity, 1.0, 0.5, double.PositiveInfinity)]
[InlineData(1.0, 3.0, 0.0, 1.0)]
[InlineData(1.0, 3.0, 0.5, 2.0)]
[InlineData(1.0, 3.0, 1.0, 3.0)]
[InlineData(1.0, 3.0, 2.0, 5.0)]
[InlineData(2.0, 4.0, 0.0, 2.0)]
[InlineData(2.0, 4.0, 0.5, 3.0)]
[InlineData(2.0, 4.0, 1.0, 4.0)]
[InlineData(2.0, 4.0, 2.0, 6.0)]
[InlineData(3.0, 1.0, 0.0, 3.0)]
[InlineData(3.0, 1.0, 0.5, 2.0)]
[InlineData(3.0, 1.0, 1.0, 1.0)]
[InlineData(3.0, 1.0, 2.0, -1.0)]
[InlineData(4.0, 2.0, 0.0, 4.0)]
[InlineData(4.0, 2.0, 0.5, 3.0)]
[InlineData(4.0, 2.0, 1.0, 2.0)]
[InlineData(4.0, 2.0, 2.0, 0.0)]
public static void LerpTest(double value1, double value2, double amount, double expectedResult)
{
AssertExtensions.Equal(+expectedResult, double.Lerp(+value1, +value2, amount), 0);
AssertExtensions.Equal((expectedResult == 0.0) ? expectedResult : -expectedResult, double.Lerp(-value1, -value2, amount), 0);
}
}
}
Loading