index.html 24 KB

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