4

.NET 7 Adds Generic Math

 1 year ago
source link: https://www.infoq.com/news/2022/11/net7-generic-math/?itm_source=infoq&itm_medium=popular_widget&itm_campaign=popular_content_list&itm_content=
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

.NET 7 Adds Generic Math

Nov 18, 2022 1 min read

In our last article we introduced generic parsing. This was a stepping stone to the real goal, generic math. Since .NET 2 was introduced in 2005, developers have been asking for an interface that supported basic arithmetical operations such addition and subtraction. This would allow them to build more complex mathematical libraries without the need to specify a particular type.

For example, one could write a function that calculated the standard deviation for a list of singles. Then that same function could be reused for doubles and halves instead of duplicating it.

It could also be used for a units of measure library. One could write new Length<int>(5) instead of having a different meter type for every underlying numeric type the caller may desire. To illustrate this, here is a theoretical division operator for length and duration.

public static Velocity<T> operator /(Length<T> length, Duration<T> duration)
{
    return new Velocity<T>(length.Value / duration.Value);
}

The actual list of generic mathematical interfaces is actually much more comprehensive than just addition and subtraction. Here is an example for double.

IAdditionOperators<double,double,double>
IAdditiveIdentity<double,double>
IBinaryFloatingPointIeee754<double>
IBinaryNumber<double>
IBitwiseOperators<double,double,double>
IComparisonOperators<double,double,bool>
IDecrementOperators<double>
IDivisionOperators<double,double,double>
IEqualityOperators<double,double,bool>
IExponentialFunctions<double>
IFloatingPoint<double>
IFloatingPointConstants<double>
IFloatingPointIeee754<double>
IHyperbolicFunctions<double>
IIncrementOperators<double>
ILogarithmicFunctions<double>
IMinMaxValue<double>
IModulusOperators<double,double,double>
IMultiplicativeIdentity<double,double>
IMultiplyOperators<double,double,double>
INumber<double>
INumberBase<double>
IPowerFunctions<double>
IRootFunctions<double>
ISignedNumber<double>
ISubtractionOperators<double,double,double>
ITrigonometricFunctions<double>
IUnaryNegationOperators<double,double>
IUnaryPlusOperators<double,double>

You can find the complete list in the System.Numerics namespace.

About the Author

Jonathan Allen

Jonathan Allen got his start working on MIS projects for a health clinic in the late 90's, bringing them up from Access and Excel to an enterprise solution by degrees. After spending five years writing automated trading systems for the financial sector, he became a consultant on a variety of projects including the UI for a robotic warehouse, the middle tier for cancer research software, and the big data needs of a major real estate insurance company. In his free time he enjoys studying and writing about martial arts from the 16th century.

Show more

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK