index.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="generator" content="rustdoc">
  7. <meta name="description" content="API documentation for the Rust `num` crate.">
  8. <meta name="keywords" content="rust, rustlang, rust-lang, num">
  9. <title>num - Rust</title>
  10. <link rel="stylesheet" type="text/css" href="../normalize.css">
  11. <link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle">
  12. <link rel="stylesheet" type="text/css" href="../dark.css">
  13. <link rel="stylesheet" type="text/css" href="../main.css" id="themeStyle">
  14. <script src="../storage.js"></script>
  15. </head>
  16. <body class="rustdoc mod">
  17. <!--[if lte IE 8]>
  18. <div class="warning">
  19. This old browser is unsupported and will most likely display funky
  20. things.
  21. </div>
  22. <![endif]-->
  23. <nav class="sidebar">
  24. <div class="sidebar-menu">&#9776;</div>
  25. <p class='location'>Crate num</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'num', ty: 'mod', relpath: '../'};</script></div>
  26. </nav>
  27. <div class="theme-picker">
  28. <button id="theme-picker" aria-label="Pick another theme!">
  29. <img src="../brush.svg" width="18" alt="Pick another theme!">
  30. </button>
  31. <div id="theme-choices"></div>
  32. </div>
  33. <script src="../theme.js"></script>
  34. <nav class="sub">
  35. <form class="search-form js-only">
  36. <div class="search-container">
  37. <input class="search-input" name="search"
  38. autocomplete="off"
  39. placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
  40. type="search">
  41. </div>
  42. </form>
  43. </nav>
  44. <section id='main' class="content">
  45. <h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>num</a></span><span class='out-of-band'><span id='render-detail'>
  46. <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
  47. [<span class='inner'>&#x2212;</span>]
  48. </a>
  49. </span><a class='srclink' href='../src/num/lib.rs.html#11-113' title='goto source code'>[src]</a></span></h1>
  50. <div class='docblock'><p>A collection of numeric types and traits for Rust.</p>
  51. <p>This includes new types for big integers, rationals, and complex numbers,
  52. new traits for generic programming on numeric properties like <code>Integer</code>,
  53. and generic range iterators.</p>
  54. <h2 id="example" class="section-header"><a href="#example">Example</a></h2>
  55. <p>This example uses the BigRational type and <a href="https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method">Newton's method</a> to
  56. approximate a square root to arbitrary precision:</p>
  57. <pre class="rust rust-example-rendered">
  58. <span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">num</span>;
  59. <span class="kw">use</span> <span class="ident">num</span>::<span class="ident">FromPrimitive</span>;
  60. <span class="kw">use</span> <span class="ident">num</span>::<span class="ident">bigint</span>::<span class="ident">BigInt</span>;
  61. <span class="kw">use</span> <span class="ident">num</span>::<span class="ident">rational</span>::{<span class="ident">Ratio</span>, <span class="ident">BigRational</span>};
  62. <span class="kw">fn</span> <span class="ident">approx_sqrt</span>(<span class="ident">number</span>: <span class="ident">u64</span>, <span class="ident">iterations</span>: <span class="ident">usize</span>) <span class="op">-&gt;</span> <span class="ident">BigRational</span> {
  63. <span class="kw">let</span> <span class="ident">start</span>: <span class="ident">Ratio</span><span class="op">&lt;</span><span class="ident">BigInt</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">Ratio</span>::<span class="ident">from_integer</span>(<span class="ident">FromPrimitive</span>::<span class="ident">from_u64</span>(<span class="ident">number</span>).<span class="ident">unwrap</span>());
  64. <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">approx</span> <span class="op">=</span> <span class="ident">start</span>.<span class="ident">clone</span>();
  65. <span class="kw">for</span> _ <span class="kw">in</span> <span class="number">0</span>..<span class="ident">iterations</span> {
  66. <span class="ident">approx</span> <span class="op">=</span> (<span class="kw-2">&amp;</span><span class="ident">approx</span> <span class="op">+</span> (<span class="kw-2">&amp;</span><span class="ident">start</span> <span class="op">/</span> <span class="kw-2">&amp;</span><span class="ident">approx</span>)) <span class="op">/</span>
  67. <span class="ident">Ratio</span>::<span class="ident">from_integer</span>(<span class="ident">FromPrimitive</span>::<span class="ident">from_u64</span>(<span class="number">2</span>).<span class="ident">unwrap</span>());
  68. }
  69. <span class="ident">approx</span>
  70. }
  71. <span class="kw">fn</span> <span class="ident">main</span>() {
  72. <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">approx_sqrt</span>(<span class="number">10</span>, <span class="number">4</span>)); <span class="comment">// prints 4057691201/1283082416</span>
  73. }
  74. </pre>
  75. <h2 id="compatibility" class="section-header"><a href="#compatibility">Compatibility</a></h2>
  76. <p>The <code>num</code> crate is tested for rustc 1.8 and greater.</p>
  77. </div><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
  78. <table>
  79. <tr class=' module-item'>
  80. <td><a class="mod" href="bigint/index.html"
  81. title='mod num::bigint'>bigint</a></td>
  82. <td class='docblock-short'>
  83. </td>
  84. </tr>
  85. <tr class=' module-item'>
  86. <td><a class="mod" href="cast/index.html"
  87. title='mod num::cast'>cast</a></td>
  88. <td class='docblock-short'>
  89. </td>
  90. </tr>
  91. <tr class=' module-item'>
  92. <td><a class="mod" href="complex/index.html"
  93. title='mod num::complex'>complex</a></td>
  94. <td class='docblock-short'>
  95. </td>
  96. </tr>
  97. <tr class=' module-item'>
  98. <td><a class="mod" href="integer/index.html"
  99. title='mod num::integer'>integer</a></td>
  100. <td class='docblock-short'>
  101. </td>
  102. </tr>
  103. <tr class=' module-item'>
  104. <td><a class="mod" href="iter/index.html"
  105. title='mod num::iter'>iter</a></td>
  106. <td class='docblock-short'>
  107. </td>
  108. </tr>
  109. <tr class=' module-item'>
  110. <td><a class="mod" href="pow/index.html"
  111. title='mod num::pow'>pow</a></td>
  112. <td class='docblock-short'>
  113. </td>
  114. </tr>
  115. <tr class=' module-item'>
  116. <td><a class="mod" href="rational/index.html"
  117. title='mod num::rational'>rational</a></td>
  118. <td class='docblock-short'>
  119. </td>
  120. </tr>
  121. <tr class=' module-item'>
  122. <td><a class="mod" href="traits/index.html"
  123. title='mod num::traits'>traits</a></td>
  124. <td class='docblock-short'>
  125. </td>
  126. </tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
  127. <table>
  128. <tr class=' module-item'>
  129. <td><a class="struct" href="struct.BigInt.html"
  130. title='struct num::BigInt'>BigInt</a></td>
  131. <td class='docblock-short'>
  132. <p>A big signed integer type.</p>
  133. </td>
  134. </tr>
  135. <tr class=' module-item'>
  136. <td><a class="struct" href="struct.BigUint.html"
  137. title='struct num::BigUint'>BigUint</a></td>
  138. <td class='docblock-short'>
  139. <p>A big unsigned integer type.</p>
  140. </td>
  141. </tr>
  142. <tr class=' module-item'>
  143. <td><a class="struct" href="struct.Complex.html"
  144. title='struct num::Complex'>Complex</a></td>
  145. <td class='docblock-short'>
  146. <p>A complex number in Cartesian form.</p>
  147. </td>
  148. </tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
  149. <table>
  150. <tr class=' module-item'>
  151. <td><a class="trait" href="trait.Bounded.html"
  152. title='trait num::Bounded'>Bounded</a></td>
  153. <td class='docblock-short'>
  154. <p>Numbers which have upper and lower bounds</p>
  155. </td>
  156. </tr>
  157. <tr class=' module-item'>
  158. <td><a class="trait" href="trait.CheckedAdd.html"
  159. title='trait num::CheckedAdd'>CheckedAdd</a></td>
  160. <td class='docblock-short'>
  161. <p>Performs addition that returns <code>None</code> instead of wrapping around on
  162. overflow.</p>
  163. </td>
  164. </tr>
  165. <tr class=' module-item'>
  166. <td><a class="trait" href="trait.CheckedDiv.html"
  167. title='trait num::CheckedDiv'>CheckedDiv</a></td>
  168. <td class='docblock-short'>
  169. <p>Performs division that returns <code>None</code> instead of panicking on division by zero and instead of
  170. wrapping around on underflow and overflow.</p>
  171. </td>
  172. </tr>
  173. <tr class=' module-item'>
  174. <td><a class="trait" href="trait.CheckedMul.html"
  175. title='trait num::CheckedMul'>CheckedMul</a></td>
  176. <td class='docblock-short'>
  177. <p>Performs multiplication that returns <code>None</code> instead of wrapping around on underflow or
  178. overflow.</p>
  179. </td>
  180. </tr>
  181. <tr class=' module-item'>
  182. <td><a class="trait" href="trait.CheckedSub.html"
  183. title='trait num::CheckedSub'>CheckedSub</a></td>
  184. <td class='docblock-short'>
  185. <p>Performs subtraction that returns <code>None</code> instead of wrapping around on underflow.</p>
  186. </td>
  187. </tr>
  188. <tr class=' module-item'>
  189. <td><a class="trait" href="trait.Float.html"
  190. title='trait num::Float'>Float</a></td>
  191. <td class='docblock-short'>
  192. <p>Generic trait for floating point numbers</p>
  193. </td>
  194. </tr>
  195. <tr class=' module-item'>
  196. <td><a class="trait" href="trait.FromPrimitive.html"
  197. title='trait num::FromPrimitive'>FromPrimitive</a></td>
  198. <td class='docblock-short'>
  199. <p>A generic trait for converting a number to a value.</p>
  200. </td>
  201. </tr>
  202. <tr class=' module-item'>
  203. <td><a class="trait" href="trait.Integer.html"
  204. title='trait num::Integer'>Integer</a></td>
  205. <td class='docblock-short'>
  206. </td>
  207. </tr>
  208. <tr class=' module-item'>
  209. <td><a class="trait" href="trait.Num.html"
  210. title='trait num::Num'>Num</a></td>
  211. <td class='docblock-short'>
  212. <p>The base trait for numeric types, covering <code>0</code> and <code>1</code> values,
  213. comparisons, basic numeric operations, and string conversion.</p>
  214. </td>
  215. </tr>
  216. <tr class=' module-item'>
  217. <td><a class="trait" href="trait.NumCast.html"
  218. title='trait num::NumCast'>NumCast</a></td>
  219. <td class='docblock-short'>
  220. <p>An interface for casting between machine scalars.</p>
  221. </td>
  222. </tr>
  223. <tr class=' module-item'>
  224. <td><a class="trait" href="trait.One.html"
  225. title='trait num::One'>One</a></td>
  226. <td class='docblock-short'>
  227. <p>Defines a multiplicative identity element for <code>Self</code>.</p>
  228. </td>
  229. </tr>
  230. <tr class=' module-item'>
  231. <td><a class="trait" href="trait.PrimInt.html"
  232. title='trait num::PrimInt'>PrimInt</a></td>
  233. <td class='docblock-short'>
  234. </td>
  235. </tr>
  236. <tr class=' module-item'>
  237. <td><a class="trait" href="trait.Saturating.html"
  238. title='trait num::Saturating'>Saturating</a></td>
  239. <td class='docblock-short'>
  240. <p>Saturating math operations</p>
  241. </td>
  242. </tr>
  243. <tr class=' module-item'>
  244. <td><a class="trait" href="trait.Signed.html"
  245. title='trait num::Signed'>Signed</a></td>
  246. <td class='docblock-short'>
  247. <p>Useful functions for signed numbers (i.e. numbers that can be negative).</p>
  248. </td>
  249. </tr>
  250. <tr class=' module-item'>
  251. <td><a class="trait" href="trait.ToPrimitive.html"
  252. title='trait num::ToPrimitive'>ToPrimitive</a></td>
  253. <td class='docblock-short'>
  254. <p>A generic trait for converting a value to a number.</p>
  255. </td>
  256. </tr>
  257. <tr class=' module-item'>
  258. <td><a class="trait" href="trait.Unsigned.html"
  259. title='trait num::Unsigned'>Unsigned</a></td>
  260. <td class='docblock-short'>
  261. <p>A trait for values which cannot be negative</p>
  262. </td>
  263. </tr>
  264. <tr class=' module-item'>
  265. <td><a class="trait" href="trait.Zero.html"
  266. title='trait num::Zero'>Zero</a></td>
  267. <td class='docblock-short'>
  268. <p>Defines an additive identity element for <code>Self</code>.</p>
  269. </td>
  270. </tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
  271. <table>
  272. <tr class=' module-item'>
  273. <td><a class="fn" href="fn.abs.html"
  274. title='fn num::abs'>abs</a></td>
  275. <td class='docblock-short'>
  276. <p>Computes the absolute value.</p>
  277. </td>
  278. </tr>
  279. <tr class=' module-item'>
  280. <td><a class="fn" href="fn.abs_sub.html"
  281. title='fn num::abs_sub'>abs_sub</a></td>
  282. <td class='docblock-short'>
  283. <p>The positive difference of two numbers.</p>
  284. </td>
  285. </tr>
  286. <tr class=' module-item'>
  287. <td><a class="fn" href="fn.checked_pow.html"
  288. title='fn num::checked_pow'>checked_pow</a></td>
  289. <td class='docblock-short'>
  290. <p>Raises a value to the power of exp, returning <code>None</code> if an overflow occurred.</p>
  291. </td>
  292. </tr>
  293. <tr class=' module-item'>
  294. <td><a class="fn" href="fn.clamp.html"
  295. title='fn num::clamp'>clamp</a></td>
  296. <td class='docblock-short'>
  297. <p>A value bounded by a minimum and a maximum</p>
  298. </td>
  299. </tr>
  300. <tr class=' module-item'>
  301. <td><a class="fn" href="fn.one.html"
  302. title='fn num::one'>one</a></td>
  303. <td class='docblock-short'>
  304. <p>Returns the multiplicative identity, <code>1</code>.</p>
  305. </td>
  306. </tr>
  307. <tr class=' module-item'>
  308. <td><a class="fn" href="fn.range.html"
  309. title='fn num::range'>range</a></td>
  310. <td class='docblock-short'>
  311. <p>Returns an iterator over the given range [start, stop) (that is, starting
  312. at start (inclusive), and ending at stop (exclusive)).</p>
  313. </td>
  314. </tr>
  315. <tr class=' module-item'>
  316. <td><a class="fn" href="fn.range_inclusive.html"
  317. title='fn num::range_inclusive'>range_inclusive</a></td>
  318. <td class='docblock-short'>
  319. <p>Return an iterator over the range [start, stop]</p>
  320. </td>
  321. </tr>
  322. <tr class=' module-item'>
  323. <td><a class="fn" href="fn.range_step.html"
  324. title='fn num::range_step'>range_step</a></td>
  325. <td class='docblock-short'>
  326. <p>Return an iterator over the range [start, stop) by <code>step</code>. It handles overflow by stopping.</p>
  327. </td>
  328. </tr>
  329. <tr class=' module-item'>
  330. <td><a class="fn" href="fn.range_step_inclusive.html"
  331. title='fn num::range_step_inclusive'>range_step_inclusive</a></td>
  332. <td class='docblock-short'>
  333. <p>Return an iterator over the range [start, stop] by <code>step</code>. It handles overflow by stopping.</p>
  334. </td>
  335. </tr>
  336. <tr class=' module-item'>
  337. <td><a class="fn" href="fn.signum.html"
  338. title='fn num::signum'>signum</a></td>
  339. <td class='docblock-short'>
  340. <p>Returns the sign of the number.</p>
  341. </td>
  342. </tr>
  343. <tr class=' module-item'>
  344. <td><a class="fn" href="fn.zero.html"
  345. title='fn num::zero'>zero</a></td>
  346. <td class='docblock-short'>
  347. <p>Returns the additive identity, <code>0</code>.</p>
  348. </td>
  349. </tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
  350. <table>
  351. <tr class=' module-item'>
  352. <td><a class="type" href="type.BigRational.html"
  353. title='type num::BigRational'>BigRational</a></td>
  354. <td class='docblock-short'>
  355. <p>Alias for arbitrary precision rationals.</p>
  356. </td>
  357. </tr>
  358. <tr class=' module-item'>
  359. <td><a class="type" href="type.Rational.html"
  360. title='type num::Rational'>Rational</a></td>
  361. <td class='docblock-short'>
  362. <p>Alias for a <code>Ratio</code> of machine-sized integers.</p>
  363. </td>
  364. </tr></table></section>
  365. <section id='search' class="content hidden"></section>
  366. <section class="footer"></section>
  367. <aside id="help" class="hidden">
  368. <div>
  369. <h1 class="hidden">Help</h1>
  370. <div class="shortcuts">
  371. <h2>Keyboard Shortcuts</h2>
  372. <dl>
  373. <dt><kbd>?</kbd></dt>
  374. <dd>Show this help dialog</dd>
  375. <dt><kbd>S</kbd></dt>
  376. <dd>Focus the search field</dd>
  377. <dt><kbd>↑</kbd></dt>
  378. <dd>Move up in search results</dd>
  379. <dt><kbd>↓</kbd></dt>
  380. <dd>Move down in search results</dd>
  381. <dt><kbd>↹</kbd></dt>
  382. <dd>Switch tab</dd>
  383. <dt><kbd>&#9166;</kbd></dt>
  384. <dd>Go to active search result</dd>
  385. <dt><kbd>+</kbd></dt>
  386. <dd>Expand all sections</dd>
  387. <dt><kbd>-</kbd></dt>
  388. <dd>Collapse all sections</dd>
  389. </dl>
  390. </div>
  391. <div class="infos">
  392. <h2>Search Tricks</h2>
  393. <p>
  394. Prefix searches with a type followed by a colon (e.g.
  395. <code>fn:</code>) to restrict the search to a given type.
  396. </p>
  397. <p>
  398. Accepted types are: <code>fn</code>, <code>mod</code>,
  399. <code>struct</code>, <code>enum</code>,
  400. <code>trait</code>, <code>type</code>, <code>macro</code>,
  401. and <code>const</code>.
  402. </p>
  403. <p>
  404. Search functions by type signature (e.g.
  405. <code>vec -> usize</code> or <code>* -> vec</code>)
  406. </p>
  407. </div>
  408. </div>
  409. </aside>
  410. <script>
  411. window.rootPath = "../";
  412. window.currentCrate = "num";
  413. </script>
  414. <script src="../main.js"></script>
  415. <script defer src="../search-index.js"></script>
  416. </body>
  417. </html>