rationals.html 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>std/rationals</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">std/rationals</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <ul class="simple simple-toc" id="toc-list">
  49. <li>
  50. <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
  51. </li>
  52. <li>
  53. <details open>
  54. <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
  55. <ul class="simple simple-toc-section">
  56. <li><a class="reference" href="#Rational" title="Rational[T] = object
  57. num*, den*: T">Rational</a></li>
  58. </ul>
  59. </details>
  60. </li>
  61. <li>
  62. <details open>
  63. <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
  64. <ul class="simple simple-toc-section">
  65. <ul class="simple nested-toc-section">$
  66. <li><a class="reference" href="#%24%2CRational%5BT%5D" title="`$`[T](x: Rational[T]): string">`$`[T](x: Rational[T]): string</a></li>
  67. </ul>
  68. <ul class="simple nested-toc-section">*
  69. <li><a class="reference" href="#%2A%2CRational%5BT%5D%2CRational%5BT%5D" title="`*`[T](x, y: Rational[T]): Rational[T]">`*`[T](x, y: Rational[T]): Rational[T]</a></li>
  70. <li><a class="reference" href="#%2A%2CRational%5BT%5D%2CT" title="`*`[T](x: Rational[T]; y: T): Rational[T]">`*`[T](x: Rational[T]; y: T): Rational[T]</a></li>
  71. <li><a class="reference" href="#%2A%2CT%2CRational%5BT%5D" title="`*`[T](x: T; y: Rational[T]): Rational[T]">`*`[T](x: T; y: Rational[T]): Rational[T]</a></li>
  72. </ul>
  73. <ul class="simple nested-toc-section">*=
  74. <li><a class="reference" href="#%2A%3D%2CRational%5BT%5D%2CRational%5BT%5D" title="`*=`[T](x: var Rational[T]; y: Rational[T])">`*=`[T](x: var Rational[T]; y: Rational[T])</a></li>
  75. <li><a class="reference" href="#%2A%3D%2CRational%5BT%5D%2CT" title="`*=`[T](x: var Rational[T]; y: T)">`*=`[T](x: var Rational[T]; y: T)</a></li>
  76. </ul>
  77. <ul class="simple nested-toc-section">+
  78. <li><a class="reference" href="#%2B%2CRational%5BT%5D%2CRational%5BT%5D" title="`+`[T](x, y: Rational[T]): Rational[T]">`+`[T](x, y: Rational[T]): Rational[T]</a></li>
  79. <li><a class="reference" href="#%2B%2CRational%5BT%5D%2CT" title="`+`[T](x: Rational[T]; y: T): Rational[T]">`+`[T](x: Rational[T]; y: T): Rational[T]</a></li>
  80. <li><a class="reference" href="#%2B%2CT%2CRational%5BT%5D" title="`+`[T](x: T; y: Rational[T]): Rational[T]">`+`[T](x: T; y: Rational[T]): Rational[T]</a></li>
  81. </ul>
  82. <ul class="simple nested-toc-section">+=
  83. <li><a class="reference" href="#%2B%3D%2CRational%5BT%5D%2CRational%5BT%5D" title="`+=`[T](x: var Rational[T]; y: Rational[T])">`+=`[T](x: var Rational[T]; y: Rational[T])</a></li>
  84. <li><a class="reference" href="#%2B%3D%2CRational%5BT%5D%2CT" title="`+=`[T](x: var Rational[T]; y: T)">`+=`[T](x: var Rational[T]; y: T)</a></li>
  85. </ul>
  86. <ul class="simple nested-toc-section">-
  87. <li><a class="reference" href="#-%2CRational%5BT%5D%2CRational%5BT%5D" title="`-`[T](x, y: Rational[T]): Rational[T]">`-`[T](x, y: Rational[T]): Rational[T]</a></li>
  88. <li><a class="reference" href="#-%2CRational%5BT%5D" title="`-`[T](x: Rational[T]): Rational[T]">`-`[T](x: Rational[T]): Rational[T]</a></li>
  89. <li><a class="reference" href="#-%2CRational%5BT%5D%2CT" title="`-`[T](x: Rational[T]; y: T): Rational[T]">`-`[T](x: Rational[T]; y: T): Rational[T]</a></li>
  90. <li><a class="reference" href="#-%2CT%2CRational%5BT%5D" title="`-`[T](x: T; y: Rational[T]): Rational[T]">`-`[T](x: T; y: Rational[T]): Rational[T]</a></li>
  91. </ul>
  92. <ul class="simple nested-toc-section">-=
  93. <li><a class="reference" href="#-%3D%2CRational%5BT%5D%2CRational%5BT%5D" title="`-=`[T](x: var Rational[T]; y: Rational[T])">`-=`[T](x: var Rational[T]; y: Rational[T])</a></li>
  94. <li><a class="reference" href="#-%3D%2CRational%5BT%5D%2CT" title="`-=`[T](x: var Rational[T]; y: T)">`-=`[T](x: var Rational[T]; y: T)</a></li>
  95. </ul>
  96. <ul class="simple nested-toc-section">/
  97. <li><a class="reference" href="#%2F%2CRational%5BT%5D%2CRational%5BT%5D" title="`/`[T](x, y: Rational[T]): Rational[T]">`/`[T](x, y: Rational[T]): Rational[T]</a></li>
  98. <li><a class="reference" href="#%2F%2CRational%5BT%5D%2CT" title="`/`[T](x: Rational[T]; y: T): Rational[T]">`/`[T](x: Rational[T]; y: T): Rational[T]</a></li>
  99. <li><a class="reference" href="#%2F%2CT%2CRational%5BT%5D" title="`/`[T](x: T; y: Rational[T]): Rational[T]">`/`[T](x: T; y: Rational[T]): Rational[T]</a></li>
  100. </ul>
  101. <ul class="simple nested-toc-section">//
  102. <li><a class="reference" href="#%2F%2F%2CT%2CT" title="`//`[T](num, den: T): Rational[T]">`//`[T](num, den: T): Rational[T]</a></li>
  103. </ul>
  104. <ul class="simple nested-toc-section">/=
  105. <li><a class="reference" href="#%2F%3D%2CRational%5BT%5D%2CRational%5BT%5D" title="`/=`[T](x: var Rational[T]; y: Rational[T])">`/=`[T](x: var Rational[T]; y: Rational[T])</a></li>
  106. <li><a class="reference" href="#%2F%3D%2CRational%5BT%5D%2CT" title="`/=`[T](x: var Rational[T]; y: T)">`/=`[T](x: var Rational[T]; y: T)</a></li>
  107. </ul>
  108. <ul class="simple nested-toc-section"><
  109. <li><a class="reference" href="#%3C%2CRational%2CRational" title="`&lt;`(x, y: Rational): bool">`&lt;`(x, y: Rational): bool</a></li>
  110. </ul>
  111. <ul class="simple nested-toc-section"><=
  112. <li><a class="reference" href="#%3C%3D%2CRational%2CRational" title="`&lt;=`(x, y: Rational): bool">`&lt;=`(x, y: Rational): bool</a></li>
  113. </ul>
  114. <ul class="simple nested-toc-section">==
  115. <li><a class="reference" href="#%3D%3D%2CRational%2CRational" title="`==`(x, y: Rational): bool">`==`(x, y: Rational): bool</a></li>
  116. </ul>
  117. <ul class="simple nested-toc-section">^
  118. <li><a class="reference" href="#%5E%2CRational%5BT%3A%20SomeInteger%5D%2CT" title="`^`[T: SomeInteger](x: Rational[T]; y: T): Rational[T]">`^`[T: SomeInteger](x: Rational[T]; y: T): Rational[T]</a></li>
  119. </ul>
  120. <ul class="simple nested-toc-section">abs
  121. <li><a class="reference" href="#abs%2CRational%5BT%5D" title="abs[T](x: Rational[T]): Rational[T]">abs[T](x: Rational[T]): Rational[T]</a></li>
  122. </ul>
  123. <ul class="simple nested-toc-section">cmp
  124. <li><a class="reference" href="#cmp%2CRational%2CRational" title="cmp(x, y: Rational): int">cmp(x, y: Rational): int</a></li>
  125. </ul>
  126. <ul class="simple nested-toc-section">div
  127. <li><a class="reference" href="#div%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D" title="`div`[T: SomeInteger](x, y: Rational[T]): T">`div`[T: SomeInteger](x, y: Rational[T]): T</a></li>
  128. </ul>
  129. <ul class="simple nested-toc-section">floorDiv
  130. <li><a class="reference" href="#floorDiv%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D" title="floorDiv[T: SomeInteger](x, y: Rational[T]): T">floorDiv[T: SomeInteger](x, y: Rational[T]): T</a></li>
  131. </ul>
  132. <ul class="simple nested-toc-section">floorMod
  133. <li><a class="reference" href="#floorMod%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D" title="floorMod[T: SomeInteger](x, y: Rational[T]): Rational[T]">floorMod[T: SomeInteger](x, y: Rational[T]): Rational[T]</a></li>
  134. </ul>
  135. <ul class="simple nested-toc-section">hash
  136. <li><a class="reference" href="#hash%2CRational%5BT%5D" title="hash[T](x: Rational[T]): Hash">hash[T](x: Rational[T]): Hash</a></li>
  137. </ul>
  138. <ul class="simple nested-toc-section">initRational
  139. <li><a class="reference" href="#initRational%2CT%2CT" title="initRational[T: SomeInteger](num, den: T): Rational[T]">initRational[T: SomeInteger](num, den: T): Rational[T]</a></li>
  140. </ul>
  141. <ul class="simple nested-toc-section">mod
  142. <li><a class="reference" href="#mod%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D" title="`mod`[T: SomeInteger](x, y: Rational[T]): Rational[T]">`mod`[T: SomeInteger](x, y: Rational[T]): Rational[T]</a></li>
  143. </ul>
  144. <ul class="simple nested-toc-section">reciprocal
  145. <li><a class="reference" href="#reciprocal%2CRational%5BT%5D" title="reciprocal[T](x: Rational[T]): Rational[T]">reciprocal[T](x: Rational[T]): Rational[T]</a></li>
  146. </ul>
  147. <ul class="simple nested-toc-section">reduce
  148. <li><a class="reference" href="#reduce%2CRational%5BT%3A%20SomeInteger%5D" title="reduce[T: SomeInteger](x: var Rational[T])">reduce[T: SomeInteger](x: var Rational[T])</a></li>
  149. </ul>
  150. <ul class="simple nested-toc-section">toFloat
  151. <li><a class="reference" href="#toFloat%2CRational%5BT%5D" title="toFloat[T](x: Rational[T]): float">toFloat[T](x: Rational[T]): float</a></li>
  152. </ul>
  153. <ul class="simple nested-toc-section">toInt
  154. <li><a class="reference" href="#toInt%2CRational%5BT%5D" title="toInt[T](x: Rational[T]): int">toInt[T](x: Rational[T]): int</a></li>
  155. </ul>
  156. <ul class="simple nested-toc-section">toRational
  157. <li><a class="reference" href="#toRational%2Cfloat%2Cint" title="toRational(x: float; n: int = high(int) shr 32): Rational[int]">toRational(x: float; n: int = high(int) shr 32): Rational[int]</a></li>
  158. <li><a class="reference" href="#toRational%2CT" title="toRational[T: SomeInteger](x: T): Rational[T]">toRational[T: SomeInteger](x: T): Rational[T]</a></li>
  159. </ul>
  160. </ul>
  161. </details>
  162. </li>
  163. </ul>
  164. </div>
  165. <div class="nine columns" id="content">
  166. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  167. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  168. <div id="tocRoot"></div>
  169. <p class="module-desc">This module implements rational numbers, consisting of a numerator and a denominator. The denominator can not be 0.
  170. <p><strong class="examples_text">Example:</strong></p>
  171. <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">rationals</span>
  172. <span class="Keyword">let</span>
  173. <span class="Identifier">r1</span> <span class="Operator">=</span> <span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">2</span>
  174. <span class="Identifier">r2</span> <span class="Operator">=</span> <span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">4</span>
  175. <span class="Identifier">doAssert</span> <span class="Identifier">r1</span> <span class="Operator">+</span> <span class="Identifier">r2</span> <span class="Operator">==</span> <span class="Operator">-</span><span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">4</span>
  176. <span class="Identifier">doAssert</span> <span class="Identifier">r1</span> <span class="Operator">-</span> <span class="Identifier">r2</span> <span class="Operator">==</span> <span class="DecNumber">5</span> <span class="Operator">//</span> <span class="DecNumber">4</span>
  177. <span class="Identifier">doAssert</span> <span class="Identifier">r1</span> <span class="Operator">*</span> <span class="Identifier">r2</span> <span class="Operator">==</span> <span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">8</span>
  178. <span class="Identifier">doAssert</span> <span class="Identifier">r1</span> <span class="Operator">/</span> <span class="Identifier">r2</span> <span class="Operator">==</span> <span class="Operator">-</span><span class="DecNumber">2</span> <span class="Operator">//</span> <span class="DecNumber">3</span></pre></p>
  179. <div class="section" id="6">
  180. <h1><a class="toc-backref" href="#6">Imports</a></h1>
  181. <dl class="item">
  182. <a class="reference external" href="math.html">math</a>, <a class="reference external" href="hashes.html">hashes</a>, <a class="reference external" href="assertions.html">assertions</a>
  183. </dl>
  184. </div>
  185. <div class="section" id="7">
  186. <h1><a class="toc-backref" href="#7">Types</a></h1>
  187. <dl class="item">
  188. <div id="Rational">
  189. <dt><pre><a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span>
  190. <span class="Identifier">num</span><span class="Operator">*</span><span class="Other">,</span> <span class="Identifier">den</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  191. <dd>
  192. A rational number, consisting of a numerator <tt class="docutils literal"><span class="pre"><span class="Identifier">num</span></span></tt> and a denominator <tt class="docutils literal"><span class="pre"><span class="Identifier">den</span></span></tt>.
  193. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L28" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  194. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L28" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  195. </dd>
  196. </div>
  197. </dl>
  198. </div>
  199. <div class="section" id="12">
  200. <h1><a class="toc-backref" href="#12">Procs</a></h1>
  201. <dl class="item">
  202. <div id="$-procs-all">
  203. <div id="$,Rational[T]">
  204. <dt><pre><span class="Keyword">func</span> <a href="#%24%2CRational%5BT%5D"><span class="Identifier">`$`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a></pre></dt>
  205. <dd>
  206. Turns a rational number into a string.
  207. <p><strong class="examples_text">Example:</strong></p>
  208. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Operator">$</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;1/2&quot;</span></pre>
  209. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L71" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  210. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L71" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  211. </dd>
  212. </div>
  213. </div>
  214. <div id="*-procs-all">
  215. <div id="*,Rational[T],Rational[T]">
  216. <dt><pre><span class="Keyword">func</span> <a href="#%2A%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`*`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  217. <dd>
  218. Multiplies two rational numbers.
  219. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L180" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  220. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L180" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  221. </dd>
  222. </div>
  223. <div id="*,Rational[T],T">
  224. <dt><pre><span class="Keyword">func</span> <a href="#%2A%2CRational%5BT%5D%2CT"><span class="Identifier">`*`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  225. <dd>
  226. Multiplies the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> with the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  227. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L185" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  228. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L185" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  229. </dd>
  230. </div>
  231. <div id="*,T,Rational[T]">
  232. <dt><pre><span class="Keyword">func</span> <a href="#%2A%2CT%2CRational%5BT%5D"><span class="Identifier">`*`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  233. <dd>
  234. Multiplies the int <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> with the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  235. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L190" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  236. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L190" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  237. </dd>
  238. </div>
  239. </div>
  240. <div id="*=-procs-all">
  241. <div id="*=,Rational[T],Rational[T]">
  242. <dt><pre><span class="Keyword">func</span> <a href="#%2A%3D%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`*=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
  243. <dd>
  244. Multiplies the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> in-place.
  245. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L195" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  246. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L195" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  247. </dd>
  248. </div>
  249. <div id="*=,Rational[T],T">
  250. <dt><pre><span class="Keyword">func</span> <a href="#%2A%3D%2CRational%5BT%5D%2CT"><span class="Identifier">`*=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
  251. <dd>
  252. Multiplies the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> in-place.
  253. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L201" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  254. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L201" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  255. </dd>
  256. </div>
  257. </div>
  258. <div id="+-procs-all">
  259. <div id="+,Rational[T],Rational[T]">
  260. <dt><pre><span class="Keyword">func</span> <a href="#%2B%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`+`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  261. <dd>
  262. Adds two rational numbers.
  263. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L122" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  264. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L122" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  265. </dd>
  266. </div>
  267. <div id="+,Rational[T],T">
  268. <dt><pre><span class="Keyword">func</span> <a href="#%2B%2CRational%5BT%5D%2CT"><span class="Identifier">`+`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  269. <dd>
  270. Adds the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  271. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L130" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  272. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L130" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  273. </dd>
  274. </div>
  275. <div id="+,T,Rational[T]">
  276. <dt><pre><span class="Keyword">func</span> <a href="#%2B%2CT%2CRational%5BT%5D"><span class="Identifier">`+`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  277. <dd>
  278. Adds the int <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  279. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L134" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  280. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L134" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  281. </dd>
  282. </div>
  283. </div>
  284. <div id="+=-procs-all">
  285. <div id="+=,Rational[T],Rational[T]">
  286. <dt><pre><span class="Keyword">func</span> <a href="#%2B%3D%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`+=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
  287. <dd>
  288. Adds the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> to the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> in-place.
  289. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L138" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  290. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L138" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  291. </dd>
  292. </div>
  293. <div id="+=,Rational[T],T">
  294. <dt><pre><span class="Keyword">func</span> <a href="#%2B%3D%2CRational%5BT%5D%2CT"><span class="Identifier">`+=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
  295. <dd>
  296. Adds the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> to the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> in-place.
  297. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L145" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  298. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L145" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  299. </dd>
  300. </div>
  301. </div>
  302. <div id="--procs-all">
  303. <div id="-,Rational[T],Rational[T]">
  304. <dt><pre><span class="Keyword">func</span> <a href="#-%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`-`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  305. <dd>
  306. Subtracts two rational numbers.
  307. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L153" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  308. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L153" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  309. </dd>
  310. </div>
  311. <div id="-,Rational[T]">
  312. <dt><pre><span class="Keyword">func</span> <a href="#-%2CRational%5BT%5D"><span class="Identifier">`-`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  313. <dd>
  314. Unary minus for rational numbers.
  315. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L149" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  316. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L149" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  317. </dd>
  318. </div>
  319. <div id="-,Rational[T],T">
  320. <dt><pre><span class="Keyword">func</span> <a href="#-%2CRational%5BT%5D%2CT"><span class="Identifier">`-`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  321. <dd>
  322. Subtracts the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> from the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
  323. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L161" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  324. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L161" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  325. </dd>
  326. </div>
  327. <div id="-,T,Rational[T]">
  328. <dt><pre><span class="Keyword">func</span> <a href="#-%2CT%2CRational%5BT%5D"><span class="Identifier">`-`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  329. <dd>
  330. Subtracts the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> from the int <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
  331. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L165" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  332. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L165" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  333. </dd>
  334. </div>
  335. </div>
  336. <div id="-=-procs-all">
  337. <div id="-=,Rational[T],Rational[T]">
  338. <dt><pre><span class="Keyword">func</span> <a href="#-%3D%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`-=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
  339. <dd>
  340. Subtracts the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> from the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> in-place.
  341. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L169" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  342. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L169" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  343. </dd>
  344. </div>
  345. <div id="-=,Rational[T],T">
  346. <dt><pre><span class="Keyword">func</span> <a href="#-%3D%2CRational%5BT%5D%2CT"><span class="Identifier">`-=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
  347. <dd>
  348. Subtracts the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> from the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> in-place.
  349. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L176" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  350. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L176" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  351. </dd>
  352. </div>
  353. </div>
  354. <div id="/-procs-all">
  355. <div id="/,Rational[T],Rational[T]">
  356. <dt><pre><span class="Keyword">func</span> <a href="#%2F%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`/`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  357. <dd>
  358. Divides the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  359. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L216" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  360. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L216" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  361. </dd>
  362. </div>
  363. <div id="/,Rational[T],T">
  364. <dt><pre><span class="Keyword">func</span> <a href="#%2F%2CRational%5BT%5D%2CT"><span class="Identifier">`/`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  365. <dd>
  366. Divides the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  367. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L221" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  368. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L221" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  369. </dd>
  370. </div>
  371. <div id="/,T,Rational[T]">
  372. <dt><pre><span class="Keyword">func</span> <a href="#%2F%2CT%2CRational%5BT%5D"><span class="Identifier">`/`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  373. <dd>
  374. Divides the int <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  375. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L226" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  376. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L226" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  377. </dd>
  378. </div>
  379. </div>
  380. <div id="//-procs-all">
  381. <div id="//,T,T">
  382. <dt><pre><span class="Keyword">func</span> <a href="#%2F%2F%2CT%2CT"><span class="Identifier">`//`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">num</span><span class="Other">,</span> <span class="Identifier">den</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  383. <dd>
  384. A friendlier version of <a class="reference external" href="#initRational,T,T">initRational</a>.
  385. <p><strong class="examples_text">Example:</strong></p>
  386. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">3</span> <span class="Operator">+</span> <span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">5</span>
  387. <span class="Identifier">doAssert</span> <span class="Identifier">x</span> <span class="Operator">==</span> <span class="DecNumber">8</span> <span class="Operator">//</span> <span class="DecNumber">15</span></pre>
  388. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L63" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  389. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L63" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  390. </dd>
  391. </div>
  392. </div>
  393. <div id="/=-procs-all">
  394. <div id="/=,Rational[T],Rational[T]">
  395. <dt><pre><span class="Keyword">func</span> <a href="#%2F%3D%2CRational%5BT%5D%2CRational%5BT%5D"><span class="Identifier">`/=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
  396. <dd>
  397. Divides the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> in-place.
  398. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L231" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  399. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L231" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  400. </dd>
  401. </div>
  402. <div id="/=,Rational[T],T">
  403. <dt><pre><span class="Keyword">func</span> <a href="#%2F%3D%2CRational%5BT%5D%2CT"><span class="Identifier">`/=`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
  404. <dd>
  405. Divides the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> by the int <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> in-place.
  406. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L237" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  407. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L237" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  408. </dd>
  409. </div>
  410. </div>
  411. <div id="&lt;-procs-all">
  412. <div id="<,Rational,Rational">
  413. <dt><pre><span class="Keyword">func</span> <a href="#%3C%2CRational%2CRational"><span class="Identifier">`&lt;`</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a></pre></dt>
  414. <dd>
  415. Returns true if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is less than <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  416. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L249" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  417. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L249" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  418. </dd>
  419. </div>
  420. </div>
  421. <div id="&lt;=-procs-all">
  422. <div id="<=,Rational,Rational">
  423. <dt><pre><span class="Keyword">func</span> <a href="#%3C%3D%2CRational%2CRational"><span class="Identifier">`&lt;=`</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a></pre></dt>
  424. <dd>
  425. Returns tue if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is less than or equal to <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.
  426. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L253" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  427. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L253" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  428. </dd>
  429. </div>
  430. </div>
  431. <div id="==-procs-all">
  432. <div id="==,Rational,Rational">
  433. <dt><pre><span class="Keyword">func</span> <a href="#%3D%3D%2CRational%2CRational"><span class="Identifier">`==`</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a></pre></dt>
  434. <dd>
  435. Compares two rationals for equality.
  436. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L257" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  437. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L257" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  438. </dd>
  439. </div>
  440. </div>
  441. <div id="^-procs-all">
  442. <div id="^,Rational[T: SomeInteger],T">
  443. <dt><pre><span class="Keyword">func</span> <a href="#%5E%2CRational%5BT%3A%20SomeInteger%5D%2CT"><span class="Identifier">`^`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">y</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  444. <dd>
  445. <p>Computes <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to the power of <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt>.</p>
  446. <p>The exponent <tt class="docutils literal"><span class="pre"><span class="Identifier">y</span></span></tt> must be an integer. Negative exponents are supported but floating point exponents are not.</p>
  447. <p><strong class="examples_text">Example:</strong></p>
  448. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Punctuation">(</span><span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator">^</span> <span class="DecNumber">0</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">1</span><span class="Punctuation">)</span>
  449. <span class="Identifier">doAssert</span> <span class="Punctuation">(</span><span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator">^</span> <span class="DecNumber">1</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">5</span><span class="Punctuation">)</span>
  450. <span class="Identifier">doAssert</span> <span class="Punctuation">(</span><span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator">^</span> <span class="DecNumber">2</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="DecNumber">9</span> <span class="Operator">//</span> <span class="DecNumber">25</span><span class="Punctuation">)</span>
  451. <span class="Identifier">doAssert</span> <span class="Punctuation">(</span><span class="Operator">-</span><span class="DecNumber">3</span> <span class="Operator">//</span> <span class="DecNumber">5</span><span class="Punctuation">)</span> <span class="Operator">^</span> <span class="Operator">-</span><span class="DecNumber">2</span> <span class="Operator">==</span> <span class="Punctuation">(</span><span class="DecNumber">25</span> <span class="Operator">//</span> <span class="DecNumber">9</span><span class="Punctuation">)</span></pre>
  452. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L307" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  453. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L307" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  454. </dd>
  455. </div>
  456. </div>
  457. <div id="abs-procs-all">
  458. <div id="abs,Rational[T]">
  459. <dt><pre><span class="Keyword">func</span> <a href="#abs%2CRational%5BT%5D"><span class="Identifier">abs</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  460. <dd>
  461. Returns the absolute value of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
  462. <p><strong class="examples_text">Example:</strong></p>
  463. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">abs</span><span class="Punctuation">(</span><span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">2</span>
  464. <span class="Identifier">doAssert</span> <span class="Identifier">abs</span><span class="Punctuation">(</span><span class="Operator">-</span><span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">1</span> <span class="Operator">//</span> <span class="DecNumber">2</span></pre>
  465. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L261" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  466. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L261" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  467. </dd>
  468. </div>
  469. </div>
  470. <div id="cmp-procs-all">
  471. <div id="cmp,Rational,Rational">
  472. <dt><pre><span class="Keyword">func</span> <a href="#cmp%2CRational%2CRational"><span class="Identifier">cmp</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
  473. <dd>
  474. Compares two rationals. Returns<ul class="simple"><li>a value less than zero, if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span> <span class="Operator">&lt;</span> <span class="Identifier">y</span></span></tt></li>
  475. <li>a value greater than zero, if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span> <span class="Operator">&gt;</span> <span class="Identifier">y</span></span></tt></li>
  476. <li>zero, if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span> <span class="Operator">==</span> <span class="Identifier">y</span></span></tt></li>
  477. </ul>
  478. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L242" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  479. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L242" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  480. </dd>
  481. </div>
  482. </div>
  483. <div id="div-procs-all">
  484. <div id="div,Rational[T: SomeInteger],Rational[T: SomeInteger]">
  485. <dt><pre><span class="Keyword">func</span> <a href="#div%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D"><span class="Identifier">`div`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  486. <dd>
  487. Computes the rational truncated division.
  488. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L268" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  489. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L268" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  490. </dd>
  491. </div>
  492. </div>
  493. <div id="floorDiv-procs-all">
  494. <div id="floorDiv,Rational[T: SomeInteger],Rational[T: SomeInteger]">
  495. <dt><pre><span class="Keyword">func</span> <a href="#floorDiv%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D"><span class="Identifier">floorDiv</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
  496. <dd>
  497. <p>Computes the rational floor division.</p>
  498. <p>Floor division is conceptually defined as <tt class="docutils literal"><span class="pre"><span class="Identifier">floor</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Operator">/</span> <span class="Identifier">y</span><span class="Punctuation">)</span></span></tt>. This is different from the <tt class="docutils literal"><span class="pre"><span class="Keyword">div</span></span></tt> operator, which is defined as <tt class="docutils literal"><span class="pre"><span class="Identifier">trunc</span><span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Operator">/</span> <span class="Identifier">y</span><span class="Punctuation">)</span></span></tt>. That is, <tt class="docutils literal"><span class="pre"><span class="Keyword">div</span></span></tt> rounds towards 0 and <tt class="docutils literal"><span class="pre"><span class="Identifier">floorDiv</span></span></tt> rounds down.</p>
  499. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L278" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  500. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L278" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  501. </dd>
  502. </div>
  503. </div>
  504. <div id="floorMod-procs-all">
  505. <div id="floorMod,Rational[T: SomeInteger],Rational[T: SomeInteger]">
  506. <dt><pre><span class="Keyword">func</span> <a href="#floorMod%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D"><span class="Identifier">floorMod</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  507. <dd>
  508. <p>Computes the rational modulo by floor division (modulo).</p>
  509. <p>This is same as <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span> <span class="Operator">-</span> <span class="Identifier">floorDiv</span><span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Operator">*</span> <span class="Identifier">y</span></span></tt>. This func behaves the same as the <tt class="docutils literal"><span class="pre"><span class="Operator">%</span></span></tt> operator in Python.</p>
  510. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L287" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  511. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L287" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  512. </dd>
  513. </div>
  514. </div>
  515. <div id="hash-procs-all">
  516. <div id="hash,Rational[T]">
  517. <dt><pre><span class="Keyword">func</span> <a href="#hash%2CRational%5BT%5D"><span class="Identifier">hash</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="hashes.html#Hash"><span class="Identifier">Hash</span></a></pre></dt>
  518. <dd>
  519. Computes the hash for the rational <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>.
  520. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L295" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  521. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L295" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  522. </dd>
  523. </div>
  524. </div>
  525. <div id="initRational-procs-all">
  526. <div id="initRational,T,T">
  527. <dt><pre><span class="Keyword">func</span> <a href="#initRational%2CT%2CT"><span class="Identifier">initRational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">num</span><span class="Other">,</span> <span class="Identifier">den</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  528. <dd>
  529. <p>Creates a new rational number with numerator <tt class="docutils literal"><span class="pre"><span class="Identifier">num</span></span></tt> and denominator <tt class="docutils literal"><span class="pre"><span class="Identifier">den</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">den</span></span></tt> must not be 0.</p>
  530. <p><strong>Note:</strong> <tt class="docutils literal"><span class="pre"><span class="Identifier">den</span> <span class="Operator">!=</span> <span class="DecNumber">0</span></span></tt> is not checked when assertions are turned off.</p>
  531. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L54" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  532. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L54" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  533. </dd>
  534. </div>
  535. </div>
  536. <div id="mod-procs-all">
  537. <div id="mod,Rational[T: SomeInteger],Rational[T: SomeInteger]">
  538. <dt><pre><span class="Keyword">func</span> <a href="#mod%2CRational%5BT%3A%20SomeInteger%5D%2CRational%5BT%3A%20SomeInteger%5D"><span class="Identifier">`mod`</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">,</span> <span class="Identifier">y</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  539. <dd>
  540. Computes the rational modulo by truncated division (remainder). This is same as <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span> <span class="Operator">-</span> <span class="Punctuation">(</span><span class="Identifier">x</span> <span class="Keyword">div</span> <span class="Identifier">y</span><span class="Punctuation">)</span> <span class="Operator">*</span> <span class="Identifier">y</span></span></tt>.
  541. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L272" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  542. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L272" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  543. </dd>
  544. </div>
  545. </div>
  546. <div id="reciprocal-procs-all">
  547. <div id="reciprocal,Rational[T]">
  548. <dt><pre><span class="Keyword">func</span> <a href="#reciprocal%2CRational%5BT%5D"><span class="Identifier">reciprocal</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  549. <dd>
  550. Calculates the reciprocal of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> (<tt class="docutils literal"><span class="pre"><span class="DecNumber">1</span><span class="Operator">/</span><span class="Identifier">x</span></span></tt>). If <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is 0, raises <tt class="docutils literal"><span class="pre"><span class="Identifier">DivByZeroDefect</span></span></tt>.
  551. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L206" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  552. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L206" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  553. </dd>
  554. </div>
  555. </div>
  556. <div id="reduce-procs-all">
  557. <div id="reduce,Rational[T: SomeInteger]">
  558. <dt><pre><span class="Keyword">func</span> <a href="#reduce%2CRational%5BT%3A%20SomeInteger%5D"><span class="Identifier">reduce</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
  559. <dd>
  560. <p>Reduces the rational number <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>, so that the numerator and denominator have no common divisors other than 1 (and -1). If <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> is 0, raises <tt class="docutils literal"><span class="pre"><span class="Identifier">DivByZeroDefect</span></span></tt>.</p>
  561. <p><strong>Note:</strong> This is called automatically by the various operations on rationals.</p>
  562. <p><strong class="examples_text">Example:</strong></p>
  563. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">Rational</span><span class="Punctuation">[</span><span class="Identifier">int</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Identifier">num</span><span class="Punctuation">:</span> <span class="DecNumber">2</span><span class="Punctuation">,</span> <span class="Identifier">den</span><span class="Punctuation">:</span> <span class="DecNumber">4</span><span class="Punctuation">)</span> <span class="Comment"># 1/2</span>
  564. <span class="Identifier">reduce</span><span class="Punctuation">(</span><span class="Identifier">r</span><span class="Punctuation">)</span>
  565. <span class="Identifier">doAssert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">num</span> <span class="Operator">==</span> <span class="DecNumber">1</span>
  566. <span class="Identifier">doAssert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">den</span> <span class="Operator">==</span> <span class="DecNumber">2</span></pre>
  567. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L32" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  568. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L32" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  569. </dd>
  570. </div>
  571. </div>
  572. <div id="toFloat-procs-all">
  573. <div id="toFloat,Rational[T]">
  574. <dt><pre><span class="Keyword">func</span> <a href="#toFloat%2CRational%5BT%5D"><span class="Identifier">toFloat</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a></pre></dt>
  575. <dd>
  576. Converts a rational number <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to a <tt class="docutils literal"><span class="pre"><span class="Identifier">float</span></span></tt>.
  577. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L113" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  578. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L113" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  579. </dd>
  580. </div>
  581. </div>
  582. <div id="toInt-procs-all">
  583. <div id="toInt,Rational[T]">
  584. <dt><pre><span class="Keyword">func</span> <a href="#toInt%2CRational%5BT%5D"><span class="Identifier">toInt</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
  585. <dd>
  586. Converts a rational number <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to an <tt class="docutils literal"><span class="pre"><span class="Identifier">int</span></span></tt>. Conversion rounds towards 0 if <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> does not contain an integer value.
  587. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L117" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  588. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L117" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  589. </dd>
  590. </div>
  591. </div>
  592. <div id="toRational-procs-all">
  593. <div id="toRational,float,int">
  594. <dt><pre><span class="Keyword">func</span> <a href="#toRational%2Cfloat%2Cint"><span class="Identifier">toRational</span></a><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <a href="system.html#float"><span class="Identifier">float</span></a><span class="Other">;</span> <span class="Identifier">n</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a> <span class="Other">=</span> <span class="Identifier">high</span><span class="Other">(</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">)</span> <span class="Keyword">shr</span> <span class="DecNumber">32</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><a href="system.html#int"><span class="Identifier">int</span></a><span class="Other">]</span> {.
  595. <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  596. <dd>
  597. <p>Calculates the best rational approximation of <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>, where the denominator is smaller than <tt class="docutils literal"><span class="pre"><span class="Identifier">n</span></span></tt> (default is the largest possible <tt class="docutils literal"><span class="pre"><span class="Identifier">int</span></span></tt> for maximal resolution).</p>
  598. <p>The algorithm is based on the theory of continued fractions.</p>
  599. <p><strong class="examples_text">Example:</strong></p>
  600. <pre class="listing"><span class="Keyword">let</span> <span class="Identifier">x</span> <span class="Operator">=</span> <span class="FloatNumber">1.2</span>
  601. <span class="Identifier">doAssert</span> <span class="Identifier">x</span><span class="Operator">.</span><span class="Identifier">toRational</span><span class="Operator">.</span><span class="Identifier">toFloat</span> <span class="Operator">==</span> <span class="Identifier">x</span></pre>
  602. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L84" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  603. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L84" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  604. </dd>
  605. </div>
  606. <div id="toRational,T">
  607. <dt><pre><span class="Keyword">func</span> <a href="#toRational%2CT"><span class="Identifier">toRational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">:</span> <a href="system.html#SomeInteger"><span class="Identifier">SomeInteger</span></a><span class="Other">]</span><span class="Other">(</span><span class="Identifier">x</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="rationals.html#Rational"><span class="Identifier">Rational</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
  608. <dd>
  609. Converts some integer <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> to a rational number.
  610. <p><strong class="examples_text">Example:</strong></p>
  611. <pre class="listing"><span class="Identifier">doAssert</span> <span class="Identifier">toRational</span><span class="Punctuation">(</span><span class="DecNumber">42</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="DecNumber">42</span> <span class="Operator">//</span> <span class="DecNumber">1</span></pre>
  612. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/rationals.nim#L78" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  613. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/rationals.nim#L78" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  614. </dd>
  615. </div>
  616. </div>
  617. </dl>
  618. </div>
  619. </div>
  620. </div>
  621. <div class="twelve-columns footer">
  622. <span class="nim-sprite"></span>
  623. <br>
  624. <small style="color: var(--hint);">Made with Nim. Generated: 2025-01-09 12:00:03 UTC</small>
  625. </div>
  626. </div>
  627. </div>
  628. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  629. </body>
  630. </html>