[Question]
  [
The cardinality of the set \$\mathbb Q\$ of rational numbers is known to be exactly the same as that of the set \$\mathbb Z\$ of integers. This means that it is possible to construct a bijection between these sets—a mapping such that each integer corresponds to exactly one rational number, and vice versa.
Provide such a bijection from \$\mathbb Z\$ to \$\mathbb Q\$. You may choose to use as the domain any contiguous subset of the integers instead of the whole of \$\mathbb Z\$. For example, you could instead choose a domain of \$\{\cdots,5,6,7\}\$ (integers \$\le 7\$) or \$\{0,1,2,\cdots\}\$ (like a 0-indexed infinite list), but not \$\{1,2,3,5,8,\cdots\}\$.
  
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 12 bytes
```
⌊d-‹Ė)1Ḟ:NY‹
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLijIpkLeKAucSWKTHhuJ46TlnigLkiLCIiLCIiXQ==)
A completely different tactic, using an alternating form of the [Calkin-Wilf](https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree#Breadth_first_traversal) sequence inspired by [Jordan's answer](https://codegolf.stackexchange.com/a/253718/100664). Append an `i` if outputting an infinite sequence is not allowed.
```
       Ḟ    # Generate a sequence...
      1     # Starting with 1
-----)      # Each value is the previous value n, put into the following...
⌊           # floor(n)
 d          # 2 * floor(n)
  -         # n - 2 * floor(n)
   ‹        # n - 2 * floor(n) - 1
    Ė       # 1 / (n - 2 * floor(n) - 1)
         Y  # Interleave with
       :N   # The sequence negated
          ‹ # Decrement every term to add a 0
