(I've tagged the question "C" for the time being, but if you're aware of another language that supports unions, you can also use that.)
Your task is to build the four standard mathematical operators + - * /
for the following struct:
union intfloat{
double f;
uint8_t h[8];
uint16_t i[4];
uint32_t j[2];
uint64_t k;
intfloat(double g){f = g;}
intfloat(){k = 0;}
}
such that the operations themselves only ever manipulate or access the integer part (so no comparing with the double anytime during the operation either), and the result is exactly the same (or functionally equivalent, in the case of non-numeric results such as NaN
) as if the corresponding mathematical operation had been applied straight to the double
instead.
You may choose which integer part to manipulate, perhaps even using different ones among different operators. (You can also choose to remove the "unsigned" from any of the fields in the union, although I'm not sure whether you'd want to do that.)
Your score is the sum of the length of code in characters for each of the four operators. Lowest score wins.
For those of us unfamiliar with the IEEE 754 specification, here is an article about it on Wikipedia.
Edits:
03-06 08:47 Added constructors to the intfloat struct. You are allowed to use them for testing, rather than manually setting the double / etc.
intstruct
in terms ofuint8_8
,uint16_t
and so on as the absolute sizes ofshort
,int
and so on are not defined by the standard (each type has a minimum size and there is a strict ordering in size, but that's it). \$\endgroup\$