IFloatingPoint math functions #60524
Replies: 3 comments 2 replies
-
This is not a language concern. Moving to runtime. |
Beta Was this translation helpful? Give feedback.
-
The question is if it really needs to be called I fully agree with you that there needs to be a common solution because I have very often stumbled across the same problem and I don't know if this problem can be solved. You might need different return types, specific for what you want to have as a result, but I think that's currently out of scope for C# and will surely be a breaking change. The |
Beta Was this translation helpful? Give feedback.
-
Floating-point in .NET, and computers in general, tends to be IEEE 754 floating-point. The interface here represents that and so exposes the operations that are expected or recommended to exist by the spec. We could certainly look at separating certain functionality out slightly, but there are limits to the number of interfaces we should expose here and to how that would be exposed or usable in practical scenarios. At the end of the day, exposing a number type that meets users needs and expectations is non-trivial and always will be. Even just considering arithmetic operations there are a multitude of things to support, but types are also typically expected to implement contracts around equality, to provide parsing/formatting support, to expose "basic" functionality around more complex operations (like This is also not unique to .NET. This is how many popular languages that have direct support for various types of "number-contracts" expose that contract. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now almost all math functions are part of the
IFloatingPoint
interface. The problem with this is that it makes it very difficult to make generic math objects that require any math functions.For example, it is impossible to make a generic vector with a
Length
property that also allows for integers:I need
Sqrt
to calculate the length, but it is only available forIFloatingPoint
.Another issue of placing math functions in
IFloatingPoint
is that everything that wants to support these math functions has to be aIFloatingPoint
So if I want to makeVector2
supportCeiling
, that would mean thatVector2
is aIFloatingPoint
, which seems a bit odd.So I suggest two changes to fix these issues:
ITrigonometryMethods
,IRoundingMethods
,IExponentMethods
, etc. This allows anything to implement a subset of the math functions without having to becomeIFloatingPoint
. It also makes it so you don't have to implement every single math function when you just want to add aSqrt
to your object.INumber
types, even integers. Even if the result get rounded, and if some functions likeSin
andCos
don't return something that is actually very useful, it is still a lot better than having to drop all integer support in your class because you happen to callSqrt
in one function.Beta Was this translation helpful? Give feedback.
All reactions