I am creating a calculator like project which requires performing operations +, -, *, /, %, ^
on variables. Standard operations are of type var1 OP var2
. I have the type of both the variables at runtime, which can either be Int64
or double
.
I can convert both of them to double and perform calculations, but I'd rather prefer doing integer-based calculations when both variables are integers (otherwise convert both to double). This is what I have got so far (for addition)
if (container1.vType == Interop.variableType.DOUBLE || container2.vType == Interop.variableType.DOUBLE)
return Convert.ToDouble(container1.value) + Convert.ToDouble(container2.value);
return (Int64)container1.value + (Int64)container2.value;
However, due to multiple operations, using same/similar code over and over will create unnecessary redundancy (I'll simply have to change the operator).
So how should I do this so that it has both high performance and minimal/no redundancy?