the new Math.Generics ?
-
gentle reminder: this forum is/was a language discussion forum; i ain't complaining, by saying that, that i think it being, now, more of a C# QA for grown-ups is a "bad thang" :) the new .NET kid: [^] ... looks promising ... if (?) ... you are remembering, as i do, the yogic contortions necessary for numeric Type conversions, the many different facilities from reflection, to 'ChangeType, etc. and, the not=quite=-there weirdness necessary to try to constrain a generic parameter to a Numeric Type. Scott Hanselman waxed eloquent on this in 2003: [^]. Okay ... the question is: have you tried/evaluated Math.Generics ? And, what do you think ? from the somewhat sidelined fossil, bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
-
gentle reminder: this forum is/was a language discussion forum; i ain't complaining, by saying that, that i think it being, now, more of a C# QA for grown-ups is a "bad thang" :) the new .NET kid: [^] ... looks promising ... if (?) ... you are remembering, as i do, the yogic contortions necessary for numeric Type conversions, the many different facilities from reflection, to 'ChangeType, etc. and, the not=quite=-there weirdness necessary to try to constrain a generic parameter to a Numeric Type. Scott Hanselman waxed eloquent on this in 2003: [^]. Okay ... the question is: have you tried/evaluated Math.Generics ? And, what do you think ? from the somewhat sidelined fossil, bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
I hadn't really used it yet (which may indicate something as well) but I briefly tried it just now, so keep in mind I don't really know my way around the API yet: - Large repertoire of available operations, which applies to .NET 7 broadly, not only generic math. Nice operations such as rotates, popcount, leading zero count, are no longer hidden away in weird corners of the language (or worse: absent - as they used to be). - Despite shift counts no longer needing to be `int`, shifting by `T` is not allowed. So while working with `T`, shifting by constants is fine (since they'd be `int`), but as soon as you want to shift by some value that comes out of a generic calculation.. well you still end up with some weird conversions. This also applies to the `count` parameter of rotates. This has anti-synergy with the return types of `T.PopCount` and `T.TrailingZeroCount` and so on, which return `T`. I definitely would want to write `x >>> T.TrailingZeroCount(x)` at some point, but no. - There is an explicit unsigned right shift, but `>>` means "either signed or unsigned right shift depending on the type". What if I want an explicit signed right shift, even if `T` is an unsigned type? E: in non-generic code of course the answer is "just cast to the corresponding signed type, shift, then cast back". But then in generic code the question is, what type is that? See one of the next points for the problem with that. - There is no easy way to get the number of bits in `T`. There are ways, sure, but I was hoping for `T.BitSize` or something like that. Do I really need to write `T.PopCount(T.AllBitsSet)`? Bonus: the result of that is a `T` but you probably want an `int`. Or `x.GetByteCount() * 8` perhaps? (why do I need an instance `x` to find the byte count?) - There seems to be no way to express certain constraints on types such as "given some signed type `TS`, `TU` is the corresponding unsigned type of the same size". Well, I don't know how C# would do that. But I can do it in C++, and it would be useful. For example if I wanted to define a type-changing version of `Abs`, which takes a signed integer and returns an unsigned integer of the same size - as it is, I wouldn't be able to constrain the types to have the same size. As far as I know, anyway. - There are standard "low multiplications" that take two `T` and return the low half of the product as a `T`. Elsewhere in .NET classes, there are functions such as `Math.BigMul` that return the full product. Generic `T` has a lot of fancy operations defined for it, but not `B
-
gentle reminder: this forum is/was a language discussion forum; i ain't complaining, by saying that, that i think it being, now, more of a C# QA for grown-ups is a "bad thang" :) the new .NET kid: [^] ... looks promising ... if (?) ... you are remembering, as i do, the yogic contortions necessary for numeric Type conversions, the many different facilities from reflection, to 'ChangeType, etc. and, the not=quite=-there weirdness necessary to try to constrain a generic parameter to a Numeric Type. Scott Hanselman waxed eloquent on this in 2003: [^]. Okay ... the question is: have you tried/evaluated Math.Generics ? And, what do you think ? from the somewhat sidelined fossil, bill
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
BillWoodruff wrote:
And, what do you think ?
I think that if one has a lot of code that requires that one become familiar with that a lot then one of the following is true. 1. One is creating a mathematics library. Some advanced product, such as a matrix library 2. The code is wrong and someone is trying to be clever which will lead to increased maintenance costs.