Division of not so little numbers

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 print 0.5
  • print(1,2,2) would print 0.50
  • print(13,7,27) would print 1.857142857142857142857142857
  • print(2,3,1) would print 0.6
  • print(4,5,7) would print 0.8000000
  • print(4,5,1) would print 0.8
  • print(9999,23,1) would print 434.7
  • print(12345613,2321,89) would print if your Language has 32 bit unsigned 5319.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.

RosLuP

Posted 2016-10-12T19:32:12.103

Reputation: 3 036

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. (1,2,0). See http://meta.codegolf.stackexchange.com/a/5608/194

– Peter Taylor – 2016-10-12T20:52:31.223

1Sorry 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.557

2Actually (1,2,0) should be irrelevant since 0 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.813

If 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 case c=0. Try (2,3,1) instead, please. – ETHproductions – 2016-10-13T00:53:56.020

What would (4,5,2) output? 0.8 or 0.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 by b which is a decimal number, and you round that to c 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 with c decimal points, without rounding. So 1/6 becomes 1.166666... and the output with four decimal points will be 1.1666 (not rounded) instead of 1.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.287

Is it allowed to use / for numerical data types that provide Rational or Floating point functionality but only use integer for the implementation? See also the discussion in the comments of this answer.

– nimi – 2016-10-13T16:42:17.257

@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

Answers

5

05AB1E, 17 13 11 19 14 bytes

Input in the form b, a, c.
Saved 5 bytes thanks to Grimy.

‰`¹+I°*¹÷¦'.sJ

Try it online!

Emigna

Posted 2016-10-12T19:32:12.103

Reputation: 50 798

I just tried it online with input 13,7,27 and answer is not correct – Mathieu J. – 2016-10-13T12:39:45.247

@shigazaru: It returns the same result as the test-cases. Did you notice the order of inputs is b,a,c?

– Emigna – 2016-10-13T12:59:06.490

I try the button "Try online" where there is this division:12345613/2321 with 89 digits, but the result there is something as 5319.922... instead of 5319.0922... – RosLuP – 2016-11-01T20:18:49.260

@RosLuP: That was an expensive fix :( I'll have to look into golfing this down at another time. – Emigna – 2016-11-01T21:16:22.663

@RosLuP: The accepted answer has the same issue, try 2/21 to 10 decimals for example. – Emigna – 2016-11-01T21:21:28.253

Yes in 2/21 10 digits it print to me 9 digit after the point – RosLuP – 2016-11-01T23:35:45.410

115: ‰`I°*¹÷I°+¦'.sJ – Grimmy – 2019-05-15T14:55:03.627

