Addition of two values

G

ggeshev

Hello!

I've got a method :
public static Object _Add(Object val1, Object val2); {}

val1 and val2 are among of these types :
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
- float
- double
- decimal

Types of val1 and val2 do not necessarily match (i.e. val1 may be int and
val2 may be decimal).
The method should implement the Addition operation (val1 + val2) and return
the result in the propper type (i.e. if an integer and double are added,
result should be "double"; if sbyte and sbyte are added, the result should
be sbyte or short - if the resulting value is beyond sbyte limits).

What would be the implementation of this method look like?
Propose me some ideas for implementing the Addition operation and returning
the result in a propper type.

Thank you!
 
J

Jon Skeet [C# MVP]

ggeshev said:
I've got a method :
public static Object _Add(Object val1, Object val2); {}

val1 and val2 are among of these types :
- sbyte
- byte
- short
- ushort
- int
- uint
- long
- ulong
- float
- double
- decimal

Types of val1 and val2 do not necessarily match (i.e. val1 may be int and
val2 may be decimal).
The method should implement the Addition operation (val1 + val2) and return
the result in the propper type (i.e. if an integer and double are added,
result should be "double"; if sbyte and sbyte are added, the result should
be sbyte or short - if the resulting value is beyond sbyte limits).

What would be the implementation of this method look like?
Propose me some ideas for implementing the Addition operation and returning
the result in a propper type.

There are various other constraints you should think about:

1) If you are passed two ints, should the result always be an int, or
could it overflow into a long?
2) If you are passed (int)1 and (int)1 could the result actually be
(byte)2?
3) What would you expect to happen with decimal + double where you may
not be able to preserve both scale and precision within one value?
4) How do you expect the caller to cope with all of this anyway?

Jon
 
G

ggeshev

1) If you are passed two ints, should the result always be an int, or
could it overflow into a long?
2) If you are passed (int)1 and (int)1 could the result actually be
(byte)2?
3) What would you expect to happen with decimal + double where you may
not be able to preserve both scale and precision within one value?
4) How do you expect the caller to cope with all of this anyway?

Jon

1) The result should be long
2) The result should be int
3) The result should be double
 
J

Jon Skeet [C# MVP]

ggeshev said:
1) The result should be long
2) The result should be int
3) The result should be double

Okay. Thinking about unsigned types as well, I think there are *lots*
of combinations you should consider.

I would start by writing a *long* set of unit tests - as many odd (and
normal!) combinations as you can think of. Each test should be
incredibly simple - just assert that the result of the addition is what
you want it to be.

Once you've got that set of unit tests, it'll be a lot easier to start
implementing the production code - you'll have thought of a lot of the
issues involved while writing the tests, and recorded the answers at
the same time. You'll probably want to start off the implementation by
working out whether you're dealing with integer arithmetic or floating
point, and possibly have one private method for each, just to keep
things clean.

Jon
 
G

ggeshev

Okay. Thinking about unsigned types as well, I think there are *lots*
of combinations you should consider.

I would start by writing a *long* set of unit tests - as many odd (and
normal!) combinations as you can think of. Each test should be
incredibly simple - just assert that the result of the addition is what
you want it to be.

Once you've got that set of unit tests, it'll be a lot easier to start
implementing the production code - you'll have thought of a lot of the
issues involved while writing the tests, and recorded the answers at
the same time. You'll probably want to start off the implementation by
working out whether you're dealing with integer arithmetic or floating
point, and possibly have one private method for each, just to keep
things clean.

Jon

Thank you Jon!
Thank you for your proposals!
I was wondering if I'm missing some very easy and tricky way of achieving my
aim by casting the input Object values /val1 and val2/ to certain Interfaces
for example, or using reflection probably.
 
J

Jon Skeet [C# MVP]

I was wondering if I'm missing some very easy and tricky way of achieving my
aim by casting the input Object values /val1 and val2/ to certain Interfaces
for example, or using reflection probably.

No, I don't believe so.
 

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