[Question]
  [
# Background
Sometimes in calculus you're expected to calculate the sum of an infinite series. Sometimes these series are very friendly, like a geometric series, but add anything else onto it and it can quickly get complicated to solve by hand.
Sometimes I like to be lazy - a lot of sums can be found simply by adding the first few terms then making an approximation. Say the sum of the first ten terms is 0.199999983, and the future terms are approaching zero. We can say with a fair degree of certainty that our final answer will be 0.2, or 1/5.
# The Challenge
Given a decimal number and an integer as inputs, calculate the best (fully simplified) fractional approximation of the decimal number for all fractions up to a denominator of the given integer. The best fractional approximation will be that which is closest to the decimal number in absolute value.
You may take these inputs any way you like and you may output the numerator and denominator any way you like. The numerator and denominator must always be integers, and you may assume we will deal with only positive numbers because adding a negative sign is trivial.
# Test Cases
```
Input | Output
1.21, 8 | 6/5
3.14159265359, 1000000 | 3126535/995207
19.0, 10000000 | 19/1
3.14159265359, 12 | 22/7
2.7182818, 100 | 193/71
0.8193927511, 22 | 9/11
0.2557463559, 20 | 1/4
0.2557463559, 100 | 11/43
0.0748947977, 225 | 14/187
```
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). Shortest code wins!
  
