473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generics, System.ValueType and Complex Numbers

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<Twhere T:System.ValueType
{
T imag;

public static Imag<Toperator +(Imag<Ta, Imag<Tb) { //implmentation}
public static Imag<Toperator -(Imag<Ta, Imag<Tb) { //implmentation}
public static T operator *(Imag<Ta, Imag<Tb) { //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

Jan 19 '07 #1
2 1794
I don't think there's a way to do what you want, unless the where
clause can support an Or concept..
Ravi Shekhar wrote:
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<Twhere T:System.ValueType
{
T imag;

public static Imag<Toperator +(Imag<Ta, Imag<Tb) { //implmentation}
public static Imag<Toperator -(Imag<Ta, Imag<Tb) { //implmentation}
public static T operator *(Imag<Ta, Imag<Tb) { //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
Jan 19 '07 #2

Ravi Shekhar wrote:
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<Twhere T:System.ValueType
{
T imag;

public static Imag<Toperator +(Imag<Ta, Imag<Tb) { //implmentation}
public static Imag<Toperator -(Imag<Ta, Imag<Tb) { //implmentation}
public static T operator *(Imag<Ta, Imag<Tb) { //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.

Jan 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
5
by: Leon Mayne | last post by:
We currently have lots of checks in our businesslayer object's Load() functions that look like: If drCourse("txtTTMCnotes").Equals(DBNull.Value) Then Me._txtTTMCnotes = "" Else...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.