Format a floating point number exactly as decimal Any binary floating point can be formatted exactly in decimal. The resulting string might be somewhat long, but it is possible. In my article on floating point I cover the importance of precision, and now I want this function. This challenge is to write a program, or function, that takes a floating point value as input and formats an exact decimal string as output.

To ensure we are working with the correct floating point numbers a precise format must be provided as input to the program. This format will be two integers Significand Exponent, where the actual floating point value is Significand * 2 ^ Exponent. Note that either value can be negative.

Specifics:


The range and precision of at least a 32-bit float must be supported (no input will go beyond that)
The decimal formatted value must be an exact representation (simply close enough to guarantee a correct round-tip back to float is not good enough)
We don't trust standard library floating point formatting functions to be correct enough nor fast enough (ex: printf), and thus they may not be used. You must do the formatting. Integral formatting/conversion functions are allowed.
There may not be any leading or trailing zeros, except for the required one leading zero in front of the . if there is no whole number component
A function, or whole program, is allowed.


Examples:

1 -2 =&gt; 0.25
17 -3 =&gt; 2.125
-123 11 =&gt; -251904
17 50 =&gt; 19140298416324608
23 -13 =&gt; 0.0028076171875
3 120 =&gt; 3987683987354747618711421180841033728
3 -50 =&gt; 0.00000000000000266453525910037569701671600341796875
-3 -50 =&gt; -0.00000000000000266453525910037569701671600341796875
10 -2 =&gt; 2.5
-12345 -3 =&gt; -1543.125
0 0 =&gt; 0
161 -4 =&gt; 10.0625
512 -3 =&gt; 64


Shortest code wins.
CJam, 43

r_'-&amp;\ize999rim&lt;s1e3'0e[W%999/(i_L?\+'.*sW%


Try it online

Explanation:

The program works with exponents up to ±999, close to double precision (64 bit). It separates the minus sign (if present) from the significand, multiplies it by 10999 then does a bit shift with the exponent, which is now an exact calculation. It then pads to the left with zeros if the result has less than 1000 digits, separates the last 999 digits as the fractional part, removes trailing zeros by converting its reverse to integer, adds a decimal point if needed, and puts everything back together.

r_         read and duplicate the significand in string form
'-&amp;        keep only the minus sign, if present
\          swap with the other copy of the significand
iz         convert to integer and get absolute value
e999       multiply by 10^999
ri         read the exponent and convert to integer
m&lt;         shift left by it; negative values will shift right
            the result is an exact non-negative integer
s          convert to string
1e3'0e[    pad to the left with zero characters up to length 1000
            longer strings will be left intact
            we need 1 more than 999 for the 0.xxx case
W%         reverse the string
999/       split into slices of length 999
(          take out the first slice (reversed fractional part)
i          convert to integer
            this removes the leading zeros (trailing in reverse)
_L?        if it's zero, replace with an empty string
\+         concatenate back (to the left) with the second slice
'.*        join the with the dot character
            if the fractional part was zero, we only have the second slice
            (reversed integer part) and there is nothing to join
s          convert to string; this is the reversed result without the sign
W%         reverse back


At the end, the minus sign (if any) and the final string are automatically printed together.