Numeric Operator Overloading

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I am overloading the + operator (and several others that are used with
numeric types). Because there are many possible combinations of parameters
for this due to all the different numeric types, and the code will be the
same for almost all of them (single, double, decimal, short, integer, long,
etc.), I was wondering if there is a more efficient way to do the
overloading. I realize that I can reuse code, but I still need to list all
the combinations of types. Is there a way to sort of say "any numeric type"
rather than make a ton of overloads for each operator? Thanks.
 
R

Robert

Nathan Sokalski said:
I am overloading the + operator (and several others that are used with numeric types). Because there are many possible
combinations of parameters for this due to all the different numeric types, and the code will be the same for almost
all of them (single, double, decimal, short, integer, long, etc.), I was wondering if there is a more efficient way to
do the overloading. I realize that I can reuse code, but I still need to list all the combinations of types. Is there a
way to sort of say "any numeric type" rather than make a ton of overloads for each operator? Thanks.


You should look up the Widening and Narrowing keywords in the help.
 
N

Nathan Sokalski

I understand the concepts of Widening and Narrowing, but that does not make
it any easier for me to allow users of my class to use any numeric type with
operators such as + in my class. Am I misunderstanding something you are
trying to say? Thanks.
 
R

Robert

Nathan Sokalski said:
I understand the concepts of Widening and Narrowing, but that does not make it any easier for me to allow users of my
class to use any numeric type with operators such as + in my class. Am I misunderstanding something you are trying to
say? Thanks.
--


Well, If you are trying to add say a byte and an integer
z = i + b

then b should by widened to integer, allowing z to be computed.
I did not test this, it just seemed like it ought to work.
 
N

Nathan Sokalski

I understand how to write the code to overload an operator, and I also
understand the concept of widening/narrowing conversions. My problem is that
the class I have written in which I want to overload the operators (the
class is called LargeDecimal, and I want to overload the operators so I can
use it like the built-in numeric types) has many different combinations of
operands, meaning many different overloads for each operator. Think about
it, let's say you are overloading the division operator. There will be a ton
of combinations of the type you are overloading and the built-in types. For
example, there could be YourClass/YourClass, Double/YourClass, and
YourClass/Double, and this is only what is needed to do Double. Multiply
that by the number of numeric types and then by the number of operators you
want to overload, there is an awful lot of overloads. And in some cases, the
code will need to be different as well, but for most of mine I will simply
be converting the operands to my class and then using the code for that, as
follows:

Public Shared Operator -(ByVal num1 As LargeDecimal, ByVal num2 As
LargeDecimal) As LargeDecimal
'CODE TO CALCULATE RETURN VALUE
End Operator
Public Shared Operator -(ByVal num1 As Double, ByVal num2 As LargeDecimal)
As LargeDecimal
Return New LargeDecimal(num1)-num2
End Operator
Public Shared Operator -(ByVal num1 As LargeDecimal, ByVal num2 As Double)
As LargeDecimal
Return num1-New LargeDecimal(num2)
End Operator

Notice that the first overload takes two operands, both of which are of type
LargeDecimal (the name of my class). All the rest take one operand of type
LargeDecimal and one of another type, which I want to do for all the numeric
types. If you look at the code above, you will notice that to add each of
the numeric types that are not already added, the only word that will need
changed is the name of the type (change Double to Single, Long, Integer,
Decimal, Short, etc.). Is there any easier or more efficient way to do this
than copy the code I have above for every numeric type? Thanks.
 
R

Robert

Nathan Sokalski said:
I understand how to write the code to overload an operator, and I also understand the concept of widening/narrowing
conversions. My problem is that the class I have written in which I want to overload the operators (the class is called
LargeDecimal, and I want to overload the operators so I can use it like the built-in numeric types) has many different
combinations of operands, meaning many different overloads for each operator. Think about it, let's say you are
overloading the division operator. There will be a ton of combinations of the type you are overloading and the built-in
types. For example, there could be YourClass/YourClass, Double/YourClass, and YourClass/Double, and this is only what
is needed to do Double. Multiply that by the number of numeric types and then by the number of operators you want to
overload, there is an awful lot of overloads. And in some cases, the code will need to be different as well, but for
most of mine I will simply be converting the operands to my class and then using the code for that, as follows:

Right, I got all that the first pass. So widen one of the operators..
Instead of n^2 permutions, you get N.

Byte, Int16, Short, Integer, etc can all be passed as an integer with no
harmful effects. You can also pass all of those to a double.

Just define your operators with the largest possible types, and thinks will
widen automagically to work. You will get a result back of a larger datatype,
which the user can test, and cast back to whatever desired type as needed.

If the runtime chokes on overflow, then so be it. You can not fit most Integers
into bytes.. The user can cast thier operands manually and pick the proper overload.

You just can not add 2 bytes, and expect a byte back..
 
B

Branco Medeiros

Nathan said:
I understand how to write the code to overload an operator, and I also
understand the concept of widening/narrowing conversions. My problem is that
the class I have written in which I want to overload the operators (the
class is called LargeDecimal, and I want to overload the operators so I can
use it like the built-in numeric types) has many different combinations of
operands, meaning many different overloads for each operator. Think about
it, let's say you are overloading the division operator. There will be a ton
of combinations of the type you are overloading and the built-in types. For
example, there could be YourClass/YourClass, Double/YourClass, and
YourClass/Double, and this is only what is needed to do Double. Multiply
that by the number of numeric types and then by the number of operators you
want to overload, there is an awful lot of overloads.
<snip>

I may be wrong, but it seems to me you only need to define the
operators in the context of your class (e.g. Operator +(V1 As
LargeDecimal, V2 As LargeDecimal) As LargeDecimal) and provide
widening conversions from all the other types to your type (Widening
Operator CType(V As Integer) As LargeDecimal, Widening Operator CType
(V As SomeOtherType) As LargeDecimal, etc) and VB will take care of
the rest for you.

Hope this helps,

Branco.
 

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