214: ‰`¹+I°*¹÷¦'.sJ – Grimmy – 2019-05-15T15:26:38.977

@Grimy:Thank you! I really should take the time to revisit some of my old answers. I should be able to golf quite a few of them, even without using new functionality (not that we still have a rule against that). – Emigna – 2019-05-15T15:47:29.627

4

Haskell, 87 bytes

(a#b)c|s<-show$div(a*10^c)b,l<-length s-c,(h,t)<-splitAt l s=['0'|l<1]++h++['.'|c>0]++t

Usage example: (13#7)27 -> "1.857142857142857142857142857".

23 bytes to handle the c==0 case and using a leading zero instead of things like .5.

How it works: multiply a with 10^c, divide by b, turn into a string, split where the . must be inserted, join both parts with a . in-between and fix the edge cases.

nimi

Posted 2016-10-12T19:32:12.103

Reputation: 34 639

4

Perl 6,  58 57 55  48 bytes

{(($^a.FatRat/$^b*10**$^c).Int.FatRat/10**$c).base(10,$c)}
{(Int($^a.FatRat/$^b*10**$^c).FatRat/10**$c).base(10,$c)}
{base Int($^a.FatRat/$^b*10**$^c).FatRat/10**$c: 10,$c}
{base ($^a*10**$^c div$^b).FatRat/10**$c: 10,$c}

What is fairly annoying is that it could be shortened to just {($^a.FatRat/$^b).base(10,$^c)} if it was allowed to round to the nearest value.

Explanation:

# bare block lambda with 3 placeholder parameters 「$^a」, 「$^b」 and 「$^c」
{
  (
    (

      # create an Int containing all of the digits we care about
      $^a * 10 ** $^c div $^b

    ).FatRat / 10**$c  # turn it into a Rational

  ).base( 10, $c )     # force it to display 「$c」 digits after the decimal point
}

Brad Gilbert b2gills

Posted 2016-10-12T19:32:12.103

Reputation: 12 713

I'm not familiar with perl6, but isn't (...).FatRat / 10**$x a division of a Rational? You're only allowed to divide integers. – nimi – 2016-10-13T05:34:26.337

@nimi a Rational is a class with two Ints.

– Brad Gilbert b2gills – 2016-10-13T15:02:44.297

The math operators are only allowed for integer types and not for other numerical types. Quote: "The clue of this exercise ... to write the function ... using only the operation +-*/% on [unsigned] integers." – nimi – 2016-10-13T15:55:04.427

@nimi So if I wrote a reimplementation of Rational, and didn't add a does Real or does Numeric it would be allowed? What if I augment (monkey patch) the existing class to remove those roles, would that be allowed? – Brad Gilbert b2gills – 2016-10-13T16:14:29.077

Don't know. I read the spec as said before: +-*/% only with plain integer types. "plain integer" in terms of functionality (first of all: integer division) not of internal representation. Do you think it's allowed to use a software floating point library, which (despite the name) also uses only integers for internal representation? – nimi – 2016-10-13T16:34:52.310

I've asked the OP to clarify. – nimi – 2016-10-13T16:43:02.157

If you use float Point as integer: is ok. For example 1.000000 / 2.000000 has to be "0" as in the C language code unsigned a=1, b=2; a/b is 0; not 0.5. – RosLuP – 2016-10-13T17:03:45.977

@RosLuP Rational is stored as two Int's (Numerator,Denominator), and that class/type is doing integer math internally. Is it ok that I'm using a built-in class that only pretends to be an unlimited precision float.

– Brad Gilbert b2gills – 2016-10-13T17:17:12.437

3

PHP, 187 Bytes

works with strings for the numerator which can be int values greater then PHP_INT_MAX

list(,$n,$d,$c)=$argv;$a=str_split($n);while($a){$n.=array_shift($a);if($n>=$d||$r)$n-=$d*$r[]=$n/$d^0;}if(!$r)$r[]=0;if($c)$r[]=".";while($c--){$n*=10;$n-=$d*$r[]=$n/$d^0;}echo join($r);

I have no other chance then 13/7 is shorten to 1.8571428571429 and I reach so not the test case with 27 decimal places

This way 36 Bytes is not allowed

<?=bcdiv(($z=$argv)[1],$z[2],$z[3]);

Jörg Hülsermann

Posted 2016-10-12T19:32:12.103

Reputation: 13 026

float division is not allowed as per OP – Maltysen – 2016-10-12T22:27:09.010

@Maltysen Sorry I make a rollback.I was so afraid to find a shorter solution that I not recognize that it against the specifation. – Jörg Hülsermann – 2016-10-12T22:36:27.900

3

Perl, 55 bytes

Includes +3 for -p

Give a and b on one line on STDIN, c on the next

division.pl
1 26
38
^D

division.pl:

#!/usr/bin/perl -p
eval'$\.=($_/$&|0)."."x!$\;$_=$_%$&.0;'x(/ .*/+<>)}{

the $_/$& is a bit arguable. I Actually want an integer division there but perl doesn't have that without loading special modules. So it's temporarily a non-integer which I then immediately truncate (using |0) so I end up with the integer an integer division would give. It could be rewritten as ($_-$_%$&)/$& in order to not even temporarily have a non-integer value (it would internally still be a float though)

Ton Hospel

Posted 2016-10-12T19:32:12.103

Reputation: 14 114

Could you use $- to make it int only? (I think there are strict limits on the mix/max of it, and I'm sure you'd have already considered it, but though it worth checking!) – Dom Hastings – 2016-10-13T08:16:05.037

@DomHastings It would still be a truncate after doing a floating point division. Perl simply doesn't have integer division without use integer – Ton Hospel – 2016-10-13T08:37:07.093

Ah, so it's just masked with $-, good to know. Thanks! – Dom Hastings – 2016-10-13T09:04:16.040

3

JavaScript (ES6), 55 50 bytes

f=(a,b,c,d=".")=>~c?(a/b|0)+d+f(a%b*10,b,c-1,""):d

(a/b|0) performs float division but immediately casts to an integer. Please let me know if this is not allowed.

ETHproductions

Posted 2016-10-12T19:32:12.103

Reputation: 47 880

2

Pyth - 21 19 18 16 14 bytes

Will be looking over the input format, that can prolly save a bunch.

j\.c`/*E^TQE]_

Test Suite. (P.S. the 27 one doesn't finish online, so I did 10 instead).

Maltysen

Posted 2016-10-12T19:32:12.103

Reputation: 25 023

@Emigna Nor does it have to since 0 is not a positive integer (even though the op keeps adding examples with c=0) – Ton Hospel – 2016-10-13T09:40:37.067

@TonHospel: Ah, indeed. I was looking at the examples. I can shorten my answer then :) – Emigna – 2016-10-13T09:43:29.960

Doesn't work with numbers that has a leading 0 after the decimal point, for example 2/21 to 10 decimals. – Emigna – 2016-11-01T21:23:00.113

2/21 with 10 digits print only 9 digit after the point – RosLuP – 2016-11-01T23:25:57.517

2/21 with 10 digits as Emigna says instead of 0.09... print 0.9... – RosLuP – 2016-11-01T23:39:39.927

2

JavaScript (ES6),  64  62 59 bytes

Saved 2 bytes thanks to ETHproductions.

The included division always results in an integer.

f=(a,b,n,s)=>~n?f((q=(a-a%b)/b,a%b*10),b,n-1,s?s+q:q+'.'):s

console.log(f(13,7,27))

Arnauld

Posted 2016-10-12T19:32:12.103

Reputation: 111 334

Would skipping m altogether still work? f=(a,b,n,s)=>n+1?f((q=(a-a%b)/b,a%b*10),b,n-1,s?s+q:q+'.'):s is 60 bytes. – ETHproductions – 2016-10-13T00:51:22.100

@ETHproductions - Indeed. Thanks! – Arnauld – 2016-10-13T07:22:48.690

2

Racket 203 bytes

(let*((s(~r(/ a b)#:precision c))(sl(string-split s "."))(s2(list-ref sl 1))(n(string-length s2)))
(if(< n c)(begin(for((i(- c n)))(set! s2(string-append s2 "0")))(string-append(list-ref sl 0)"."s2))s))

Ungolfed:

(define (f a b c)
  (let* ((s (~r(/ a b)#:precision c))
         (sl (string-split s "."))
         (s2 (list-ref sl 1))
         (n (string-length s2)))
    (if (< n c)
        (begin 
          (for ((i (- c n)))
            (set! s2 (string-append s2 "0")))
          (string-append (list-ref sl 0) "." s2))
        s )))

Usage:

(f 7 5 3)
(f 1 2 1) 
(f 1 2 2) 
(f 13 7 27)

Output:

"1.400"
"0.5"
"0.50"
"1.857142857142857142857142857"

Other method (not valid answer here):

(real->decimal-string(/ a b)c)

rnso

Posted 2016-10-12T19:32:12.103

Reputation: 1 635

I'm afraid this is invalid, because real->decimal-string expects a real value as its first argument, so / is floating point division, which is not allowed in this task. Also: real->decimal-string rounds ((f 1 6 7) -> 0.1666667) instead of truncating. – nimi – 2016-10-13T06:33:00.440

Thanks for the observations. I will correct the code soon. – rnso – 2016-10-13T11:41:47.757

2

Java 7, 105 bytes

import java.math.*;String c(int...a){return new BigDecimal(a[0]).divide(new BigDecimal(a[1]),a[2],3)+"";}

Ungolfed & test code:

Try it here.

import java.math.*;
class M{
  static String c(int... a){
    return new BigDecimal(a[0]).divide(new BigDecimal(a[1]), a[2], 3)+"";
  }

  public static void main(String[] a){
    System.out.println(c(1, 2, 1));
    System.out.println(c(1, 2, 2));
    System.out.println(c(13, 7, 27));
    System.out.println(c(2, 3, 1));
    System.out.println(c(4, 5, 7));
    System.out.println(c(4, 5, 0));
    System.out.println(c(9999, 23, 0));
    System.out.println(c(12345613, 2321, 89));
  }
}

Output:

0.5
0.50
1.857142857142857142857142857
0.6
0.8000000
0
434
5319.09220163722533390779836277466609220163722533390779836277466609220163722533390779836277466

Kevin Cruijssen

Posted 2016-10-12T19:32:12.103

Reputation: 67 575

I don't think this is valid, because it's essentially floating-point division, even though it's called divide and not /. – corvus_192 – 2016-10-13T19:14:26.647

@corvus_192 Another deleted Java answer that posted later than me had the reasoning, which I'll copy-paste here (Credit for this explanation goes to @SocraticPhoenix): "How It Works: Java BigDecimals are implemented as BigIntegers, with a scale. This is technically floating point, however the BigInteger and BigDecimal objects use only the int type to store numerical value. (Isn't that cool? BigInteger is an int[] of digits. Like, {1,2,5} in base 10 is 125. I'm not sure what base the digits of BigInteger are actually in, but I would guess it's more than 10." – Kevin Cruijssen – 2016-10-14T08:56:03.423

2

Ruby, 67 bytes

->(a,b,c){('0'+(a*10**c/b).to_s).gsub(/^0*(.+)(.{#{c}})$/,'\1.\2')}

if I make it a function to execute the test cases above

def print(a,b,c); ('0'+(a*10**c/b).to_s).gsub(/^0*(.+)(.{#{c}})$/, '\1.\2'); end
 => :print 
print(1,2,1)   # would print 0.5
 => "0.5" 
print(1,2,2)   # would print 0.50
 => "0.50" 
print(13,7,27) # would print 1.857142857142857142857142857
 => "1.857142857142857142857142857" 
print(2,3,1)   # would print 0.6
 => "0.6" 
print(4,5,7)   # would print 0.8000000
 => "0.8000000" 
print(4,5,1)   # would print 0.8
 => "0.8" 
print(9999,23,1) # would print 434.7
 => "434.7" 
print(12345613,2321,89) # would print if your Language has 32 bit unsigned 5319.09220163722533390779836277466609220163722533390779836277466609220163722533390779836277466
 => "5319.09220163722533390779836277466609220163722533390779836277466609220163722533390779836277466" 
"('0'+(a*10**c/b).to_s).gsub(/^0*(.+)(.{#{c}})$/, '\1.\2')".length
 => 52 

Mathieu J.

Posted 2016-10-12T19:32:12.103

Reputation: 121

Welcome to code golf! For most languages, when you define a function, you have to define it all the way, and you can't rely on assuming there are predefined variables like that. In Ruby, the shortest way to define a lambda is with ->a,b,c{...} where you replace the ellipses with your code. (The actual assigning of the variable is unneeded by consensus.) – Value Ink – 2016-10-13T19:21:08.703

thanks, I was under the wrong impression that others had left out that part... but you're right. I just added it. – Mathieu J. – 2016-10-21T03:22:15.767

1

Python 3, 62 Bytes

a,b,c=map(int,input().split());print("{:.{1}f}".format(a/b,c))

Try It Here

*Note: repl.it uses an older ver of Python 3, which requires all field indices to be specified, meaning "{:.{1}f}" will be "{0:.{1}f}" instead, making it 63 bytes in repl.it

How to use

Enter all three values with spaces in-between. i.e. An input of 1 2 1 would give a result of 0.5

Explanation

input().split(): Gets user input and splits it into a list with a separator of (space)

a,b,c = map(int,XX): Maps the variables into the values specified by the user with an int type

"{:.{1}f}".format(a/b,c): Formats a string to display the division result and substitutes {1} with c to set the decimal place of the displayed string

print(XX): prints the string supplied

user7297223

Posted 2016-10-12T19:32:12.103

Reputation: 11

1

Stax, 13 bytes

ä·oαì█↕▬AS¥é▼

Run and debug it

Arguments are accepted in c a b order.

recursive

Posted 2016-10-12T19:32:12.103

Reputation: 8 616

The problem would be the case "print(9999,23,1)" the result print in the post is "434.7" while your result seems "4.7" the same the case last: 5319.092201etc instead of 9.092201etc – RosLuP – 2019-05-16T08:51:45.123

I not agree... I followed this morning your link and the result for the case "print(9999,23,1)" in your function order was 4.7 and not 434.7 – RosLuP – 2019-05-16T15:48:09.043

@RosLuP: I changed the approach. The result is now correctly 434.7. I also reduced the size by a byte in the process. – recursive – 2019-05-16T16:11:50.320

1

Python 3, 58 bytes

lambda a,b,c:(lambda s:s[:-c]+"."+s[-c:])(str(a*10**c//b))

Try it online!

This is precise to the specified number of decimals as long as a * 10 ** c isn't too large.

I tried Python 2 to shorten the str(...) to `...` but Python 2 inserts an L at the end if it's too large so checking for that would take way more bytes than it's worth.

HyperNeutrino

Posted 2016-10-12T19:32:12.103

Reputation: 26 575

1

q, 196 bytes

w:{((x 0)div 10;1+x 1)}/[{0<x 0};(a;0)]1;{s:x 0;m:x 2;r:(10*x 1)+$[m<0;{x*10}/[-1*m;a];{x div 10}/[m;a]]mod 10;d:1#.Q.s r div b;($[m=-1;s,".",d;$[s~,:'["0"];d;s,d]];r mod b;m-1)}/[c+w;("";0;w-1)]0

To run: set a, b, c first.

Boston Walker

Posted 2016-10-12T19:32:12.103

Reputation: 141

1

Rust, 114 bytes

fn print(mut a:u32,b:u32,c:u32){let mut p=||{print!("{}",a/b);a=a%b*10};p();if c>0{print!(".")}for _ in 0..c{p()}}

test code:

fn main() {
    print(1, 2, 1);    println!(""); // should print 0.5
    print(1, 2, 2);    println!(""); // should print 0.50
    print(13, 7, 27);  println!(""); // should print 1.857142857142857142857142857
    print(2, 3, 1);    println!(""); // should print 0.6
    print(4, 5, 7);    println!(""); // should print 0.8000000
    print(4, 5, 0);    println!(""); // should print 0
    print(9999, 23, 0);println!(""); // should print 434
    print(12345613,2321,89); println!("\n");  // 5319.09220163722533390779836277466609220163722533390779836277466609220163722533390779836277466
}

Christopher Phillips

Posted 2016-10-12T19:32:12.103

Reputation: 11

1

PHP, 89 bytes

list(,$a,$b,$c)=$argv;for($o=intdiv($a,$b).'.';$c--;)$o.=intdiv($a=10*($a%$b),$b);echo$o;

intdiv() is introduced in php 7 so it requires that. php 7.1 would allow me to change the list() to [] and so would save 4 bytes.

use like:

php -r "list(,$a,$b,$c)=$argv;for($o=intdiv($a,$b).'.';$c--;)$o.=intdiv($a=10*($a%$b),$b);echo$o;" 1 2 1

user59178

Posted 2016-10-12T19:32:12.103

Reputation: 1 007

replace $o.=intdiv($a=10*($a%$b),$b); with $o.=($a=10*($a%$b))/$b^0; will save 4 Bytes. – Jörg Hülsermann – 2016-10-13T14:56:41.280

Initially I was going to take that advice (I edited the answer and everything) but on second thought it IS floating point division and then a cast to int so for a <10% increase in length I'd rather stick completely within the specs of the question. – user59178 – 2016-10-13T16:00:05.483

1

C#, 126 bytes

(a,b,c)=>{var v=a*BigInteger.Parse("1"+new string('0',c))/b+"";return v.PadLeft(c+1,'0').Insert(Math.Max(1,v.Length-c),".");};

Full program with test cases:

using System;
using System.Numerics;

namespace DivisionOfNotSoLittleNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<BigInteger,BigInteger,int,string>f= (a,b,c)=>{var v=a*BigInteger.Parse("1"+new string('0',c))/b+"";return v.PadLeft(c+1,'0').Insert(Math.Max(1,v.Length-c),".");};

            //test cases:
            Console.WriteLine(f(1,2,1));    //0.5
            Console.WriteLine(f(1,2,2));    //0.50
            Console.WriteLine(f(13,7,27));  //1.857142857142857142857142857
            Console.WriteLine(f(2,3,1));    //0.6
            Console.WriteLine(f(4,5,7));    //0.8000000
            Console.WriteLine(f(4,5,1));    //0.8
            Console.WriteLine(f(9999,23,1));    //434.7
            Console.WriteLine(f(12345613,2321,89)); //5319.09220163722533390779836277466609220163722533390779836277466609220163722533390779836277466
            Console.WriteLine(f(2,3,1));    //0.6
            Console.WriteLine(f(4,5,2));    //0.80
        }
    }
}

Integer division is implemented. Numbers of any size can be used, due to the BigInteger data type (the import System.Numerics is required). The digit-count parameter c is restricted to 2^31-1, however it should provide more than enough digits.

adrianmp

Posted 2016-10-12T19:32:12.103

Reputation: 1 592

1

Groovy (78 77 42 Bytes)

{a,b,n->Eval.me(a+'.0g').divide(b, n, 1)}​

Explanation

Eval.me(a+'.0g'); - Convert from integer input to BigDecimal input. In groovy BigDecimal notation is double notation with an appended G or g. I could've also used the constructor new BigDecimal(it) but this saved a byte.
.divide(b, n, 1) - Divide by b with n precision, rounding mode half-up.

Try it here: https://groovyconsole.appspot.com/script/5177545091710976

Magic Octopus Urn

Posted 2016-10-12T19:32:12.103

Reputation: 19 422

But BigDecimal supports floating point operation which, unless I am mistaken, I believe was indicated to be forbidden for this exercise. – Mathieu J. – 2016-10-21T03:08:27.000

1

Batch, 122 bytes

@set/as=%1/%2,r=%1%%%2
@set s=%s%.
@for /l %%a in (1,1,%3)do @set/ad=r*10/%2,r=r*10%%%2&call set s=%%s%%%%d%%
@echo %s%

Neil

Posted 2016-10-12T19:32:12.103

Reputation: 95 035

1

Mathematica, 50 bytes

StringInsert[ToString@Floor[10^# #2/#3],".",-#-1]&

Unnamed function of three arguments (which are ordered c, a, b to save a byte somewhere), which returns a string. It multiplies a/b by 10^c, takes the greatest integer function, then converts to a string and inserts a decimal point at the appropriate spot. Too bad the function names aren't shorter.

Greg Martin

Posted 2016-10-12T19:32:12.103

Reputation: 13 940

0

C, 67 bytes

h(a,b,i){int j=!i;for(;printf(j++?"%u":"%u.",a/b),i--;a=10*(a%b));}

Try it online!

Some previous version I think had a bug in read memory out of assigned to the program...Thanks to ceilingcat and for all...

RosLuP

Posted 2016-10-12T19:32:12.103

Reputation: 3 036

@ceilingcat ok thank you – RosLuP – 2019-05-16T17:30:57.023

I like the last one 69 bytes – RosLuP – 2019-05-16T18:34:22.643

62 bytes – ceilingcat – 2019-05-16T21:02:21.483

@ceilingcat ok thank you – RosLuP – 2019-05-17T04:18:28.340