.NET 7’deki yenilikler [9]: Genel matematik

Womanne

Member
.NET 7.0’daki Generic Math, temel sınıf ad alanında bazı arabirimler içerir System.NumericsBu, matematiksel işlemleri herhangi bir sayı türü için çalışacak şekilde uygulamanıza izin verir: herhangi bir bit uzunluğundaki tamsayı ve kesirli sayılar.


.NET 6.0’da deneysel olarak işaretlenen genel matematik işlemleri, örneğin INumber<T>, INumberBase<T>, IComparisonOperators<T, T>, IAdditionOperators<T, T, T>, IMultiplyOperators<T, T, T> VE ISubtractionOperators<T, T, T> .NET 7.0 ile üretim olgunluğuna ulaştı.


Sonraki liste, yöntemdeki genel bir matematiksel hesaplamanın iyi bir örneğini gösterir. Calc() ve bir diziden bir sayının genel olarak çıkarılması ParseNumber(). Diğer şeylerin yanı sıra, .NET 7.0’da tanıtılan sayı türü dahildir System.Int128 (tamsayı, 16 bayt).


using System.Globalization;
using System.Numerics;

namespace CS11;

public class CS11_GenericMath
{
T Calc<T>(T x, T y)
where T : INumber<T> // Neues Interface mit
// static abstract Members!
{
Console.WriteLine(
$"Calc {x.GetType().ToString()}/{y.GetType().ToString()}");
if (x == T.Zero || y <= T.Zero) return T.One;
return (x + y) * T.CreateChecked(42.24);
}

T ParseNumber<T>(string s)
where T : IParsable<T>
{
return T.Parse(s, CultureInfo.InvariantCulture);
}

public void Run()
{
CUI.H2("Calc mit 1 und 2");
Console.WriteLine($"Ergebnis mit System.Byte:
{Calc((byte)1, (byte)2)}"); // 126
Console.WriteLine($"Ergebnis mit System.Int32:
{Calc(1, 2)}"); // 126
Console.WriteLine($"Ergebnis mit System.Int128:
{Calc( (Int128)1, (Int128)2)}"); // 126
Console.WriteLine($"Ergebnis mit System.Single:
{Calc((Single)1.0, (Single)2.0)}"); // 126,72
Console.WriteLine($"Ergebnis mit System.Double:
{Calc(1.0d, 2.0d)}"); // 126,72
Console.WriteLine($"Ergebnis mit System.Decimal:
{Calc(1.0m, 2.0m)}"); // 126,720
Console.WriteLine($"Ergebnis mit System.Half:
{Calc((Half)1.0m, (Half)2.0m)}"); // 126,75

CUI.H2("ParseNumber 1.00 und 2.00");
var x = ParseNumber<float>("1.00");
var y = ParseNumber<float>("2.00");

Console.WriteLine($"Ergebnis mit System.Single:
{Calc(x, y)}"); // 3,6000001
Console.WriteLine($"Ergebnis mit System.Int32:
{Calc(0, 1)}"); // 1
}
}



C# programlama dilinin buradaki katkısı, C# 10.0’dan beri deneysel olarak mümkün olan ve C# 11.0’dan beri resmi olarak dil sözdiziminin bir parçası olan arayüzlerde statik soyut üyeleri tanımlama yeteneğidir. Microsoft, bu değiştiriciyi aşağıdakiler gibi temel sınıflarda kullanır: INumberBase<T>.


public interface INumberBase<TSelf>
: IAdditionOperators<TSelf, TSelf, TSelf>,
IAdditiveIdentity<TSelf, TSelf>,
IDecrementOperators<TSelf>,
IDivisionOperators<TSelf, TSelf, TSelf>,
IEquatable<TSelf>,
IEqualityOperators<TSelf, TSelf, bool>,
IIncrementOperators<TSelf>,
IMultiplicativeIdentity<TSelf, TSelf>,
IMultiplyOperators<TSelf, TSelf, TSelf>,
ISpanFormattable,
ISpanParsable<TSelf>,
ISubtractionOperators<TSelf, TSelf, TSelf>,
IUnaryPlusOperators<TSelf, TSelf>,
IUnaryNegationOperators<TSelf, TSelf>
where TSelf : INumberBase<TSelf>?
{
/// <summary>Gets the value <c>1</c> for the type.</summary>
static abstract TSelf One { get; }

/// <summary>Gets the value <c>0</c> for the type.</summary>
static abstract TSelf Zero { get; }


/// <summary>Tries to parses a string into a value.</summary>
static abstract bool TryParse([NotNullWhen(true)] string? s,
NumberStyles style,
IFormatProvider? provider,
out TSelf result);
}




(rm)



Haberin Sonu
 
Üst