[Question]
  [
We are all familiar with the famous [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number), that starts with `0` and `1`, and each element is the sum of the previous two. Here are the first few terms (OEIS [A000045](http://oeis.org/A000045)):
```
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584
```
---
Given a *positive integer*, return the closest number of the Fibonacci sequence, under these rules:
* The *closest Fibonacci number* is defined as the Fibonacci number with the smallest absolute difference with the given integer. For example, `34` is the closest Fibonacci number to `30`, because `|34 - 30| = 4`, which is smaller than the second closest one, `21`, for which `|21 - 30| = 9`.
* If the given integer belongs to the Fibonacci sequence, the closest Fibonacci number is exactly itself. For example, the closest Fibonacci number to `13` is exactly `13`.
* In case of a tie, you may choose to output either one of the Fibonacci numbers that are both closest to the input or just output them both. For instance, if the input is `17`, all of the following are valid: `21`, `13` or `21, 13`. In case you return them both, please mention the format.
[Default Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) apply. You can take input and provide output through [any standard method](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods). Your program / function must only handle values up to 108.
---
# Test Cases
```
Input -> Output
1    -> 1
3    -> 3
4    -> 3 or 5 or 3, 5
6    -> 5
7    -> 8
11   -> 13
17   -> 13 or 21 or 13, 21
63   -> 55
101  -> 89
377  -> 377
467  -> 377
500  -> 610
1399 -> 1597
```
---
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes *in every language* wins!
  
[Answer]
# [Python 2](https://docs.python.org/2/), 43 bytes
```
f=lambda n,a=0,b=1:a*(2*n<a+b)or f(n,b,a+b)
```
[Try it online!](https://tio.run/##dZAxa8MwEIV3/4ojEJAapdihU1oP6VDo0KlDCyGEsy0lwsrZyDZNfr0rKQ52A/Vine7dd3qvvrTHilZ9r1KDp6xAIIFpLLI0WeMDWz3QCy4yXllQjEQmfNEXUoHStmn3SmcNK/k6AjDpdud@bvjZDbuTckN70AQW6SAHldM9Yl1LKhjyUKPbFbiusrLtLIGJorf3109Ip1ueYh5FfnN@lHm5J4lWXntMCaBAzw3qkyxuTQ9gxCeNQodbQAe8Fy8hSL2HvO3QTCgnTcy/SJTycsupXHtIuSTOx5GB7zt3kJE@mPz7pHRK8LrRqeoo9xY/Nt9pErsvWPXp0phuEvqLZAjZreyMf8k/YQWRVkBVO2ivcwC11dTC7MtWdACk5kdamDdh3byZwRyYj1QMCIDMSizdWZpGXhkDYWMM5JW1Mm9n3tD1esvOQrEz54F4nhpI4oTvoqln3v8C "Python 2 ‚Äì Try It Online")
Iterates through pairs of consecutive Fibonacci numbers `(a,b)` until it reaches one where the input `n` is less than their midpoint `(a+b)/2`, then returns `a`.
Written as a program (47 bytes):
```
n=input()
a=b=1
while 2*n>a+b:a,b=b,a+b
print a
```
[Same length](https://tio.run/##dZAxa8MwEIV3/4ojEJAapbFDpxQP6VDo0KlDCyEEyZYSYeVsZJsmv96VZAe7gXixpHv33b1XXZtTieuuU6nhZ5FzQMbTmIk02YjVeoVPRCw5LS0ogkwwvhC0y6UCpW3dHJQWNSnoJgIw6W7vfq751TW7k3JNB9AIluNRDiqne@ZVJTEnnIY7d7MC192sbFqLYKLo/ePtC9LplJeYRpGfnJ1kVhxQciv7GlEMMNAzw/VZ5reiBxCkk0KuwytwB7wXLyFIvYesabmZUM4aid@IFfJ6y6nYeEixRErHloHvK3eQkT6Y/L9SOiV43ehUtZh5i5/bnzSJ3Res@nRxTDcJ9UUyhOxGtsZv8iCsINIKsGwGbd8HUFmNDcy@bYlH4Fj/SgvzOoyb1zOYA/GRsgEBIKzkhTtLU8ueMRC2xkBWWiuzZuYN9c87cmGKXCgNxMvUQBIndB9NPdPuDw):
```
f=lambda n,a=0,b=1:b/2/n*(b-a)or f(n,b,a+b)
```
[Answer]
# [Neim](https://github.com/okx-code/Neim), 5 bytes
```
fùêñùïñSùïî
```
Explanation:
```
f       Push infinite Fibonacci list
 ùêñ                     93
  ùïñ     Select the first ^ elements
        This is the maximum amount of elements we can get before the values overflow
        which means the largest value we support is 7,540,113,804,746,346,429
   Sùïî   Closest value to the input in the list
```
In the newest version of Neim, this can be golfed to 3 bytes:
```
fSùïî
```
As infinite lists have been reworked to only go up to their maximum value.
[Try it online!](https://tio.run/##y0vNzP3/P@3D3AnTPsydOi0YSEz5/9/QHAA "Neim ‚Äì Try It Online")
[Answer]
# [Python](https://docs.python.org/3/), ~~55~~ 52 bytes
```
f=lambda x,a=1,b=1:[a,b][b-x<x-a]*(b>x)or f(x,b,a+b)
```
[Try it online!](https://tio.run/##FcqxDoIwFEDR/X3FG6mWhCdIAxE3XF3cCEOrJZAgNKXE8vUVxntzzOb6ebqE0FWj/KqPRM9lRVxVVDaSq7ZRsb/5WLanSN09my12keeKy7NiwdmtBMRfP4waX3bVRyEaO0xud8NkVhcxBtq/tXFYPx@1tbM9lJHLEghSyCAHAURAAvIUKNmnEJDlAq5JApQWxR8 "Python 2 ‚Äì Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~70~~ ~~67~~ ~~64~~ ~~62~~ 60 bytes
*-2 bytes thanks to djhurio!*
*-2 more bytes thanks to djhurio*
(boy can he golf!)
```
F=1:0;while(F<1e8)F=c(F[1]+F[2],F);F[order((F-scan())^2)][1]
```
Since we only have to handle values up to `10^8`, this works.
[Try it online!](https://tio.run/##K/r/383W0MrAOs@2ODkxT0PTujwjMydVI8/OTdPNNlnDLdowVtst2ihWx03T2i06vygltUhDw003TzPOSDMWKPnf2Nz8PwA "R ‚Äì Try It Online")
Reads `n` from stdin. the `while` loop generates the fibonacci numbers in `F` (in decreasing order); in the event of a tie, the larger is returned. This will trigger a number of warnings because `while(F<1e8)` only evaluates the statement for the first element of `F` with a warning
Originally I used `F[which.min(abs(F-n))]`, the naive approach, but @djhurio suggested `(F-n)^2` since the ordering will be equivalent, and `order` instead of `which.min`. `order` returns a permutation of indices to put its input into increasing order, though, so we need `[1]` at the end to get only the first value.
### faster version:
```
F=1:0;n=scan();while(n>F)F=c(sum(F),F[1]);F[order((F-n)^2)][‚Äå‚Äã1]
```
only stores the last two fibonacci numbers
[Answer]
## JavaScript (ES6), 41 bytes
```
f=(n,x=0,y=1)=>y<n?f(n,y,x+y):y-n>n-x?x:y
```
```
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0
```
Rounds up by preference.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~9~~ 7 bytes
-2 bytes thanks to @EriktheOutgolfer
```
‚ÄòR√Ü·∏û·∫°√ê·πÇ
```
[Try it online!](https://tio.run/##y0rNyan8//9Rw4ygw20Pd8x7uGvh4QkPdzb9P9x@dNLDnTNUHjWtifz/P9pQh8tYh8tEh8tMh8tch8sQyDcE0mZAQUMDkKQ5kGdiBiRMDQyAYsaWlrEA "Jelly ‚Äì Try It Online")
Golfing tips welcome :). Takes an int for input and returns an int-list.
```
            ' input -> 4
‚Äò           ' increment -> 5
 R          ' range -> [1,2,3,4,5]
  √Ü·∏û        ' fibonacci (vectorizes) -> [1,1,2,3,5,8]
     √ê·πÇ     ' filter and keep the minimum by:
    ·∫°       ' absolute difference -> [3,3,2,1,1,4]
            ' after filter -> [3,5]
```
[Answer]
# Mathematica, 30 bytes
```
Array[Fibonacci,2#]~Nearest~#&
```
[Try it online!](https://tio.run/##y00sychMLv6fZvvfsagosTLaLTMpPy8xOTlTx0g5ts4vNbEotbikTlntf0BRZl6JgkOagoOhsaXlfwA "Mathics ‚Äì Try It Online")
[Answer]
# x86-64 Machine Code, 24 bytes
```
31 C0 8D 50 01 92 01 C2 39 FA 7E F9 89 D1 29 FA 29 C7 39 D7 0F 4F C1 C3
```
The above bytes of code define a function in 64-bit x86 machine code that finds the closest Fibonacci number to the specified input value, `n`.
The function follows the [System V AMD64 calling convention](https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI) (standard on Gnu/Unix systems), such that the sole parameter (`n`) is passed in the `EDI` register, and the result is returned in the `EAX` register.
**Ungolfed assembly mnemonics:**
```
; unsigned int ClosestFibonacci(unsigned int n);
    xor    eax, eax        ; initialize EAX to 0
    lea    edx, [rax+1]    ; initialize EDX to 1
  CalcFib:
    xchg   eax, edx        ; swap EAX and EDX
    add    edx, eax        ; EDX += EAX
    cmp    edx, edi
    jle    CalcFib         ; keep looping until we find a Fibonacci number > n
    mov    ecx, edx        ; temporary copy of EDX, because we 'bout to clobber it
    sub    edx, edi
    sub    edi, eax
    cmp    edi, edx
    cmovg  eax, ecx        ; EAX = (n-EAX > EDX-n) ? EDX : EAX
    ret
```
**[Try it online!](https://tio.run/##dVDRaoMwFH33K4JjoFJHbGxU3ApD508se5BobaCNQ@MIjP363E3b9SkJhJOce87l3MvjgfN15aOcFeLHdkI6p9XY9e8f6AX5TJOE6QoznddM7wAx/IvtFStAUjDdvDKdvQHCO4dbQ2174w1W2VVXA@KG6RRuZfzEL71ZtUpwtMhZDLLvUBBBgkbLMPinQkhyS1WuD0Ly09L16HlWnRifjnvPE1KhcytkYB7tNPDNZZQoMp@v0Pv2EJzPCcqHwL@2D5KQqXjP1GPHpL9BdzYsbWpiVROHOrWqU4eaWtXUoc6s6syhThxjuuZM7O0TV39qXwx1bSbBjkDYufnMHsnwju1Tu8PwdscOY6vD8I45SFHYBzEF8FxMU6@WSSJcej/rLz@c2mFe4zNN/wA "C (gcc) ‚Äì Try It Online")**
The code basically divides up into three parts:
* The first part is very simple: it just initializes our working registers. `EAX` is set to 0, and `EDX` is set to 1.
* The next part is a loop that iteratively calculates the Fibonacci numbers on either side of the input value, `n`. This code is based on [my previous implementation of Fibonacci with subtraction](https://codegolf.stackexchange.com/questions/131513/fibtraction-fibonacci-but-with-subtraction/131706#131706), but‚Ä¶um‚Ä¶isn't with subtraction. :-) In particular, it uses the same trick of calculating the Fibonacci number using two variables‚Äîhere, these are the `EAX` and `EDX` registers. This approach is *extremely* convenient here, because it gives us adjacent Fibonacci numbers. The candidate potentially *less than* `n` is held in `EAX`, while the candidate potentially *greater than* `n` is held in `EDX`. I'm quite proud of how tight I was able to make the code inside of this loop (and even more tickled that I re-discovered it independently, and only later realized how similar it was to the subtraction answer linked above).
* Once we have the candidate Fibonacci values available in `EAX` and `EDX`, it is a conceptually simple matter of figuring out which one is closer (in terms of absolute value) to `n`. Actually taking an absolute value would cost *way* too many bytes, so we just do a series of subtractions. The comment out to the right of the penultimate conditional-move instruction aptly explains the logic here. This either moves `EDX` into `EAX`, or leaves `EAX` alone, so that when the function `RET`urns, the closest Fibonacci number is returned in `EAX`.
In the case of a tie, the *smaller* of the two candidate values is returned, since we've used `CMOVG` instead of `CMOVGE` to do the selection. It is a trivial change, if you'd prefer the other behavior. Returning *both* values is a non-starter, though; only one integer result, please!
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), ~~80~~ 74 bytes
```
param($n)for($a,$b=1,0;$a-lt$n;$a,$b=$b,($a+$b)){}($b,$a)[($b-$n-gt$n-$a)]
```
(Try it online! temporarily unresponsive)
Iterative solution. Takes input `$n`, sets `$a,$b` to be `1,0`, and then loops with Fibonacci until `$a` is larger than the input. At that point, we index into `($b,$a)` based on Boolean of whether the difference between the first element and `$n` is greater than between `$n` and the second element. That's left on the pipeline, output is implicit.
[Answer]
## JavaScript (ES6), 67 bytes
```
f=(n,k,y)=>(g=k=>x=k>1?g(--k)+g(--k):k)(k)>n?x+y>2*n?y:x:f(n,-~k,x)
```
### Test cases
```
f=(n,k,y)=>(g=k=>x=k>1?g(--k)+g(--k):k)(k)>n?x+y>2*n?y:x:f(n,-~k,x)
console.log(f(1   )) // -> 1
console.log(f(3   )) // -> 3
console.log(f(4   )) // -> 3 or 5 or 3, 5
console.log(f(6   )) // -> 5
console.log(f(7   )) // -> 8
console.log(f(11  )) // -> 13
console.log(f(17  )) // -> 13 or 21 or 13, 21
console.log(f(63  )) // -> 55
console.log(f(101 )) // -> 89
console.log(f(377 )) // -> 377
console.log(f(467 )) // -> 377
console.log(f(500 )) // -> 610
console.log(f(1399)) // -> 1597
```
[Answer]
# [JavaScript (Babel Node)](https://babeljs.io/), 41 bytes
```
f=(n,i=1,j=1)=>j<n?f(n,j,j+i):j-n>n-i?i:j
```
Based on [ovs's awesome Python answer](https://codegolf.stackexchange.com/a/133381/53880)
[Try it online!](https://tio.run/##DchRCoAgDADQ6zhygV@BpJ5FTWNDZmR0fet9Po5vHPmm68EUU2ko/ShzVqdEkzOanQHneZdQ/2HNC4FlFC9IgSzP3GX0VtbWT1WV2QDmBw "JavaScript (Babel Node) ‚Äì Try It Online")
### Ungolfed
```
f=(n,i=1,j=1)=> // Function f: n is the input, i and j are the most recent two Fibonacci numbers, initially 1, 1
 j<n?           // If j is still less than n
  f(n,j,j+i)    //  Try again with the next two Fibonacci numbers
 :              // Else:
  j-n>n-i?i:j   //  If n is closer to i, return i, else return j
```
[Answer]
# Python, 74 bytes
```
import math
r=5**.5
p=r/2+.5
lambda n:round(p**int(math.log(n*2*r,p)-1)/r)
```
[Try it online!](https://tio.run/##FY1BDoMgEADvvIIj0K26IhJNfEnbg42xmshCNvTQ11Od48xh0i9vkWxZp2c55vBeZkkjxy8tKhmzU1Zhzlt1xI8i0xqGpO@oa9ZiDylyllcWPDljKifSxHV7q1xZI0uSO8kHgoUOevCACOiht4DNKb2HrvfgmgbQDsNrFPIk8bUkWBVpXf4 "Python 3 ‚Äì Try It Online")
### How it works
For all *k* ‚â• 0, since |œÜ‚àí*k*/‚àö5| < 1/2, *F**k* = œÜ*k*/‚àö5 + œÜ‚àí*k*/‚àö5 = round(œÜ*k*/‚àö5). So the return value switches from *F**k* ‚àí 1 to *F**k* exactly where *k* = logœÜ(*n*‚ãÖ2‚àö5) ‚àí 1, or *n* = œÜ*k* + 1/(2‚àö5), which is within 1/4 of *F**k* + 1/2 = (*F**k* ‚àí 1 + *F**k*)/2.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 bytes
```
n√ÖFs.x
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/73CrW7Fexf//JgA "05AB1E ‚Äì Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~86~~ ~~85~~ ~~83~~ 76 bytes
```
f(n){n=n<2?:f(n-1)+f(n-2);}i,j,k;g(n){for(;k<n;j=k,k=f(i++));n=k-n<n-j?k:j;}
```
[Try it online!](https://tio.run/##ZdDRboMgFAbge5/CNFkCERKOKMyi64vsZnHRKBldTO8an92CZdiBFxzFT/zP6enY99s2IIPvpjNteTnbewq4cKXEap3ITLQaHRiuC1K6NWruNNHdgKaiwFiZTlPTGjpf9HlW6/bzNRmEs3uW2@t3mcxtQCdwD/Qjh/zt@9OcyIjAfvqPcE94IDwmVSDXJa/dwomtf76KvfD@ICIm0pP3QGRMAHz2Ixkk6UEGZGOV4Faw6cqXjpOTBff5XgImTQODZ8LmOIml05PyORpbw/xk8sdKpMzuxaxmbGcCWGB2L4nGm2Zvum72OR4B7QuH12x7AA "C (gcc) ‚Äì Try It Online")
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), ~~60~~ ~~57~~ 56 bytes
```
c->{int s=1,r=2;for(;c>r;s=r-s)r+=s;return c-s>r-c?r:s;}
```
[Try it online!](https://tio.run/##RY8xb8IwEIV3fsWNiepESaEgYoVOHTpUDKhT1cF1HGoaztH5goQQvz09Cm3fcL57lv2@25mDyULvcNd8jf3w0XkLtjMxwovxCKcJiCIbFr/1aDrYyYt8YN/l7YCWfcD8GfkVDR3XvSPDgQChHm22OnlkiHWpqL7XbaBE2xXpWFMWU7qroybHAyHYLK4os49URX0e9U/mjeUWfQi@gb0QJRsmj9u3dzC0jekN8KJ/rsjkzP5Ctbl2oU1KBVMFMwVzBQsFpcylnHMxy@JyuZBpNpfyUBTiTZfL9O/rX@V70yeY5rLKk7Gfa2ocuSbZHCM7SRm4qnqh4w7T6xLnyXkyfgM "Java (OpenJDK 8) ‚Äì Try It Online")
-1 byte thanks to @Neil
[Answer]
# [Python 3](https://docs.python.org/3/), 103 bytes
```
import math
def f(n):d=5**.5;p=.5+d/2;l=p**int(math.log(d*n,p))/d;L=[l,p*l];return round(L[2*n>sum(L)])
```
[Try it online!](https://tio.run/##FY5BDoMgEEXX7SkmroBOVVRq1NgTeAPjognSkigQioue3sLiv8n8@T8Z9wsfa@rz1LuzPsD@Cp@rXBUoYmgvR8FYLgY35uImi2rYRseYNoGkXL7ZN5HMoKO0kMM0zhs6ti2DX8PhDXh7GEmmuWLm@T12MtGFnsp60KANzByhRmgQHggtAo87j7OKapIbj20ZlfwygTcJIiFW6jYFHxGijDFed93SXy/Op/c0ZvdnhopoSs8/ "Python 3 ‚Äì Try It Online")
Sadly, had to use a def instead of lambda... There's probably much room for improvement here.
Original (incorrect) answer:
#  ~~[Python 3](https://docs.python.org/3/), 72 bytes~~
```
lambda n:r(p**r(math.log(d*n,p))/d)
import math
d=5**.5
p=.5+d/2
r=round
```
[Try it online!](https://tio.run/##FYnBDoMgEAXP5Ss2noBurVTRaEJ/pO3BhlhJFMiGHvr1FC/vZWbiL63Bt3kxz7zN@9vO4CfiUUri@5zWegsfbqXHKMTVCub2GCjBkZg1Wspas2hqfbbXGyND4ettXgKBA@fhoRBahA6hRxgQVGFVvi9SNUccCnV9Gd00xbXj@JrYKZLziTusLvcKF@6EyH8 "Python 3 ‚Äì Try It Online")
My first PPCG submission. Instead of either calculating Fibonacci numbers recursively or having them predefined, this code uses how the n-th Fibonacci number is the nearest integer to the n-th power of the golden ratio divided by the root of 5.
[Answer]
# Taxi, 2321 bytes
```
Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Trunkers.Go to Trunkers:n 1 l 1 l.0 is waiting at Starchild Numerology.1 is waiting at Starchild Numerology.Go to Starchild Numerology:w 1 l 2 l.Pickup a passenger going to Bird's Bench.Pickup a passenger going to Cyclone.Go to Cyclone:w 1 r 4 l.[a]Pickup a passenger going to Rob's Rest.Pickup a passenger going to Magic Eight.Go to Bird's Bench:n 1 r 2 r 1 l.Go to Rob's Rest:n.Go to Trunkers:s 1 l 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:w 2 r.Pickup a passenger going to Trunkers.Pickup a passenger going to Magic Eight.Go to Zoom Zoom:n.Go to Trunkers:w 3 l.Go to Magic Eight:e 1 r.Switch to plan "b" if no one is waiting.Pickup a passenger going to Firemouth Grill.Go to Firemouth Grill:w 1 r.Go to Rob's Rest:w 1 l 1 r 1 l 1 r 1 r.Pickup a passenger going to Cyclone.Go to Bird's Bench:s.Pickup a passenger going to Addition Alley.Go to Cyclone:n 1 r 1 l 2 l.Pickup a passenger going to Addition Alley.Pickup a passenger going to Bird's Bench.Go to Addition Alley:n 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l.Switch to plan "a".[b]Go to Trunkers:w 1 l.Pickup a passenger going to Cyclone.Go to Bird's Bench:w 1 l 1 r 1 l.Pickup a passenger going to Cyclone.Go to Rob's Rest:n.Pickup a passenger going to Cyclone.Go to Cyclone:s 1 l 1 l 2 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to What's The Difference.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 l.Pickup a passenger going to Magic Eight.Go to Sunny Skies Park:e 1 r 1 l.Go to Cyclone:n 1 l.Pickup a passenger going to Sunny Skies Park.Pickup a passenger going to What's The Difference.Go to Sunny Skies Park:n 1 r.Pickup a passenger going to What's The Difference.Go to What's The Difference:n 1 r 1 l.Pickup a passenger going to Magic Eight.Go to Magic Eight:e 1 r 2 l 2 r.Switch to plan "c" if no one is waiting.Go to Sunny Skies Park:w 1 l 1 r.Pickup a passenger going to The Babelfishery.Go to Cyclone:n 1 l.Switch to plan "d".[c]Go to Cyclone:w 1 l 2 r.[d]Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 2 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.
```
[Try it online!](https://tio.run/##tVZNj9owEP0rIy69RYXtZXNb2u2etkVLpUpFHIzjJCOMjWxHaX49deLAJg7kA3UPECX2zLw3b2ZsQ/7i6fQiwUhYSW3gZxwjZWEOc@D2p8pnsEK6z45A4Ei0ZiJhChKJIimtfqUMlmTHeIw6ZaoInDP/c6jPHvu9qUzsmdJnL/VrKGprHnwG1JATNKUFMbA2RNEUeQQ/sgNTksukCOZjNrkQ15Zq@osB6ktU0ScNSyZo2rvxa0G5FKyOWL9VQRR8sUE2ZNtn/iZ3Nswb06Y3yitJkMIzJqmpIzUBVilUlpOT1G149xwKP@VnwYZFu0VvMVbsaaz@SHmo/rqYc3i4cGsYhqzisM7R0LRcO3IiYLabAcYgJFi4jYLphfMdFTvIzKTwopCfY3lfnbTdHLe6qn5OSW1Lz/60PUWRJSMFPHHOCk8accEwVOKem9Hd4OK1rW3YxWTKTchuAPgqklmw2W07hTA0t3oyO374tZ20umk6xfeGG1JlnQlRwHqPTMOKqH3v5t8pMRZWOZC/YRwzZTmyOywc2KtrTp2JbeyTcE3aGE4t6T86HzcwicFyvS9Z6o6EdQZaWSfVkPVbgt4YbDdI5uOO5usHfVslH0lkm5Nuu@eew72Jtv/vajFmtjSuOEH30iMueTid5g@Pj/8A)  
[Try it online with comments!](https://tio.run/##vVffT9tIEH7nrxjxUmghEHJ3UtMDqVwvFQ@lqKl00kU8rO1xPIqzm9td100Rfzs3u/6BY0KcoKoSJHF2d@ab@b6Z2VjxnR4eJjAiGYFNkD8ESoowJJDZPEANYaoMGgtW@XWSi8zC7d4ErqRZkMYIguUQEmsXZnhyEqoIpyqNe8aKcIbfw0TIKfZCNT/576Q/GAz@@J0P7/FxCecw/vrh6pqfPypn/kaxm89xTCEOc@hDyv/avff2biicZQsQsBDGIFvUMFUkp@7YV0Z1KQJMYzIJ6mWvNNf@fmgqmx32dCZnqE1tp3weyvI849nz8ZMlkdIPhJxsAqPT81MQnMZR/7zPQZ0CGcgFb2K7wsLYCh0mlEZwnc1Rq1RNGWt/q10FknVrZabOurJ0STp6ZeASZZhs3vnXkimXWDktH70fDb9V0Y@UhgNiDvvvmMoLGB3Q4TugN28O4Y6BKo3@KxfSFxWw5y9ORD49B3Tc9wtNTHDvRCVuN0J7tLQ5hE9iSiH8TdPEVmE0fXkmNeesVFex49H4UD7hvtLOFvp5Nn9n2ytvx@D@VWruX9Ygz2HwGGPj7BCLUMYsXk4/Ly5SIWE/2AeKQSpg0A1t9oqavTgvaDUKyKIWlvfAZtJG3CTmKuMK@agpraG0vi70tYaLlU5Qvu/GwAr1Hcl9H0UcrpLwPk1x2eZQ1jg6661taPvaLFyunmfPZy@IvIm77FxtusW@r@fAdXTP7AWznCfEe@YopKk59zXL5ZsjSOSuzxZimmZc50xieYLl4seF9jsXmqRlmdw@lWRnU99E4Q6zoWVmtcRfkMjHNtApgHEm5RLGM0IDN0LPNu/@JxGWobmZ9YHiGDVHii86MmFv5V4eWUfAfyV7FQ9rDxYa2aHzPPFz3HLTTkDRcJodd0WevyCbK5BrwFV@jgr9Pwtfdpff7n75VW5BTJm2TYaOZUcfXjM3nowDJ@tiTrX7RLhhLFzVC7moV47cdVHCXYXyHv6EuwLovRsf3Ec02kxL6NJNvuW97Zl7YEtl7cAi1wAnoet/nxmxzslgEzV3v2dhH/qL8LWyOHRJ4LWIIvnKgll1Eh65NWN5zsGiCMGfDrhzltHnbi1Af8d@HZM29nUjyLLxKpkuKwMu3XyHakfdQHSJocg4GBVDovIm2ZArPTMelPcr0lwszUpgJD0U/Ibcw9mCAEtYc9S8FJaCmUS3P/OavtW4a/xi6K35ESFr4Tw89Adv3/4P)
Un-golfed with comments:
```
[ Find the Fibonacci number closest to the input ]
[ Inspired by: https://codegolf.stackexchange.com/q/133365 ]
[ n = STDIN ]
Go to Post Office: west 1st left 1st right 1st left.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 1st right.
Pickup a passenger going to Trunkers.
Go to Trunkers: north 1st left 1st left.
[ Initialize with F0=0 and F1=1 ]
0 is waiting at Starchild Numerology.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: west 1st left 2nd left.
Pickup a passenger going to Bird's Bench.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 1st right 4th left.
[ For (i = 1; n > F(i); i++) { Store F(i) at Rob's Rest and F(i-1) at Bird's Bench } ]
[a]
Pickup a passenger going to Rob's Rest.
Pickup a passenger going to Magic Eight.
Go to Bird's Bench: north 1st right 2nd right 1st left.
Go to Rob's Rest: north.
Go to Trunkers: south 1st left 1st left 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: west 2nd right.
Pickup a passenger going to Trunkers.
Pickup a passenger going to Magic Eight.
Go to Zoom Zoom: north.
Go to Trunkers: west 3rd left.
Go to Magic Eight: east 1st right.
Switch to plan "b" if no one is waiting.
[ n >= F(i) so iterate i ]
Pickup a passenger going to Firemouth Grill.
Go to Firemouth Grill: west 1st right.
Go to Rob's Rest: west 1st left 1st right 1st left 1st right 1st right.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: south.
Pickup a passenger going to Addition Alley.
Go to Cyclone: north 1st right 1st left 2nd left.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Bird's Bench.
Go to Addition Alley: north 2nd right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: north 1st left 1st left.
Switch to plan "a".
[b]
[ F(i) > n which means n >= F(i-1) and we need to figure out which is closer and print it]
Go to Trunkers: west 1st left.
Pickup a passenger going to Cyclone.
Go to Bird's Bench: west 1st left 1st right 1st left.
Pickup a passenger going to Cyclone.
Go to Rob's Rest: north.
Pickup a passenger going to Cyclone.
Go to Cyclone: south 1st left 1st left 2nd left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to What's The Difference.
[ Passengers:n, n, F(i-1) ]
Go to What's The Difference: north 1st left.
Pickup a passenger going to Magic Eight.
[ Passengers:n, n-F(i-1) ]
Go to Sunny Skies Park: east 1st right 1st left.
Go to Cyclone: north 1st left.
Pickup a passenger going to Sunny Skies Park.
Pickup a passenger going to What's The Difference.
[ Passengers: n-F(i-1), F(i-1), F(i) ]
Go to Sunny Skies Park: north 1st right.
Pickup a passenger going to What's The Difference.
[ Passengers: n-F(i-1), F(i), n ]
Go to What's The Difference: north 1st right 1st left.
[ Passengers: n-F(i-1), F(i)-n ]
Pickup a passenger going to Magic Eight.
Go to Magic Eight: east 1st right 2nd left 2nd right.
Switch to plan "c" if no one is waiting.
[ If no one was waiting, then {n-F(i-1)} < {F(i)-n} so we return F(i-1) ]
Go to Sunny Skies Park: west 1st left 1st right.
Pickup a passenger going to The Babelfishery.
Go to Cyclone: north 1st left.
Switch to plan "d".
[c]
[ Otherwise {n-F(i-1)} >= {F(i)-n} so we return F(i) ]
[ Note: If we didn't switch to plan c, we still pickup F(i) but F(i-1) will be the *first* passenger and we only pickup one at The Babelfishery ]
[ Note: Because of how Magic Eight works, we will always return F(i) in the event of a tie ]
Go to Cyclone: west 1st left 2nd right.
[d]
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery: south 1st left 2nd right 1st right.
Pickup a passenger going to Post Office.
Go to Post Office: north 1st left 1st right.
```
[Answer]
# [Python 3](https://docs.python.org/3/), 84 bytes
```
lambda k:min(map(f,range(2*k)),key=lambda n:abs(n-k))
f=lambda i:i<3or f(i-1)+f(i-2)
```
[Try it online!](https://tio.run/##NcpBDsIgEIXhfU8xy0GnC4qxCbEnURc0FpwgU4Ld9PSIia5e/i8v79tzFVPDdKsvl@aHg2gTCyaX0VNxEhYcDlEpiss@/S5i3fxG6Rt3/o9s@WLWAh651@r4nUFV34SBBa6awBCcCM4EI4Furce77SAXlg2ZICArVT8 "Python 3 ‚Äì Try It Online")
It may work, but it's certainly not fast...
Outputs `True` instead of `1`, but in Python these are equivalent.
[Answer]
# dc, 52 bytes
```
si1d[dsf+lfrdli>F]dsFxli-rlir-sdd[lild-pq]sDld<Dli+p
```
[Try it online!](https://tio.run/##S0n@b2xqgAn@F2capkSnFKdp56QVpeRk2rnFphS7VeRk6hblZBbpFqekROdk5qToFhTGFrvkpNi45GRqF/z/DwA "dc ‚Äì Try It Online")
Takes input at run using `?`
Edited to assume top of stack as input value, -1 byte.
Input is stored in register `i`. Then we put 1 and 1 on the stack to start the Fibonacci sequence, and we generate the sequence until we hit a value greater than `i`. At this point we have two numbers in the Fibonacci sequence on the stack: one that is less than or equal to `i`, and one that is greater than `i`. We convert these into their respective differences with `i` and then compare the differences. Finally we reconstruct the Fibonacci number by either adding or subtracting the difference to `i`.
Oops, I was loading two registers in the wrong order and then swapping them, wasting a byte.
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 63 56 bytes
-1 byte thanks to @Neil
-6 bytes thanks to @Nevay
```
n=>{int a=0,b=1;for(;b<n;a=b-a)b+=a;return n-a>b-n?b:a;}
```
[Try it online!](https://tio.run/##dc2xCsIwGATgvU@RscVWDC0tNU0dBCedHJz/xFgC9Q8mqSClzx7rbpbj4OM46QpprAqT0ziQ68d59WSJHME58poT58FrSd5G38kFNKbZnJwmlJ1Gn5M1ejLwgLyf106A73LBKXsYmzLRIQMuCsjEhgOzyk8WCRbQiwIPYg9sCSw5GnRmVNub1V6dNap0SGmW/YcyBlUM6hjQJjqJvpRNdFTVUaJl2/5sWcIX "C# (.NET Core) ‚Äì Try It Online")
[Answer]
## Q/KDB+, 51 bytes
```
{w where l=min l:abs neg[x]+w:{x,sum -2#x}/[x;0 1]}
```
[Answer]
# [Hexagony](https://github.com/m-ender/hexagony), 37 bytes
```
?\=-${&"*.2}+".|'=='.@.&}1.!_|._}$_}{
```
[Try it online!](https://tio.run/##y0itSEzPz6v8/98@xlZXpVpNSUvPqFZbSa9G3dZWXc9BT63WUE8xvkYvvlYlvrb6/39DQwA "Hexagony ‚Äì Try It Online")
ungolfed:
```
   ? \ = - 
  $ { & " * 
 . 2 } + " .
| ' = = ' . @
 . & } 1 . !
  _ | . _ }
   $ _ } { 
```
Broken down:
```
start:
? { 2 ' * //set up 2*target number
" ' 1     //initialize curr to 1
main loop:
} = +     //next + curr + last
" -       //test = next - (2*target)
branch: <= 0 -> continue; > 0 -> return
continue:
{ } = &   //last = curr
} = &     //curr = next
return:
{ } ! @   //print last
```
Like some other posters, I realized that when the midpoint of last and curr is greater than the target, the smaller of the two is the closest or tied for closest.
The midpoint is at (last + curr)/2. We can shorten that because next is already last + curr, and if we instead multiply our target integer by 2, we only need to check that (next - 2\*target) > 0, then return last.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 22 bytes
```
;I‚âú-.‚àß{0;1‚ü®t‚â°+‚ü©‚Å±h‚Ñï‚Üô.!}
```
[Try it online!](https://tio.run/##AXQAi/9icmFjaHlsb2cy/3t3IiAtPiAidz/ihrDigoLhuol94bWQ/ztJ4omcLS7iiKd7MDsx4p@odOKJoSvin6nigbFo4oSV4oaZLiF9//9bMSwzLDQsNiw3LDExLDE3LDYzLDEwMSwzNzcsNDY3LDUwMCwxMzk5XQ "Brachylog ‚Äì Try It Online")
Really all I've done here is paste together Fatalize's classic [Return the closest prime number](https://codegolf.stackexchange.com/a/182381/85334) solution and my own [Am I a Fibonacci Number?](https://codegolf.stackexchange.com/a/190930/85334) solution. Fortunately, the latter already operates on the output variable; unfortunately, it also includes a necessary cut which has to be isolated for +2 bytes, so the only choice point it discards is `‚Å±`, leaving `‚âú` intact.
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-g`](https://codegolf.meta.stackexchange.com/a/14339/), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
√≤!gM √±aU
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LWc&code=8iFnTSDxYVU&input=MTM5OQ)
[Answer]
# Java 7, ~~244~~ 234 Bytes
```
 String c(int c){for(int i=1;;i++){int r=f(i);int s=f(i-1);if(r>c && s<c){if(c-s == r-c)return ""+r+","+s;else if(s-c > r-c)return ""+r;return ""+s;}}} int f(int i){if(i<1)return 0;else if(i==1)return 1;else return f(i-2)+f(i-1);}
```
[Answer]
# [Pyke](https://github.com/muddyfish/PYKE), 6 bytes
```
}~F>R^
```
[Try it online!](https://tio.run/##K6jMTv3/v7bOzS4o7v9/Q0MA "Pyke ‚Äì Try It Online")
```
}      -    input*2
 ~F    -   infinite list of the fibonacci numbers
   >   -  ^[:input]
    R^ - closest_to(^, input)
```
[Answer]
# Common Lisp, 69 bytes
```
(lambda(n)(do((x 0 y)(y 1(+ x y)))((< n y)(if(<(- n x)(- y n))x y))))
```
[Try it online!](https://tio.run/##JUxLCoNAFLtKEErzKMI8tA6C9C62Igg6I62LmY1X19d2k4T8XvP0WQ/OMa4Y4xsJU8CVigo1GnioQj2aCurM9B5143F3Dlq1rWCItNnSb9iKvR5QPmC0Xwp7ov32y3PoGYRWZIJDFmYob5ZnESE7hK85jexYmk5ilBFE/g050g9P)
[Answer]
# [Perl¬†6](https://perl6.org), 38 bytes
```
{(0,1,*+*...*>$_).sort((*-$_).abs)[0]}
```
[Test it](https://tio.run/##RY7PboJAEMbv8xRzwMo/kXGBLSGaXnvvzZpGYU1IEAi7Nm2MT9ZbX4zu4lr3MPntfPN9M70Ymmw8S4GfWVQWcPrGp7KrBK7HixuHFPqBH0WRv3E@vEh2g3Jdf2F4f5DeNt5dR@14UUIqXGNTt0K63u9PVHang7t8r4Kl/m11zk43X1tVgFn1pscL6Jt9i8HkLeDYDTZmsUHXqdv@rEJHfPWiVKLy8AKItURzmlW9EP/1EGU/1K064nyWVCZiVs21fot5zBVwHQn10xMEzBKD5E6oz0hNYSGmkNl2CtzSMxBZOwPidzSOFZlK2rgiyJh1pkAx3aw5MM5vaziHJHtwGscTZxQDsTyfQtOc/wE "Perl 6 ‚Äì Try It Online")
```
{   # bare block lambda with implicit parameter ÔΩ¢$_ÔΩ£
  ( # generate Fibonacci sequence
    0, 1,  # seed the sequence
    * + *  # WhateverCode lambda that generates the rest of the values
    ...    # keep generating until
    * > $_ # it generates one larger than the original input
           # (that larger value is included in the sequence)
  ).sort(           # sort it by
    ( * - $_ ).abs  # the absolute difference to the original input
  )[0]              # get the first value from the sorted list
}
```
For a potential speed-up add `.tail(2)` before `.sort(‚Ä¶)`.
In the case of a tie, it will always return the smaller of the two values, because `sort` is a stable sort. (two values which would sort the same keep their order)
[Answer]
# Pyth, 19 bytes
```
JU2VQ=+Js>2J)hoaNQJ
```
[Try it here](http://pyth.herokuapp.com/?code=JU2VQ%3D%2BJs%3E2J%29hoaNQJ&input=467&debug=0)
### Explanation
```
JU2VQ=+Js>2J)hoaNQJ
JU2                  Set J = [0, 1].
   VQ=+Js>2J)        Add the next <input> Fibonacci numbers.
              oaNQJ  Sort them by distance to <input>.
             h       Take the first.
```
[Answer]
# Haskell, 48 bytes
```
(%)a b x|abs(b-x)>abs(a-x)=a|1>0=b%(a+b)$x
(1%2)
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/X0NVM1EhSaGiJjGpWCNJt0LTDsRIBDJsE2sM7Qxsk1Q1ErWTNFUq/ucmZuYp2CoUFGXmlSioKGgYqhppKpgaGPwHAA "Haskell ‚Äì Try It Online")
]