Questions tagged [.net-generic-math]

.NET 7 and C# 11 include innovations that allow you to perform mathematical operations generically — that is, without having to know the exact type you're working with.

.NET 7 and C# 11 include innovations that allow you to perform mathematical operations generically — that is, without having to know the exact type you're working with. For example, if you wanted to write a method that adds two numbers, previously you had to add an overload of the method for each type. Now you can write a single, generic method, where the type parameter is constrained to be a number-like type. For more information, see the Generic math article and the Generic math blog post.

17 questions
7
votes
1 answer

How to generate constant values with generic math

I have the following generic math function: private static T Fade(T t) where T : IFloatingPoint { return t * t * t * (t * (t * 6 - 15) + 10); } This doesn't compile however, since 6, 15 and 10 are not of type T. The best solution I…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
5
votes
1 answer

Generic math how to cast from INumber to a "concrete" type (int/double/etc)

Suppose we have a generic method which accepts INumber and returns int: // process val and return as int static int DoSomething(T val) where T : INumber => ... How can the T be casted to int.
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
5
votes
1 answer

C# 11 - What have we gained by adding IAdditionOperators to this method?

I just upgraded Visual Studio 2022 to .NET7, which includes C# 11. I am interested in trying out the new static abstract interface methods, and so followed the tutorial there. The article shows how to define Point and Translation records that use…
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
4
votes
4 answers

How to have a generic variable to INumber in .NET 7?

We can use the new INumber interface in .NET 7 to reference any numeric type, like the following: using System.Numerics; INumber myNumber = 72; INumber mySecondNumber = 93.63f; However, because of the type constraint in INumber,…
Jossean Yamil
  • 850
  • 2
  • 9
  • 22
3
votes
1 answer

Using Numeric literals/constants in Generic Math C# 11

I am working with the new Generic Math System in C# 11, which is cool, but I haven't found a way to use numeric literals or mathematical constants with them. The following example which calculates the circumference of a circle does not work. public…
2
votes
2 answers

Is it possible to combine .NET 7 generic math functions that have different constraints?

Consider the following methods: static T Divide(T dividend, T divisor) where T : INumber { return dividend / divisor; } static T DivideF(T dividend, T divisor) where T : IFloatingPoint { return T.Floor(dividend /…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
2
votes
1 answer

How can I do simple math with a generic INumber in .NET 7 / C#11

I like to program a histogram function using a class with a generic data type bound to an INumber in .NET 7/C# 11 as public class Histogram where TValue : INumber public List> CalcHistogram(List
Qrt
  • 389
  • 3
  • 16
2
votes
2 answers

How do I properly use INumber in .NET 7 in F#?

I was reading the following on .NET 7 and INumber: It gave an example of adding two INumber generic values, which I tried to replicate in F# to no success. let add<'T when 'T :> INumber<'T>> (left : 'T) (right: 'T) : 'T = left + right This…
ANTZY21
  • 65
  • 6
1
vote
1 answer

Resolving Operator '+' cannot be applied to operand of type 'T' and 'T'

I'm trying create a generic calculator class which can support arithmetic operations like add , sub & mul very basic. but I'm getting this below mentioned error. I'm getting a feel that I need to add generic constraint on 'T' to allow operation only…
sameer
  • 1,635
  • 3
  • 23
  • 35
1
vote
1 answer

Is it possible to define an operator that accepts a parameter of a generic numerical type different than the base class?

I want my container to do element-wise arithmetic, but I can't figure out how to make an operator definition accept type parameters. public static Stats operator + (Stats a, Stats b) where T2 :…
1
vote
1 answer

Designing generic types with constraints on generic math interfaces

Consider the following trivial example, which is a struct that represents the square of an ISignedNumber. public readonly struct Square where T : ISignedNumber { public Square(T value) { Value = value * value; } …
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
1
vote
3 answers

Int32.Min vs Int32.MinMagnitude

What is the difference between the static methods Int32.Min and Int32.MinMagnitude, that were introduced in .NET 7? Their signature and description is the same: // Compares two values to compute which is lesser. public static int Min (int x, int…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
1
vote
2 answers

Is this possible? Specify any generic type as long as the + operation is defined on it

I'm not sure if this is possible, but if it is then it would be useful. I am attempting to program in a class called Matrix. The intent is to be able to have matrices of various data types, such as integers, floats, doubles, etc. I now want to…
Robin
  • 373
  • 1
  • 3
  • 20
1
vote
1 answer

Adding operator support to interfaces (Preview Feature in .NET 6)

Just a warning, this question requires a preview of .NET 6 to be installed I'm trying to create an interface in C# that can allow the + operator similar to how it is implemented in Microsoft's INumber. Interfaces.cs using System; using…
Dan
  • 7,286
  • 6
  • 49
  • 114
0
votes
0 answers

How to obtain an "accurate", unscaled IEEE-754 floating point number in C#

I say "accurate" because IEEE-754 doesn't accurately represent decimal numbers, which seems to be the crux of the matter. All decimal numbers can be represented in scientific notation, as illustrated below. I have included a column for the unscaled…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
1
2