```
[Answer]
# [J](https://www.jsoftware.com), ~~21~~ 16 bytes
```
**|1&(1%+-2*|)-~
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70wa8FOJT0F9TQFWysFdR0FAwUrINbVU3AO8nFbWlqSpmuxQUurxlBNw1BVW9dIq0ZTtw4ivFmTKzU5I19BQydNU81OIdPK1KACIrNgAYQGAA)
Uses the Calkin-Wilf generator. The domain is full \$\mathbb{Z}\$. `f(n) = CalkinWilf(n)` and `f(-n) = -f(n)` for `n>=0`. The input must be given as a bigint.
```
**|1&(1%+-2*|)-~    input: n, bigint
  |                 abs(n)
    &(       )-~    repeat ^ times, starting from bigint zero:
   1& 1%+-2*|       1/((1 + x) - 2 * frac(x))
**                  multiply sign(n) to ^
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~67~~ 50 bytes
-17 bytes thanks to [Bubbler](https://codegolf.stackexchange.com/users/78410/bubbler)
This is the [Calkin-Wilf sequence](https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree#Breadth_first_traversal) but mapped to the negative rationals for negative inputs, plus \$f(0) = 0\$.
```
F=->n{n<1?0:(m=F[n-1]
n%2>0?1r/(1-2*m.ceil+m):-m)}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGCpaUlaboWN43cbHXt8qrzbAztDaw0cm3dovN0DWO58lSN7AzsDYv0NQx1jbRy9ZJTM3O0czWtdHM1a6E69TUM9PQMDQw09VITkzMUUvK5FBQKSkuKFZSUq-MNaxVs7RSUq92i4w1ja5W4UvNSINpgFgMA)
## Ruby, 40 bytes
This is the Calkin-Wilf sequence for positive inputs/outputs only.
```
F=->n{n<2?1:1r/(2*(m=F[n-1]).floor+1-m)}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGCpaUlaboWNzXcbHXt8qrzbIzsDa0Mi_Q1jLQ0cm3dovN0DWM19dJy8vOLtA11czVroeoVNQz19AwNDDT1UhOTMxRS8rkUFApKS4oV3KLjDWO5UvNSIAphFgAA)
[Answer]
# [R](https://www.r-project.org), ~~71~~ 66 bytes
```
f=\(x,n=abs(x))`if`(n,c(sum(m<-f(n-1))-2*m[2]%%m[1],x/n*m[1]),1:0)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=PY1BCoMwEEXvEhBmZILGbkpoNl4jCqahAcGkwSjYs3STLrrpjXqbUipd_bd5798fc37NZhmvwUxJPdfF8eO7daqDjYIy5wQb4jC6AQJZSKsHf-IOAheIvCm9bvqi8Fr0tFWh_AKSkDXupYM1CyQT43QDLmopavq_oW6koJ7SJSoLrGLEusBwV3P-7Qc)
Outputs rational numbers represented as vectors of (*denominator*, *numerator*).
---
# [R](https://www.r-project.org), 67 bytes
```
\(x,y=rle(intToBits(2*abs(x)+1))$l)sign(x)*head(c(y[1]-1,y[-1]),-1)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=fZAxbsMgFIbVqQqnQEkHSLBqMlWRWNordEujiDo4IFNswYsbq8pJuqRDD9WepuA4SqYuwM_3v__B-_zyx9-b2wkuagdrqNellwUud64AU7t02yoPocfG7dQGJ0PPvGq8CsqB7CXUOJi3xqqLw9Unhq7DBSrFOZ-0TAu-yFkl8gWnH2hkSmKV24ImLU16pEVB2iVfTXVcZno5X7F0ohFVZ1QlVCVUDaiMIOPRyqooD1jZoHAjA6icpHI2vh8P5gNCvn-ltOJ7B2X28PP0QvasE94qYhw8148GAplP5WsgezrjlN5ZGszWRTXVSm5IQbqYlXHWpa6UZZwOUSvpggiyaWxHMh4_mbNzN4oiQxP8bkD_MzoJZKiPdnY9STo0OR5P-x8)
Outputs rational numbers represented as [continued fractions](https://en.wikipedia.org/wiki/Continued_fraction).  
Works by calculating element i of the [Calkin–Wilf sequence](https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree) using the [run-length encoding of the binary representation](https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree#Breadth_first_traversal) of i.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 19 bytes
```
›"ƛȧ‹*[½₍⌊⌈Uvx∑;÷ȧ"
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJBIiwiIiwi4oC6XCLGm8in4oC5KlvCveKCjeKMiuKMiFV2eOKIkTvDt8inXCIiLCIiLCItNFxuLTNcbi0yXG4tMVxuMFxuMVxuMlxuM1xuNCJd)
A mess. Uses the Stern-Brocot sequence and works in theory...
Given an integer, outputs a pair of integers.
```
›"                  # [n, n+1]
  ƛ            ;    # Map to...
      [             # If ....
   ȧ‹               # |a| - 1
     *              # * a
       ½            # Then take a/2
        ₍⌊⌈         # [floor(a/2), ceil(a/2)
           U        # Uniquify (if even, just n/2)
            vx      # Recurse on each
              ∑     # Sum
                ÷ȧ" # Take the absolute value of the second.
```
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), ~~69~~ 67 bytes
-2 bytes from [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)
```
f=lambda i,m=1,n=1:i>3and f(i//2,m+i%2*n,n+~i%2*m)or(-~i*2%3*m-m,n)
```
[Try it online!](https://tio.run/##JYxBCoMwEEXXeopshBkdq9FNEdK7RKLtQDNK4sJuvHqa0t3778PbP8drk/G@h5RW87Z@dlYxeaNJjJ74MVpxagXuuoF8w9VQC0lz/cDjFqC9uB6qsfatJ8EUlVFxOQDLdQuKFYsKVp4LaNJ9j1NZnCbXsCz2wHIA08ySN51ZxZt1DjL9v/ciEBHTFw "Python 3.8 (pre-release) – Try It Online")
Maps \$\{1,2,\ldots,\}\$. Represents fractions as pairs of integers; \$0\$ represented as \$(0,1)\$.
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), 44 bytes
```
f(n)=if(n<0,-f(-n),n,(1+f(n\2)^q=n%2*2-1)^q)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728ILEoMz69YMGCpaUlaboWN3XSNPI0bTOBpI2Bjm6ahm6epk6ejoahNlAkxkgzrtA2T9VIy0jXEMjUhOpRS8sv0shTsFXQNTXQUQDhgqLMvBKgkJKCrh2QAJmpCVUNswkA)
Using [the Calkin-Wilf sequence](https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree) like other answers.
---
## [PARI/GP](https://pari.math.u-bordeaux.fr), 47 bytes
```
n->t=0;[t=(1+t^q--)^q|q<-2*binary(n)];t*sign(n)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728ILEoMz69YMHiNAXbpaUlaboWN_XzdO1KbA2so0tsNQy1S-IKdXU14wprCm10jbSSMvMSiyo18jRjrUu0ijPT84BMqDa1tPwijTwFWwVdUwMdBRAuKMrMKwEKKSno2gGJNKBaTajqBQsgNAA)
Starting from \$t=0\$. For each binary digit of the input \$n\$, take \$t=t+1\$ if the digit is \$1\$, and \$t=\frac{t}{t+1}\$ if the digit is \$0\$. Finally multiply the result with \$\operatorname{sign}(n)\$.
---
## [PARI/GP](https://pari.math.u-bordeaux.fr), 47 bytes
```
n->for(i=!t=0,abs(n),t=1/(1-t+t\1*2));t*sign(n)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728ILEoMz69YMHiNAXbpaUlaboWN_XzdO3S8os0Mm0VS2wNdBKTijXyNHVKbA31NQx1S7RLYgy1jDQ1rUu0ijPT84BSUG1qID15CrYKuqYGOgogXFCUmVcCFFJS0LUDEmlAtZpQ1QsWQGgA)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 37 bytes
```
ＮθＦ²⊞υιＦ↔θ⊞υ⁻Σ…⮌υ²⊗﹪§υ±²↨υ⁰‹θ⁰⪫⮌…⮌υ²/
```
[Try it online!](https://tio.run/##dY1LDoIwEIb3nKJxNU0gIltWiBuMEKIn4DEKSWmhZYievhYTdOUkM4tv/kfTVbpRlbA2kyPNBQ01aph47N2VZhBxVpLpgHzWbyypjRI0o1P9vnkvycCNBkhfjcC0UyNccUFtEIj7LHJ7UlQLbCFXLQkFyZzJFp@ru8BH5QIj7lTHarX4LOTrxF6peznDBY2B6UM3dFa9/Hb8a3Vnt985k7XBIQxtsIg3 "Charcoal – Try It Online") Link is to verbose version of code. Explanation: Port of my JavaScript answer to [Output the nth rational number according to the Stern-Brocot sequence](https://codegolf.stackexchange.com/q/90232/).
```
Ｎθ
```
Input `n`.
```
Ｆ²⊞υι
```
Start with `0/1`.
```
Ｆ↔θ
```
Repeat `|n|` times.
```
⊞υ⁻Σ…⮌υ²⊗﹪§υ±²↨υ⁰
```
Calculate the next term of the sequence.
```
‹θ⁰
```
Output a `-` if the input was negative.
```
⪫⮌…⮌υ²/
```
Output the last two terms of the sequence, joined with `/`.
Alternative implementation, also 37 bytes:
```
Ｎθ≔¹η≔⁰ζＦ↔θ«≔⁻⁺ζη⊗﹪ζηε≔ηζ≔εη»‹θ⁰Ｉζ/Ｉη
```
[Try it online!](https://tio.run/##VY3LCsIwEEXX9itCVxNIMV13VXQjWOkv9BFtISY2DxcRvz2OxFLcDNwz9zFMnRl0J2M8qYd3F3/vhYGFVllt7XxTUDIybYozElBdtSFQ91ZL7wS6KXllu5@lmZW30Eo84Ztl5Kh9L8UIjR691IlS5AKb1tSUilcp0uo7a82sHJyFtbAwwinChA6ddRA2ne/z/x9uVDEWJeexeMoP "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input `n`.
```
≔¹η≔⁰ζ
```
Start with `0/1`.
```
Ｆ↔θ«
```
Repeat `|n|` times.
```
≔⁻⁺ζη⊗﹪ζηε
```
Calculate the next term of the sequence.
```
≔ηζ≔εη
```
Shuffle the terms into the desired variables.
```
»‹θ⁰
```
Output a `-` if the input was negative.
```
Ｉζ/Ｉη
```
Output the current two terms, separated by `/`.
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 61 bytes
```
\d+
$*#/1
+`#(?=1*/(1+))(\1*)(1*)/\3(1+)
$1/$2$4
^/
0/
1+
$.&
```
[Try it online!](https://tio.run/##Fcs5DgIxEETR/F9jDPKixu6xZwsQIZew0CBBQEKAuL8xQVVQpfd5fl/vezvY697qI2D8EJWwD/ZyVh@tBudsVe9sT6z5P2A0mtEUbpEU0a5Ox9ZEU6IXsiErsiAzMiEFycjYPxLKSKYwMbOwstFFlz8 "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation: Another port of my JavaScript answer to [Output the nth rational number according to the Stern-Brocot sequence](https://codegolf.stackexchange.com/q/90232/).
```
\d+
$*#/1
```
Convert the absolute value of `n` to unary using `#`s, then append `/1` representing `0/1` in unary.
```
+`#(?=1*/(1+))(\1*)(1*)/\3(1+)
```
Match and consume one `#` each time, so that the replacement happens `n` times; look ahead and capture `b` as `$1` so that `a-a%b` and `a%b` can be calculated as `$2` and `$3`, and therefore also `b-a%b` as `$4`.
```
$1/$2$4
```
Replace `a` with `b` and `b` with `a-a%b+b-a%b` i.e. `a+b-a%b*2`.
```
^/
0/
```
Special case `0/`, since `0` in unary is the empty string.
```
1+
$.&
```
Convert to decimal.
[Answer]
# [Python](https://docs.python.org/3.8/), 207 bytes.
\$ \text{207 bytes, it can be golfed much more.} \$
Golfed version. [Try it online!](https://tio.run/##VY7BDoIwEETvfMXExKRVSShejBGO/AfRFpvYbbPUgyF8OwoSwbnt7L6ZDa9493Q8BR4Gw97BcH2N1lML64LniGo2ULeokps2aATJc4KPrAHhAvWdRrGOTyZkk6EfrV5WDsWIpkr@rInfIkeJbDlc5VRCHSAUUuQ7S1E4iT2cXBL@K1Zk6gbjGRaWwDU1WqhMzW8HHrPMprM9ihJdI6zsN3J4Aw)
```
from fractions import Fraction as F
def g(n):
    if n < 1:
        return 0
    else:
        m = g(n-1)
        if n % 2 > 0:
            return F(1, (1 - 2*int(m) + m))
        else:
            return -m
```
Ungolfed version. [Try it online!](https://tio.run/##VY7NCsIwEITveYpBEBK10NSLiO2x71E00YD5YRsPUvrs0dZq69x2duebDc94825/CJSSJm@hqTlH410LY4OniHoyGLsojZo7cWR4y2g4nCA/0yBS8UEO@Wioe6vmlUU5RDMpftaYX6NAhXw@XHC@xVzuwCUyFBvjIrcCW1gxg/6bFoDMMqY9wcA4UOOuistcTu8HGmB61ZkeZYWu5kb0K8FSegE)
```
from fractions import Fraction
def F(n):
    if n < 1:
        return 0
    else:
        m = F(n-1)
        if n % 2 > 0:
            return Fraction(1, (1 - 2*int(m) + m))
        else:
            return -m
for i in range(101):
    print(f"{i} => {F(i)}")
```
]