Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加浮点数五舍六入方法;添加区间的 Clamp 方法 #15

Merged
merged 3 commits into from
Jan 10, 2025
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
19 changes: 19 additions & 0 deletions DotNetCampus.Numerics/FloatingPointHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,24 @@ public static TNum Sqrt<TNum>(this TNum a)
throw new NotSupportedException();
}

public static TNum Clamp<TNum>(this TNum value, TNum min, TNum max)
where TNum : unmanaged, IFloatingPoint<TNum>
{
if (typeof(TNum) == typeof(float))
return Unsafe.BitCast<float, TNum>(Math.Clamp(Unsafe.BitCast<TNum, float>(value), Unsafe.BitCast<TNum, float>(min), Unsafe.BitCast<TNum, float>(max)));

if (typeof(TNum) == typeof(double))
return Unsafe.BitCast<double, TNum>(Math.Clamp(Unsafe.BitCast<TNum, double>(value), Unsafe.BitCast<TNum, double>(min), Unsafe.BitCast<TNum, double>(max)));

if (typeof(TNum) == typeof(NFloat))
return Unsafe.BitCast<NFloat, TNum>(NFloat.Clamp(Unsafe.BitCast<TNum, NFloat>(value), Unsafe.BitCast<TNum, NFloat>(min), Unsafe.BitCast<TNum, NFloat>(max)));

return value <= min
? min
: value >= max
? max
: value;
}

#endregion
}
25 changes: 24 additions & 1 deletion DotNetCampus.Numerics/FloatingPointNumberExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.Intrinsics.Arm;

namespace DotNetCampus.Numerics;

/// <summary>
Expand Down Expand Up @@ -84,6 +86,7 @@ public static int Round(this float value)
/// <returns>四舍五入到整数。</returns>
public static int RoundingHalfAwayFromZero(this double value)
{

return (int)Math.Round(value, MidpointRounding.AwayFromZero);
}

Expand All @@ -97,6 +100,26 @@ public static int RoundingHalfAwayFromZero(this float value)
return (int)MathF.Round(value, MidpointRounding.AwayFromZero);
}

/// <summary>
/// 对浮点数进行五舍六入。
/// </summary>
/// <param name="value">要进行五舍六入的浮点数。</param>
/// <returns>五舍六入到整数。</returns>
public static int RoundingHalfToZero(this double value)
{
return (int)Math.Truncate(value - Math.CopySign(0.5, value));
}

/// <summary>
/// 对浮点数进行五舍六入。
/// </summary>
/// <param name="value">要进行五舍六入的浮点数。</param>
/// <returns>五舍六入到整数。</returns>
public static int RoundingHalfToZero(this float value)
{
return (int)MathF.Truncate(value - MathF.CopySign(0.5f, value));
}

/// <summary>
/// 对浮点数进行向下取整。
/// </summary>
Expand Down Expand Up @@ -281,4 +304,4 @@ public static double Pow(this float value, double exponent)
}

#endregion
}
}
10 changes: 10 additions & 0 deletions DotNetCampus.Numerics/Interval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public bool Contains(TNum value)
return Start <= value && value <= End;
}

/// <summary>
/// 将目标值限制在区间内。
/// </summary>
/// <param name="value">需要限制的值。</param>
/// <returns>限制后的值。</returns>
public TNum Clamp(ref TNum value)
{
return value.Clamp(Start, End);
}

/// <inheritdoc />
public override string ToString()
{
Expand Down
Loading