[Answer]
# [Python 3](https://docs.python.org/3/), ~~66~~ 61 bytes
```
lambda x:Fraction(x).limit_denominator
from fractions import*
```
[Try it online!](https://tio.run/##ZY3PasQgEIfP9SmGPSVFRMe46sJe@xSFYmtChfgHIyX79GmyW3po5zLMb75vptzaZ05ym66v2@ziu3ewXl6q@2ghp27t2RxiaG9@TDmG5FquZKo5wvSDLBBiybU9b48Oy20hU66wUvAQEkRXuvHLzfTYsKX5kPoLeSo1pNYdEIVp/9P5fifGcj1ROPWbYCgoGCKZGISyeFZSWQqC34sIy/jvxP9RSJBpYdAIc6cIZ0ZYaVErsZ9F3ANUSg9nqQ4B@Z/g4XA9GDtoq/XhqG8 "Python 3 – Try It Online")
The above function takes a floating point number and returns a bound function `Fraction.limit_denominator` which, in turn, takes the upper bound for the denominator to return a simplified, approximated fraction as requested.
As you can tell, I’m more of an API reader than a golfer.
---
* minus 5 bytes [thanks to ovs](/questions/207352/cleaning-up-decimal-numbers/207379#comment491671_207379)
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), ~~77~~ 71 bytes
*-6 bytes thanks to @ovs !*
```
lambda x,n:min([abs(x-(a:=round(x*b))/b),a,b]for b in range(1,n+1))[1:]
```
[Try it online!](https://tio.run/##ZY/RboMgGIXvfQrSK9iY@KMUMfFJul7gajcSRYJ0sUnf3YFbm2UzXuj5zndQdw0fky1r59dz@7oOeuxOGi3UNqOx@KC7GS8vWDetny72hJenjhDWEappdzxPHnXIWOS1fe8xUPsMhBygOa5mdJMPaL7OWWoNxvapGN/zOZyMbTKEFktRv7j@LfQn1KJROzwHH7k3jm5KPrvBBLy77QhJAkU2FvtPPeDFpuSXfjA24EBQOi6ks@7sPsJ25BiVePt@vgwhOmccfzTtOJ/s75w@ePvYICvkHCiq0Q3tmcjKHCoQiu9FKRRFUGxXhCVsGVNK8EJmoPLigRMHxeCfzSPgnMmM5xJqXkO9OVu9ZBKyIq/jk@JSQPwInvpxJ@VcCFntS5F2@Gaw6k/8sxRBGUkhq1pVUkmZhkQiFYNafgE "Python 3.8 (pre-release) – Try It Online")
Simply try all denominators from `1` to `n`, saving all results into a list where each element has the form `[error, numerator, denominator]`. By taking the min of the list, the fraction with the smallest error is selected.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~168, 135~~, 87 bytes
```
z=i=1
def f(x,y):exec"r=round(x*i);q=abs(r/i-x)\nif q<z:z=q;t=r;u=i\ni+=1;"*y;print t,u
```
[Try it online!](https://tio.run/##jY9BboMwEADPzSus5IJTStkFx3aIf9ILAbu12tgBjAR8nkAq5ZIeuseRZnb3OoYv73CeJ2UVbGptiImGeKRHPehq26rW966Ohr2lRaPKcxe17/ZtoB/OGtKcpuOkmiKotuiVXdirgmK7H4tra10gIe5nE0GCEBNBNzsTZQnkwCQeWMZkTCC9D33ZVf5y0S7omvg@kLOuyr7TxC6N8lt3JHhPfrz7XBsgk/ShLu7/5ef9SBeICQeBAsS9upI0ESAziZzBcjriL0PGeH7I2Gpi@swecspzIXMuOV9lRue/374B "Python 2 – Try It Online")
Thank you all for your recommendations on my first golf!
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 11 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
î*LãΣ`/¹α}н
```
[Try it online](https://tio.run/##ASsA1P9vc2FiaWX//8OuKkzDo86jYC/Cuc6xfdC9//8wLjI1NTc0NjM1NTkKMTAw) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWXCoZX/D6/T8jm8@NziBP1D685trL2w97/O/@hoCx1DPSPDWJ1oQyMdYz1DE0NTSyMzU2NTS5CQgYGOkZ65oYWRhaEFkG9kpGOgZ2FoaWxpZG5qCNJkZAAUMTI1NTcxMzaF60ETMjIyBQoZmJtYWJqYW5qbx8YCAA) (except for the two `1000000` test cases, which take too long).
**Explanation:**
```
î          # Ceil the (implicit) input-decimal
 *         # Multiply it by the (implicit) input-integer
  L        # Pop and push a list in the range [1, ceil(decimal)*int]
   ã       # Create all possible pairs of this list by taking the cartesian product
    Σ      # Sort this list of pairs by:
     `     #  Pop and push both values separated to the stack
      /    #  Divide them by one another
       ¹α  #  Get the absolute difference with the first input-decimal
    }н     # After the sort: leave only the first pair
           # (after which it is output implicitly as result)
```
[Answer]
# [Python 3.8](https://docs.python.org/3.8/), 169 bytes
Maybe the reason why there was no submissions using [Farey sequences](https://en.wikipedia.org/wiki/Farey_sequence) is that the code appears rather lengthy.
In short, every proper fraction \$\frac{m}{k}\$ in lowest terms appears in Farey sequence of order \$d\$ if and only if \$k\le d\$.  
Farey sequences are constructed by taking [mediant](https://en.wikipedia.org/wiki/Mediant_(mathematics)) of neighboring terms of lower order: \$\left(\frac ab,\frac cd\right)\to\frac{a+c}{b+d}\$, starting from \$\left(\frac 01,\frac 11\right)\$.
And the target number is within one of the intervals \$\left[\frac ab,\frac{a+c}{b+d}\right]\$, \$\left[\frac{a+c}{b+d},\frac cd\right]\$, then we take the interval as a current one.
So the algorithm is:
1. Take \$\left(\frac ab,\frac cd\right):=\left(\frac 01,\frac 11\right)\$.
2. Take \$\frac{m}{k}:=\frac{a+c}{b+d}\$ and compare with target.
3. If \$\frac{m}{k}>\$ target, replace \$\frac{a}{b}\$ by \$\frac{m}{k}\$,
4. Else replace \$\frac{c}{d}\$ by \$\frac{m}{k}\$.
5. If the next denominator \$b+d\$ is no more than denominator limit, repeat from 2.
6. Else return nearest from \$\frac ab,\frac cd\$ to the target.
```
def f(e,n,g,j):
	if n==0:return e,1
	x=[(0,1),(1,1)]
	while True:
		(a,b),(c,d)=x
		if b+d>j:break
		m,k=a+c,b+d
		x[m*g>n*k]=(m,k)
	m,k=x[2*n/g-a/b>c/d]
	return m+e*k,k
```
[Try it online!](https://tio.run/##ZZLNkqMgEMfP4Sm4CYYVQQmaKvIUe8vkgIqJEyUWmppM1b57tjXZPez2Aej/r7tpPsbv@XLzWTGG57NxLW6JY56d2Sfdo03XYm9Mug9uvgePHRNo8zBHkjJBGREwntDm69L1Dv8MdwcZG2JZBaxmDTUP8KFEtW0On/sqOHsFYWBXY7c1AxW8x3GIzwcfX0@GAKFo5Y@jjD0//7C8OtS8gU3eHQxbF1/Z9TmZEEWRSKRguMC/8I4rlCUiF6qUO5WpkmGRrgYwE6vGy1LJVCNRJulfvHBRcvFftgQgJddIJloUshDFmrOGZ1wLlCYFrEqplYAm5BIPdRZdKqXzXaaWOnLN4Pk/8rsSgAxIqvOizHWp9VJILSTnotAIzoi6YbyFGQeHBjvXFxdMcEl9G0a4dYIwGPjTvSIh@mi2EevtUDUWD/sQERBoxP4Euam2oyNTMo19N5Pow0f0mJ7oyt/2cihqbwHDSzJ4R@ZwBzf/2jxpO9/YvicT/I8l1E6Tg/Za0vmZWMqWCT6ASOO4d35ZLkpNqTFrSPMSHKXP3w "Python 3.8 (pre-release) – Try It Online")
Eats (entire\_part,proper\_numerator,proper\_denominator,denominator\_limit) and produces (numerator,denominator), like
```
>>> f(3,141592653589793,10**len('141592653589793'),57)
(179, 57)
```
P.S. Recursive version is nothing but shorter, even with all whitespace removed:
```
f=(lambda e,n,g,j,a=0,b=1,c=1,d=1:
   n and(
       b+d>j and(lambda x,y:(x+e*y,y))(*([(a,b),(c,d)][2*n/g-a/b>c/d]))
       or((m:=a+c)*g>n*(k:=b+d))and f(e,n,g,j,a,b,m,k)or f(e,n,g,j,m,k,c,d)
       )or(e,1)
   )
```
[Try it online!](https://tio.run/##ZVLRjqQgEHznK3iTdnpVUAY1cX5kdh5QcdZdRYNzyU5y/z4H7uzmckdCaKqqi4Zmvd/eFpuXq3sMDXtMem57TQ1avOI76ibDtuHY@dk3vCaWatszQiltD/3pfd89cz7xXrPPg4nveAdgMTszjS0g67CHy1nENr2@6LQ9dWl/AQgei2Nsrht96CC@nmzMPurG@wJ4WzqwnyqwxRk/YHF/gR7A4Bx8PONxDg8gW@OiKOKJ4EhL@pseU0nyhBdcVuIoc1kh5dk@PJnzHUurSopMEV4l2Q8deF6l/L9s4QkhUkVEongpSl7uObs8TxUnWVL6qBJKcl@ECHrvE3AhpSqOuQw@Ys9Ii3/gp5Mncs9kqiirQlVKBSMZmCLlpSL@jmSc18XdqDNk1rfuzbjGmaRb5nWczN4iTyXbr5a56LU/RPjs01y7iHkAIvwWma3Tq2Fbsq3TeGPRq43gnF32t/0eXxsgg@9CaIh/ezR0tPR5eDKMttfTxDaod6neNuPLG9hob0wDhsV/B57F8WRsCAPSATTNLum/AAPw@AM)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 31 bytes
```
Ｎθ⪫…⮌⌊ＥＥＮ⌊⁺·⁵×θ⊕κ⟦↔⁻θ∕ι⊕κ⊕κι⟧²/
```
[Try it online!](https://tio.run/##bYzLCsIwFET3fkVwdQOxWrWCdCUVoUJFxJ246OOCF/OoaVPw66NmJeKB2Qxzpr6Vtjal9D7XresPTlVo4cHT0dGS7mFvSEP2rCVmN9PCCQe0HUJBmpRTUJRtyLfMBdtJYywcpetgFiWCnUlhBw/Bcl1bVKh7bODOPwh22VSdka4Pry7MtjRQg0B/hN9KMLqGev7OeDrmPPV@EcXLOFnPV8kiWY/iWcBPBvkC "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ                              Input decimal as a number
        Ｎ                       Input maximum denominator
       Ｅ                        Map over implicit range
                κ               Current index (0-indexed)
               ⊕                Increment (i.e. 1-indexed)
             ×                  Multiplied by
              θ                 Input decimal
         ⌊⁺·⁵                   Round to nearest integer
      Ｅ                         Map over results
                      ι         Current numerator
                     ∕          Divided by
                       ⊕κ       Current denominator
                    θ           Input decimal
                  ↔⁻            Absolute difference
                         ⊕κ     Current denominator
                           ι    Current numerator
                 ⟦          ⟧   Make into list
     ⌊                          Take the minimum (absolute difference)
    ⮌                           Reverse the list
   …                         ²  Take the first two entries
  ⪫                           / Join with literal `/`
                                Implicitly print
```
I'm not 100% sure that the algorithm is correct, so just in case, here's a 34-byte brute-force solution:
```
ＮθＦＮＦ⊕⌈×θ⊕ι⊞υ⟦↔⁻θ∕κ⊕ι⊕ικ⟧Ｉ⊟⌊υ/Ｉ⊟⌊υ
```
[Try it online!](https://tio.run/##hY5LC4JAFIX3/oqh1R2w0sxAWoVtWhQu2kULH1NenIeOM/79KaNAIugsv/PglHWuS5Vz5w6yteZkRcE0dHTr3ZQmMIWUkjcrNRNMGlZBypCjvMMZBeuh88nURDqKZLavwfrksit6xa1hcERpX@k9DlgxaH70vpFPmuvzVaZRGkjz3kCm2nEJhRVgx8rHnS1n/5LORYtwHcbJahNHceKFQRC4@cAf "Charcoal – Try It Online") Link is to verbose version of code. Very slow, so test case is limited to a denominator of `1000`. Explanation:
```
Ｎθ
```
Input the decimal.
```
ＦＮ
```
Loop over the possible denominators (except 0-indexed, so all of the references to the loop variable have to be incremented).
```
Ｆ⊕⌈×θ⊕ι
```
Loop until the nearest numerator above.
```
⊞υ⟦↔⁻θ∕κ⊕ι⊕ικ⟧
```
Save the fraction difference and the denominator and numerator.
```
Ｉ⊟⌊υ/Ｉ⊟⌊υ
```
Print the numerator and denominator of the fraction with the minimum difference.
[Answer]
# [Japt](https://github.com/ETHproductions/japt) v2.0a0 [`-g`](https://codegolf.meta.stackexchange.com/a/14339/), 15 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
mc ×õ ï ñ@ÎaXr÷
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=2.0a0&flags=LWc&code=bWMg1/Ug7yDxQM5hWHL3&input=WzEuMjEsIDhd)
```
mc ×õ ï ñ@ÎaXr÷     :Implicit input of array U
m                   :Map
 c                  :  Ceiling
   ×                :Reduce by multiplication
    õ               :Range [1,result]
      ï             :Cartesian product with itself
        ñ           :Sort by
         @          :Passing each pair X through the following function
          Î         :  First element of U
           a        :  Absolute difference with
            Xr÷     :  X reduced by division
                    :Implicit output of first pair
```
[Answer]
# [Zig 0.6.0](https://ziglang.org), 149 bytes
```
fn a(e:f64,m:f64)[2]f64{var n:f64=1;var d=n;var b=d;var c=b;while(d<m){if(n/d>e)d+=1 else n+=1;if(@fabs(n/d-e)<@fabs(b/c-e)){b=n;c=d;}}return.{b,c};}
```
[Try it](https://godbolt.org/z/9MYE8b)
Formatted:
```
fn a(e: f64, m: f64) [2]f64 {
    var n: f64 = 1;
    var d = n;
    var b = d;
    var c = b;
    while (d < m) {
        if (n / d > e) d += 1 else n += 1;
        if (@fabs(n / d - e) < @fabs(b / c - e)) {
            b = n;
            c = d;
        }
    }
    return .{ b, c };
}
```
Variable declarations are annoying.
[Answer]
# [Perl 5](https://www.perl.org/) `-p -MList::Util=min`, ~~65~~, 61 bytes
-4 bytes thanks to DomHastings
```
/ /;$_=min map abs($`-($-=.5+$_*$`)/$_)." $-/$_",1..$';s;.* ;
```
[Try it online!](https://tio.run/##VY5BasMwEEX3PsWQDthOKlkz9kRShG/QLrszOC60IHASE3lTSq9e1aWr7v6D/@Atb/dZ8kN8r3CoP9P0gUPAoS/Lr9xAE3DsL/EKl2mB6TVVeFYVql7LAcc9nusGx1rvANU2do@kNZYhBb2HkDNpJnBFq6kj8XyUVjwQF6wtOXbkgIwpjHbkW89WiIB5Yxax3bGV7c3mP/8JxnbOd9ZbuwnyfVvWeLumrOYlq@enmNbT6WWN82/4Dw "Perl 5 – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~61~~ 60 bytes
With a byte saved by Dominic van Essen.
```
function(x,d,n=round(1:d*x))c(m<-order((x-n/1:d)^2)[1],n[m])
```
[Try it online!](https://tio.run/##dY/BasQgEIbvfQqhFy1GndHJOGX7JMv2smmghzUQdiFvn6qhhUI7p@H/5vvRdZ/VadjnR7neP5eiNzvZ8rYujzJpeJ1eNmOu@nYalnX6WLXehuJrbN7RnOFiy/l2Mfusg0MiTmMkEqswGPWswKenWYNDsCq3YPRUg@ggAQmOFNsthD6NR@ihFyEM3Fxx4efi6BQPf3RgY4i@SegYMmbI3Tyk6LlpweW6CzJBfRN2qxYe6L8P/CbflZXFDgOnLImFuTVSh8lD5v0L "R – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Ċ×⁹p÷/ạ¥Þ⁸Ḣ
```
A dyadic Link accepting the decimal [evaluated as a float] on the left and the denominator limit on the right which yields a pair `[numerator, denominator]` representing the simplified fraction.
**[Try it online!](https://tio.run/##ATIAzf9qZWxsef//xIrDl@KBuXDDty/huqHCpcOe4oG44bii////My4xNDE1OTI2NTM1Of8xMg "Jelly – Try It Online")** Or see the [test-suite](https://tio.run/##ZcuxDcIwEADAPlNkAMvxv/359yyWSxqUgpYylCxAhwQSoqEnUGIWiRcxsZBoaE@69WoYtqW89@mQx8cm3bv5eXpd0jGP0zydS7p2eXcrJQTQCKqV2KhgNTggjz1Z8qoFrIiaQVBAFjCmitEC3npkgmUifg2J2PWW6kTzb79s2Il37JlrptjEDw "Jelly – Try It Online") (big denominator limit cases removed due to inefficiency.)
### How?
```
Ċ×⁹p÷/ạ¥Þ⁸Ḣ - Link: v, d
Ċ           - ceil (of the decimal value, v)
 ×⁹         - multiplied by chain's right argument (denominator limit, d)
   p        - Cartesian power (d) -> all pairs [[1,1],...,[1,d],[2,1],...,[Ċ×⁹,d]]
                  (note that any pair representing a non-simplified fraction is to
                   the right of its simplified form)
        Þ   - (stable) sort by:
       ¥    -   last two links as a dyad:
     /      -     reduce by:
    ÷       -       division (i.e. evaluate the fraction)
      ạ  ⁸  -     absolute difference with the chain's left argument (v)
          Ḣ - head
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 26 [bytes](https://github.com/abrudz/SBCS)
```
⌊.5+(⊃∘⍋1+|⍨⌊⊢-|⍨)∘÷∘⍳×1,⊣
```
[Try it online!](https://tio.run/##zZCxTsMwEIZ3P0XGhKZx7hzX9swCK@UFKqGyVIKpEqJMSBVKSQUDChMDLGwMFarESN7kXiSc0xZIGWAkiiLn/@67RP/gdNQ9OhuMTo7rc7rOE92h4g2qkvJLKmbjEDqUPzLoRuOqhAnDajmm6U21pGJBxeuF@EXjnK3ntrNSQogpf4qqMvTa1T2b0PHDjFnv@mPEOXseLuq19rdp/l2/vh7yl6mYs/T@omh6S/O7/sEuPw/39vs1JAjBMLAipHIGER8hbS4B7tvLF0UR7mzmRJpYcMqh0eC3IHKCWpusp7T2PqbbyUpLTWZdZpwxjaaFoOLhX93CNxMHNpgEPamFSiAD7bCnlXbxpheGCppMOqcxNdxakn5iz8FJ@GEjA0RpBCYGLFqwjdOMK2mgVWzMo0x4D7TK5LwxZLYVrzcxUK2q/SLtSSbBmg8 "APL (Dyalog Unicode) – Try It Online")
A dyadic tacit function that takes the decimal number on its left and the max denominator on its right, and gives a 2-element vector of `[denominator, numerator]`.
### How it works
```
⌊.5+(⊃∘⍋1+|⍨⌊⊢-|⍨)∘÷∘⍳×1,⊣  ⍝ Left: x, Right: d
                  ∘÷∘⍳      ⍝ v←[1, 1/2, ..., 1/d]
    (          |⍨)          ⍝ Remainder of x divided by each of v
          |⍨⌊⊢-             ⍝ Min distance from x to some integer multiple of v
        1+                  ⍝ Add 1 to treat close enough numbers as same
                            ⍝ Otherwise it gives something like 5/20 due to FP error
     ⊃∘⍋                    ⍝ D←The index of minimum (the optimal denominator)
                      ×1,⊣  ⍝ Exact fraction (D,Dx)
⌊.5+                        ⍝ Round both
```
[Answer]
# [Io](http://iolanguage.org/), 78 bytes
Port of Surculose Sputum's answer.
```
method(x,y,Range 1to(y)map(a,list((x-(b :=(a*x)round)/a)abs,b,a))min slice(1))
```
[Try it online!](https://tio.run/##dcxBDoIwEEDRvafocsZU7AyUtiZewrWboqBNoCWACZ4edWM0xvX7@SEtjdjtxXHp6umazjDLuzz4eKkFTQnu2PkevGzDOAHMG6ieMfj1jEO6xTNuPfpqlJX0iF2IYmzDqQZCXBrIMypIOy51rp0UxCj6IcSpjasGODNk2ZJ9glKfojJLLndsNJEUzN/GWpuizPXryOq//UyVKawrjDPmNdVvXB4 "Io – Try It Online")
# [Io](http://iolanguage.org/), 93 bytes
```
method(x,y,list(((q :=(r :=Range 0to(y)map(a,(x-(a*x)round/a)abs))indexOf(r min))*x)round,q))
```
[Try it online!](https://tio.run/##dcy9DoIwFIbh3avoeI6p2B4obU28BhNnlxpAm0D5NYGrRxg0GuPyLU@@19dzwQ5HdpmrfLjXGYx84qXvBwBoF4BumbMLt5yJoYYJK9eA4zDuwG1H7OpHyPYO3bVH9CHLx1OxXCofEF/MW8S5gDiSiVSWUhUry5kkZE3nw1CGTQEUaWnISLOAEJ8iIiNtbEkrKTkj@jZSSidprNYiif/2ExU6MTbRVus1qt44PwE "Io – Try It Online")
## Explanation (ungolfed):
```
method(x, y,                   // Take two operands
    r := Range 0 to(y) map(a,  // Map in range 0..y (set to r):
        (x-(a*x)round/a)abs    //     |x-round(a*x)/a|
    )                          //     (Aka find the appropriate numerator)
    q :=r indexOf(r min)       // Set q as the 0-index of the smallest number of r
    list((q*x)round,q)         // Output [numerator,denominator]
)                              // End function
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 60 bytes
```
(t=s=1;While[Denominator[s=Rationalize[#,1/t++]]<#2,j=s];j)&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n277X6PEttjW0Do8IzMnNdolNS8/NzMvsSS/KLrYNgioKD8vMSezKjVaWcdQv0RbOzbWRtlIJ8u2ONY6S1Ptf0BRZl6JQ3q0sZ6hiaGppZGZqbGppY6CoVHs//8A "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# Julia, 66 bytes
```
r(x,m)=minimum((n=Int(round(x*d));(abs(x-n/d),n//d)) for d=1:m)[2]
```
or
```
(x,m)->minimum((n=Int(round(x*d));(abs(x-n/d),n//d)) for d=1:m)[2]
```
[Answer]
# [R](https://www.r-project.org/), 75 bytes
```
function(x,d)c((n=rep(0:1,e=d)+(1:d*x)%/%1)[f<-order((x-n/1:d)^2)[1]],f%%d)
```
[Try it online!](https://tio.run/##fY3RSsQwEEXf/QpBColmk8wk00kW@yXLKtK0sCCtZFfo39e06EMr7Dzee@bcPLef47W73t77/NE2c/89tLfLOIhJJdkKMTS5@xL2CKprknwRcEzPk6xMBfLUvx7GnLosxHQYTGnkG8oTnM@qr6okN2YBGkEF@fhUG3rYNE6DB4pYk6OowK5XQAdrZGIktLz9gajtH7qwEA3ctWKBEM1Og5ohYICwuFaNM7wTWR1KHJEJQOHiKVv/GCRiXzsqW7iajL@D/K4VyO0pyz5Ez5G5jNFCeQOB5x8 "R – Try It Online")
Not a competitive answer as it's already beaten by [Kirill](https://codegolf.stackexchange.com/questions/207352/cleaning-up-decimal-numbers/207381#207381), but posting for fun anyway.
I didn't think of the `round()` function, so this approach rounds down & then up to generate a double-length list of candidate numerators, and then finds the index of the closest fraction. Since the index might be in the second (rounded-up) part of the list, the denominator is the index *mod* the length of the single-length list.
I think it's fair to conclude that the `round()` function indeed serves a useful role...
]