problem in defining a generics

  • Thread starter Thread starter m.a
  • Start date Start date
M

m.a

Hello,

I am developing a class which needs to do some mathematical processing. I
like to develop it in a way that it accept double or float or int based on
the user requirement so I defined it as a generic as follow:



public class MyClass<T>

{

T pi = (T)
3.1415926535897932384626433832795028841971693993751058209749445;

public MyClass()

{



}

}



But I am getting error:

Can not convert double to T.



I defined the class as follow but the error is the same:



public class MyClass<T>

{

T pi;

public MyClass()

{

pi = (T) 3.1415926535897932384626433832795028841971693993751058209749445



}

}



How can I do this?



Any help is very appreciated.



Regards
 
You can't easily. A literal is one thing or the other.

I'm not quite sure how useful an Int32 pi is, but perhaps just do the math
in double, and round (at the very end). Note also that double is not capable
of storing that level of detail. The constant System.Math.PI may be useful
to you.

Maths and generics don't play very well together. I have some solutions for
arithmetic etc on generics, but I think in this case the answer may be more
fundamental... and a simple approach will probably make life much easier.

Marc
 
Marc Gravell said:
You can't easily. A literal is one thing or the other.

I'm not quite sure how useful an Int32 pi is, but perhaps just do the math
in double, and round (at the very end). Note also that double is not
capable of storing that level of detail. The constant System.Math.PI may
be useful to you.

Maths and generics don't play very well together. I have some solutions
for arithmetic etc on generics, but I think in this case the answer may be
more fundamental... and a simple approach will probably make life much
easier.

Marc

Thanks

I want to develop some DSP classes and since it would run on .net micro
framework, I like to support fixed point arithmetic. I like to be able to
test the classes with double and float and also fixed point arithmetic.



What is the best approach for this? Since I may have several version of
fixed point, I thought I may be to use generics.



Regards
 
For simplicity and performance (in particular on micro) I would recommend
different implementations - i.e. a DspSingle, a DspDouble and a DspDecimal
(or similar) class. You might, however, be able to have them implement a
generic interface : IDsp<T> - that way you can at least forget about the
implementations after you have created them. You might also use a
pseudo-factory approach?

i.e. something akin to:

using System;
interface IDsp<T> {
void Foo(T bar);
}
class DspFloat : IDsp<float>
{
public void Foo(float bar) { }
}
class DspDecimal : IDsp<decimal>
{
public void Foo(decimal bar) { }
}
static class DspFactory {
public static IDsp<T> Create<T>()
{
Type type = typeof(T);
if (type == typeof(decimal)) return (IDsp<T>)new DspDecimal();
if (type == typeof(float)) return (IDsp<T>)new DspFloat();
throw new NotSupportedException("No implementation for " +
type.Name);
}
}
 
Back
Top