9
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 => 0.25
17 -3 => 2.125
-123 11 => -251904
17 50 => 19140298416324608
23 -13 => 0.0028076171875
3 120 => 3987683987354747618711421180841033728
3 -50 => 0.00000000000000266453525910037569701671600341796875
-3 -50 => -0.00000000000000266453525910037569701671600341796875
10 -2 => 2.5
-12345 -3 => -1543.125
0 0 => 0
161 -4 => 10.0625
512 -3 => 64
Shortest code wins.
3Is the use of unlimited precision floating point arithmetic allowed? – Dennis – 2015-07-08T04:55:09.680
2If the exponent is nonnegative, can we end with
.0
? – Sp3000 – 2015-07-08T04:55:21.687@Dennis: Yes, unlimited or high fixed precision arithmetic is allowed. – edA-qa mort-ora-y – 2015-07-08T05:19:55.133
@Sp3000: No, that's a trailing zero and is disallowed. I think a trailing
.
should also be disallowed. – edA-qa mort-ora-y – 2015-07-08T05:20:21.580May I suggest adding the test case
-3 -50
? There are no test cases where both numbers are negative and my first submission broke on it. – Dennis – 2015-07-08T06:28:27.967Added the test case. If anybody else has any test cases that trip up their code please list them in case they can also trip up other solutions as well. – edA-qa mort-ora-y – 2015-07-08T07:43:42.340
1I think that's inconsistent. If
0.abc
is not a leading zero, thenabc.0
isn't a trailing one. – orlp – 2015-07-08T15:43:31.297I suggest adding cases
10 -2
and-12345 -3
– aditsu quit because SE is EVIL – 2015-07-08T17:00:51.693@orlp: It's just convention when writing numbers to not lead with
.
but to always put a0
in the front. – edA-qa mort-ora-y – 2015-07-08T17:10:45.4671It's also convention to always end with
.0
for whole numbers when dealing with floating point numbers. See for example Python:str(1.0) == '1.0'
versusstr(1) == '1'
. Your logic is still inconsistent. – orlp – 2015-07-08T17:58:37.510@orlp Maybe the OP prefers JavaScript style formatting :) Anyway, he has the right to define the requirements (and you have the right to raise your concern too) – aditsu quit because SE is EVIL – 2015-07-08T18:08:47.487
Another case suggestion:
0 0
(not sure if other exponents are needed) – aditsu quit because SE is EVIL – 2015-07-08T19:09:02.510And another case:
161 -4
(zero before and after decimal point but not starting with 0) – aditsu quit because SE is EVIL – 2015-07-08T21:37:22.920Proposed test case:
512 -3
. Negative exponent, but result has no fractional part. – Reto Koradi – 2015-07-09T14:57:37.607