12
4
(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.
1Yikes! Tell me you have a solution. – dmckee --- ex-moderator kitten – 2013-03-01T04:13:30.133
I'll come up with one eventually. It'll be ungolfed and act as a last-place example entry. – Joe Z. – 2013-03-01T04:22:41.753
4Hmmm...perhaps it would be better to define
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). – dmckee --- ex-moderator kitten – 2013-03-01T04:26:53.3971I guess that this a great (and challenging) practice even if ungolfed – John Dvorak – 2013-03-01T06:49:13.687
3This question could use documentation of how rounding is handled and a good test suite. – Peter Taylor – 2013-03-01T07:59:12.083
Do we need full IEEE-754 compliance, e.g. proper handling of INF, NAN, denormals, etc ? – Paul R – 2013-03-01T10:07:05.807
@dmckee: Noted. I'll change them to their fixed bit sizes. – Joe Z. – 2013-03-01T12:02:04.637
@PeterTaylor Rounding documentation is probably in IEEE 754 (I believe most computers will round half to even). As for a test suite, that's a good question. I'm not sure how I'd go about writing that. – Joe Z. – 2013-03-01T13:26:18.507
@PaulR Yes, if the C/C++ compiler will do the same. – Joe Z. – 2013-03-01T13:29:19.800
4I'm sure it is in the spec, but the real spec is going to cost a few hundred USD. There are probably descriptions which are available for free, but IMO the onus should be on the question-asker to include that kind of details (or at least a link to a site which is likely to still be around in a couple of years) within the question rather than on the answerers to go searching themselves for the necessary materials to know what the question actually wants. – Peter Taylor – 2013-03-01T13:48:09.300
Understood. In that case, you'll have to wait a while because I need to go looking for a spec. I've been relying on the Wikipedia article. – Joe Z. – 2013-03-01T13:52:14.027
I'm currently creating the reference implementation. It's harder than it looks :S – Joe Z. – 2013-03-05T21:59:54.327