15
1
Write a program or function that takes in positive integers a
, b
and c
, and prints or returns a/b
to c
decimal places, using the operations +-*/% [add, subtract, multiply, divide, modulate] on the positive integers: you can use all that your language allows, but not on floating point numbers. The range of a,b,c would be the range allowed for unsigned integers in your language.
The number result will be truncated to the last digit to print (so no round
).
This means that if your language does not have an integer type (only float), you can participate by using these float numbers as positive integers only. The clue of this exercise it would be to write the function that find the digits in a float point division, using only the operation +-*/% on [unsigned] integers.
Examples
print(1,2,1)
would print0.5
print(1,2,2)
would print0.50
print(13,7,27)
would print1.857142857142857142857142857
print(2,3,1)
would print0.6
print(4,5,7)
would print0.8000000
print(4,5,1)
would print0.8
print(9999,23,1)
would print434.7
print(12345613,2321,89)
would print if your Language has 32 bit unsigned5319.09220163722533390779836277466609220163722533390779836277466609220163722533390779836277466
The shortest code in bytes wins. I'm sorry if this appear not clear... I don't know languages too, not remember words well... It is better to have one link to Ideone.com or some other place for easily try the answer especially for to test some input different from proposed.
1What is the range of the integers a,b,c ? – Ton Hospel – 2016-10-12T20:42:15.260
@Ton Hospel the range of a,b,c would be the range allow for unsigned integer in your Language: for example if it is a 32 bit unsigned it would be 0..0xFFFFFFFF but if c>=0xFFFFFFF so big the output would be a little slow... – RosLuP – 2016-10-12T20:45:17.277
2
It is a rounding function - or, more precisely, it needs to be a rounding function to be properly specified. At present, it's unclear what the correct answer would be for e.g.
– Peter Taylor – 2016-10-12T20:52:31.223(1,2,0)
. See http://meta.codegolf.stackexchange.com/a/5608/1941Sorry for reopening this question; I think it's almost ready to be reopened, except for the problem Peter Taylor pointed out. What is the output fo
(1,2,0)
? – ETHproductions – 2016-10-12T20:59:15.5572Actually
(1,2,0)
should be irrelevant since0
is not a positive integer. And I'd prefer it if c remains like that since I'd prefer not having to think about appending a.
or not – Ton Hospel – 2016-10-12T21:30:56.813If
c
is always positive, you could change your(1,2,0)
example to(2,3,1)
(0.6
without rounding,0.7
with). – ETHproductions – 2016-10-12T22:42:38.173(2,3,0)
is still a useless example if we don't need to handle the non-positive casec=0
. Try(2,3,1)
instead, please. – ETHproductions – 2016-10-13T00:53:56.020What would
(4,5,2)
output?0.8
or0.80
– Brad Gilbert b2gills – 2016-10-13T03:24:24.570@BradGilbertb2gills would print "0.80"... this is a convension... it is so because here the clue is the function that find the digits in a division.... – RosLuP – 2016-10-13T04:05:30.660
I'm a bit confused about what you mean with "using the operations +-*/% [add, subtract, multiply, divide, modulate] on the positive integers:". As far as I understand, you will have
a
divided byb
which is a decimal number, and you round that toc
amount of decimal points behind the comma. So why would I need anything else besides/
as mathematical operand? – Kevin Cruijssen – 2016-10-13T07:27:58.897@KevinCruijssen if a and b are not negative integers, b!=0, than a/b as unsigned integer is not a decimal number is a not negative integer... 1/3 is not, see as integer 0.3333333[float] but it is 0[not negative integer]. 7/3=2 [as not negative integer ] ecc – RosLuP – 2016-10-13T09:14:18.527
@RosLuP So you basically mean that you should take
a/b
withc
decimal points, without rounding. So1/6
becomes1.166666...
and the output with four decimal points will be1.1666
(not rounded) instead of1.1667
. So someone could just use floor/down-rounding for the correct results. – Kevin Cruijssen – 2016-10-13T10:03:56.023@KevinCruijssen: yes, but you are only allowed to use integer math. No floating point division, for example. – nimi – 2016-10-13T10:19:02.947
@nimi Well, floating points only have a 'precision' of 17 decimal points if I'm not mistaken, and after that ever decimal point digit becomes
0
, so third and last test cases wouldn't even be possible to output with mere floating points. – Kevin Cruijssen – 2016-10-13T11:35:53.287Is it allowed to use
– nimi – 2016-10-13T16:42:17.257/
for numerical data types that provideRational
orFloating point
functionality but only useinteger
for the implementation? See also the discussion in the comments of this answer.@nimi If you use float Point as integer: is ok. For example 1.000000 / 2.000000 has to be "0.000000" as in the C language code unsigned a=1, b=2; a/b is 0; not 0.5. – RosLuP – 2016-10-13T17:06:19.707
@RosLuP: ... and other numerical data types like Rationals or Fixed-point? Did I get it right, they are only fine as long as the result of
/
is immediately turned into an integer (in the mathematical sense)? – nimi – 2016-10-13T17:13:28.507@nimi yes an integer that is truncated; as the "/" for unsigned integer type in the C Language example 8/3=2 not 8/3=2.6666666666666666666666666666667 so no round to the digit afther the point – RosLuP – 2016-10-13T17:22:35.253