15
2
Implement a function divide(int a, int b, int c) that prints the base 10 value of a/b. without using any floating point math nor BigInteger/BigDecimal or equivalent libraries whatsoever. At least c accurate characters within the set of 0123456789. must be printed, except for the (possible) exception in point 4 below.
aandbmay be any 32 bit integers. Update: If, for golfing purposes, you would like to have input be 64 bit primitives that is okay, but you do not need to support the whole 64 bit range of data.- You do not need to check that
cis positive (though hopefully your program does not crash) if it's not. - The minimum supported upper bound for
cis500. It is okay if your program does not support values ofcabove500, but it is also okay if it does. - For numbers that divide evenly, it is your choice whether to print extra zeroes (based on the value of
c) or nothing. - You do not need to be able to use the function to do any further tasks with the quotient, the only goal is printing.
- For numbers between
-1and1, it is your choice whether to print a leading0. However, this is the only scenario where printing a leading zero is acceptable, and you may only print one such zero. - You may use any rounding / floor / ceil logic you prefer for the last decimal place.
- For a negative answer, you must print a leading
-. This does not count towardsc. However, it is your choice if you wish to print,+, or nothing for a positive answer. - Integer division and integer modulus are both allowed. However, keep in mind that you are restricted to primitives, unless you choose to implement your own
BigInteger/BigDecimallibrary which counts against your code length. - You do not need to handle
bbeing0, though you can if you want. Your program may enter an infinite loop, or crash, ifb=0, and you will not be penalized. - Slight rule change per comment. To make sure the playing field is level, while
aandbare guaranteed to be 32 bit integers, you may use 64 bit long integers. If your chosen language goes beyond 64 bit integers as a primitive, you may not at any point use that functionality (pretend it is capped at 64 bits). - Another point that is unclear (it shouldn't change any of the current valid answers, though): while
cmay be interpreted as either the number of printed characters or the number of spaces after the decimal, your program must usecsomehow in a relevant way to decide how many characters to print. In other words,divide(2,3,2)should be much shorter output thandivide(2,3,500); it is not okay to print 500 characters without regard toc. - I actually don't care about the name of the function.
dis okay for golfing purposes.
Input
Both a function call and reading from stdin are accepted. If you read from stdin, any character not in the set [-0123456789] is considered an argument delimiter.
Output
Characters to stdout as described above.
Example
for divide(2,3,5), all of the following are acceptable outputs:
0.666
0.667
.6666
.6667
0.666
0.667
.6666
.6667
+0.666
+0.667
+.6666
+.6667
Another example: for divide(371,3,5) the following are all acceptable outputs:
123.6
123.7
123.6
123.7
+123.6
+123.7
123.66666
123.66667
123.66666
123.66667
+123.66666
+123.66667
And for divide(371,-3,5) the following are are all acceptable:
-123.6
-123.7
-123.66666
-123.66667
I think this title is misleading. "Arbitrary precision division" usually means "can divide arbitrarily large integers". This is not that. I see where it comes from, displaying an "arbitrary" number of decimal places, but I believe that the title would be better if it was changed. – Liam – 2016-02-06T21:39:00.530
For the sake of a level playing field, it might be wise to give a specific maximum bit length that can be used (primitive or otherwise) unless you roll your own larger implementation, because a) in some languages bit length of primitives varies depending on the underlying architecture and b) in some languages big number types are primitives. – Jonathan Van Matre – 2014-02-25T22:34:46.967
@JonathanVanMatre Added rule 11 per your comment – durron597 – 2014-02-25T22:50:22.560
1How do you count accurate digits? In your example I see three or four but never five as the last argument indicates. – Howard – 2014-02-26T06:07:54.803
@Howard, if you did
92,3,5the answer would be, for example,30.67– durron597 – 2014-02-26T10:32:03.7831btw 370/3=123.333 lol – izabera – 2014-02-26T11:34:13.180
@izabera boy am I tired lol – durron597 – 2014-02-26T22:14:19.310