I am working on an embedded project I am trying to remove a virtual number class that has + / – * implemented. removing this class saves a lot of code space so I have replaced + with the following function,
if (BASE(h)->type() == FLOAT && BASE(v)->type() == FLOAT)
{
res = FLOAT(h)->floatValue() + FLOAT(v)->floatValue();
}
else if (BASE(h)->type() == INTEGER && BASE(v)->type() == INTEGER)
{
res = INTEGER(h)->intValue() + INTEGER(v)->intValue();
}
else if (BASE(h)->type() == INTEGER && BASE(v)->type() == FLOAT)
{
res = INTEGER(h)->floatValue() + FLOAT(v)->floatValue();
}
else
{
res = FLOAT(h)->floatValue() + INTEGER(v)->floatValue();
}
Is there a less uglier way to achieve this? cause I have to use the same scheme for other ops and comparison?
This actually branches on the type of
htwice, but only in the case that (you say in a comment elsewhere) is expensive anyway, the floating-point op. You could avoid that with agoto, but I’m not going to have that argument again. Something like:As with Howard’s answer, if
BASE()ortype()is expensive then you could calculate the answer for each argument once, even though it’s used twice.