Generics, System.ValueType and Complex Numbers

R

Ravi Shekhar

Hello,

So I'm doing some mathematical modeling and it turns out I need
distinct imaginary and complex types to carry out complex contour
integration.

I want to have it in all three precisions (float, double, and decimal),
and the code is highly redundant, but I can't seem to come up with a
clean way to use Generics in the code. For example, the following
doesn't work

public struct Imag<T> where T:System.ValueType
{
T imag;

public static Imag<T> operator +(Imag<T> a, Imag<T> b) { //implmentation}
public static Imag<T> operator -(Imag<T> a, Imag<T> b) { //implmentation}
public static T operator *(Imag<T> a, Imag<T> b) { //implmentation}
// and so on
}

Basically I need a way to make a Generic struct (can't be a class for
speed) that can only accept floats, doubles and decimals. Is this
possible in the current spec? I tried setting up an interface, but
those do not allow operators.

I really appreciate any help. Thanks.

Ravi
 
A

Andy

I don't think there's a way to do what you want, unless the where
clause can support an Or concept..
 
B

Bruce Wood

Ravi said:
Hello,

So I'm doing some mathematical modeling and it turns out I need
distinct imaginary and complex types to carry out complex contour
integration.

I want to have it in all three precisions (float, double, and decimal),
and the code is highly redundant, but I can't seem to come up with a
clean way to use Generics in the code. For example, the following
doesn't work

public struct Imag<T> where T:System.ValueType
{
T imag;

public static Imag<T> operator +(Imag<T> a, Imag<T> b) { //implmentation}
public static Imag<T> operator -(Imag<T> a, Imag<T> b) { //implmentation}
public static T operator *(Imag<T> a, Imag<T> b) { //implmentation}
// and so on
}

Basically I need a way to make a Generic struct (can't be a class for
speed) that can only accept floats, doubles and decimals. Is this
possible in the current spec? I tried setting up an interface, but
those do not allow operators.

I really appreciate any help. Thanks.

Well, all the generic is saving you is the trouble of writing three
separate classes. Really, all you're saving here is typing. There
really shouldn't be that much code; just type it out three times.

Generics are really intended for open-ended type creation. In other
words, if you're in the situation in which you say, "I need imaginary
numbers based on floats, doubles, and decimals, but later I might
create another value type and want an imaginary version of _that_,"
then you need a generic solution. If your set of possible type
parameters is closed, and only three, just write the code three times,
as ugly as that may seem, it's simple and easy to maintain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top