From b724dce434975ae1ca8b4c484f39c085c11f1627 Mon Sep 17 00:00:00 2001 From: SeWZC <36623158+SeWZC@users.noreply.github.com> Date: Fri, 10 Jan 2025 11:30:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=AE=E7=82=B9?= =?UTF-8?q?=E6=95=B0=E4=BA=94=E8=88=8D=E5=85=AD=E5=85=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FloatingPointNumberExtensions.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs b/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs index dc0dcce..14f4645 100644 --- a/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs +++ b/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs @@ -1,3 +1,5 @@ +using System.Runtime.Intrinsics.Arm; + namespace DotNetCampus.Numerics; /// @@ -84,6 +86,7 @@ public static int Round(this float value) /// 四舍五入到整数。 public static int RoundingHalfAwayFromZero(this double value) { + return (int)Math.Round(value, MidpointRounding.AwayFromZero); } @@ -97,6 +100,26 @@ public static int RoundingHalfAwayFromZero(this float value) return (int)MathF.Round(value, MidpointRounding.AwayFromZero); } + /// + /// 对浮点数进行五舍六入。 + /// + /// 要进行五舍六入的浮点数。 + /// 五舍六入到整数。 + public static int RoundingHalfToZero(this double value) + { + return (int)Math.Truncate(value + Math.CopySign(0.5, value)); + } + + /// + /// 对浮点数进行五舍六入。 + /// + /// 要进行五舍六入的浮点数。 + /// 五舍六入到整数。 + public static int RoundingHalfToZero(this float value) + { + return (int)MathF.Truncate(value + MathF.CopySign(0.5f, value)); + } + /// /// 对浮点数进行向下取整。 /// @@ -281,4 +304,4 @@ public static double Pow(this float value, double exponent) } #endregion -} \ No newline at end of file +} From 73beb72419a9f26271d27dba4a98fb3041179b43 Mon Sep 17 00:00:00 2001 From: SeWZC <36623158+SeWZC@users.noreply.github.com> Date: Fri, 10 Jan 2025 11:35:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8C=BA=E9=97=B4?= =?UTF-8?q?=E7=9A=84=20Clamp=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DotNetCampus.Numerics/FloatingPointHelper.cs | 19 +++++++++++++++++++ DotNetCampus.Numerics/Interval.cs | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/DotNetCampus.Numerics/FloatingPointHelper.cs b/DotNetCampus.Numerics/FloatingPointHelper.cs index 70ad5dd..81ebfcc 100644 --- a/DotNetCampus.Numerics/FloatingPointHelper.cs +++ b/DotNetCampus.Numerics/FloatingPointHelper.cs @@ -66,5 +66,24 @@ public static TNum Sqrt(this TNum a) throw new NotSupportedException(); } + public static TNum Clamp(this TNum value, TNum min, TNum max) + where TNum : unmanaged, IFloatingPoint + { + if (typeof(TNum) == typeof(float)) + return Unsafe.BitCast(Math.Clamp(Unsafe.BitCast(value), Unsafe.BitCast(min), Unsafe.BitCast(max))); + + if (typeof(TNum) == typeof(double)) + return Unsafe.BitCast(Math.Clamp(Unsafe.BitCast(value), Unsafe.BitCast(min), Unsafe.BitCast(max))); + + if (typeof(TNum) == typeof(NFloat)) + return Unsafe.BitCast(NFloat.Clamp(Unsafe.BitCast(value), Unsafe.BitCast(min), Unsafe.BitCast(max))); + + return value <= min + ? min + : value >= max + ? max + : value; + } + #endregion } diff --git a/DotNetCampus.Numerics/Interval.cs b/DotNetCampus.Numerics/Interval.cs index 2e4e53b..dcb5de3 100644 --- a/DotNetCampus.Numerics/Interval.cs +++ b/DotNetCampus.Numerics/Interval.cs @@ -67,6 +67,16 @@ public bool Contains(TNum value) return Start <= value && value <= End; } + /// + /// 将目标值限制在区间内。 + /// + /// 需要限制的值。 + /// 限制后的值。 + public TNum Clamp(ref TNum value) + { + return value.Clamp(Start, End); + } + /// public override string ToString() { From 82ec95808eccc9e6b2416ce9d4ca9dba622bb76e Mon Sep 17 00:00:00 2001 From: SeWZC <36623158+SeWZC@users.noreply.github.com> Date: Fri, 10 Jan 2025 11:59:16 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AC=A6=E5=8F=B7?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DotNetCampus.Numerics/FloatingPointNumberExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs b/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs index 14f4645..23eca56 100644 --- a/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs +++ b/DotNetCampus.Numerics/FloatingPointNumberExtensions.cs @@ -107,7 +107,7 @@ public static int RoundingHalfAwayFromZero(this float value) /// 五舍六入到整数。 public static int RoundingHalfToZero(this double value) { - return (int)Math.Truncate(value + Math.CopySign(0.5, value)); + return (int)Math.Truncate(value - Math.CopySign(0.5, value)); } /// @@ -117,7 +117,7 @@ public static int RoundingHalfToZero(this double value) /// 五舍六入到整数。 public static int RoundingHalfToZero(this float value) { - return (int)MathF.Truncate(value + MathF.CopySign(0.5f, value)); + return (int)MathF.Truncate(value - MathF.CopySign(0.5f, value)); } ///