6
Format
The format is a 64 bit value x representing a IEEE-754 double-precision binary floating-point number (a.k.a "a double"). x is assumed to represent a real number (not a NaN or Infinity).
Goal
The goal is to print/output the shortest string containing a decimal representation for which x is the closest IEEE 754 representation. Scientific notation is allowed. You can choose whether or not to allow a missing 0 in front of the ".".
x = 0x4000CCCCCCCCCCCD could be represented as "2.10000000000000008881784197001" but also as "2.1", which is much shorter. x = 0x3DDB7CDFD9D7BDBB can be represented as "1e-10" which is much shorter than alternative representation "1.0000000000000000364321973155E-10"
Rules
In addition to being correct, the program must run in a reasonable amount of time on a modern CPU, ruling out some forms of exhaustive search.
This being code-golf entries should be judged by concision, but voters should feel free to attribute points based on a discretionary judgment of elegance, and efficiency.
A few test cases
In these test cases, I represent the input as an hexadecimal number to make it clear that a IEEE 754 double 64 is passed. You can pass it as a double if the language supports it, or as a 64 bit integer, at your convenience.
f(0x4000CCCCCCCCCCCD) == "2.1"
f(0x3DDB7CDFD9D7BDBB) == "1e-10"
f(0x40934A456D5CFAAD) == "1234.5678" // N.B. not 1.2345678e3
f(0x3FEFFFFFFAA19C47) == "0.99999999"
f(0xC088A8CF13CEE9DD) == "-789.101112"`
Pretty much. Very efficient solutions require long pieces of code, but there are some simple ones which can be implemented concisely. – Arthur B – 2014-12-15T20:36:12.767
If by "C will parse" you're talking about C compilers, the standard says that "the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner." If you're talking about
scanf
there is still implementation-specific behaviour with regards to rounding. To make this question answerable, I think it needs to be rewritten to talk not about C's parsing but about the string-to-double conversion of the language used in the answer. – Peter Taylor – 2014-12-02T12:49:55.867The cleanest way of expressing it is probably to require that the answer be a function (whose length is counted for the score) and also include a test framework (length not counted) which demonstrates that the string returned by the function round-trips to the original value. – Peter Taylor – 2014-12-02T12:51:46.963
OK I'll edit in a few hours with a few test cases.
The requirement isn't compiler specific, it's a standard. But I'll replace with what the standard actually says... – Arthur B – 2014-12-02T13:10:28.927
1
Can I submit an entry for Python 3.1 with
– kennytm – 2014-12-02T15:51:22.180f=str
? Because Python will always choose the shortest representation.I suppose that technically counts, though I'd prefer to see an implementation of Dragon4 – Arthur B – 2014-12-02T16:25:25.147
@KennyTM, no.
– Peter Taylor – 2014-12-02T21:59:22.943@PeterTaylor: All 3 current answers are effectively just
f=str
though. – kennytm – 2014-12-03T08:34:06.217@ArthurB: Also may I suggest adding a test case for
f(0x400921fb54442d18) == "3.141592653589793"
? – kennytm – 2014-12-03T08:38:17.1572I don't think this challenge is worthwhile unless solutions that are effectively calls to
printf
are disqualified. – hobbs – 2014-12-06T07:17:56.330