38 lines
No EOL
15 KiB
HTML
38 lines
No EOL
15 KiB
HTML
<!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="A collection of numeric types and traits for Rust."><title>num - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-fa3bb1812debf86c.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="num" data-themes="" data-resource-suffix="" data-rustdoc-version="1.74.0 (79e9716c9 2023-11-13)" data-channel="1.74.0" data-search-js="search-8be46b629f5f14a8.js" data-settings-js="settings-74424d7eec62a23e.js" ><script src="../static.files/storage-fec3eaa3851e447d.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-c5bd66d33317d69f.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-5d8b3c7633ad77ba.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="logo-container" href="../num/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a></nav><nav class="sidebar"><a class="logo-container" href="../num/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2 class="location"><a href="#">Crate num</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.4.1</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><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 Aliases</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">num</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/num/lib.rs.html#11-108">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A collection of numeric types and traits for Rust.</p>
|
||
<p>This includes new types for big integers, rationals, and complex numbers,
|
||
new traits for generic programming on numeric properties like <code>Integer</code>,
|
||
and generic range iterators.</p>
|
||
<h3 id="example"><a href="#example">Example</a></h3>
|
||
<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
|
||
approximate a square root to arbitrary precision:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>
|
||
<span class="kw">use </span>num::FromPrimitive;
|
||
<span class="kw">use </span>num::bigint::BigInt;
|
||
<span class="kw">use </span>num::rational::{Ratio, BigRational};
|
||
|
||
<span class="kw">fn </span>approx_sqrt(number: u64, iterations: usize) -> BigRational {
|
||
<span class="kw">let </span>start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
|
||
<span class="kw">let </span><span class="kw-2">mut </span>approx = start.clone();
|
||
|
||
<span class="kw">for _ in </span><span class="number">0</span>..iterations {
|
||
approx = (<span class="kw-2">&</span>approx + (<span class="kw-2">&</span>start / <span class="kw-2">&</span>approx)) /
|
||
Ratio::from_integer(FromPrimitive::from_u64(<span class="number">2</span>).unwrap());
|
||
}
|
||
|
||
approx
|
||
}
|
||
|
||
<span class="kw">fn </span>main() {
|
||
<span class="macro">println!</span>(<span class="string">"{}"</span>, approx_sqrt(<span class="number">10</span>, <span class="number">4</span>)); <span class="comment">// prints 4057691201/1283082416
|
||
</span>}
|
||
</code></pre></div>
|
||
<h3 id="compatibility"><a href="#compatibility">Compatibility</a></h3>
|
||
<p>The <code>num</code> crate is tested for rustc 1.31 and greater.</p>
|
||
</div></details><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="bigint/index.html" title="mod num::bigint">bigint</a></div></li><li><div class="item-name"><a class="mod" href="cast/index.html" title="mod num::cast">cast</a></div></li><li><div class="item-name"><a class="mod" href="complex/index.html" title="mod num::complex">complex</a></div></li><li><div class="item-name"><a class="mod" href="integer/index.html" title="mod num::integer">integer</a></div></li><li><div class="item-name"><a class="mod" href="iter/index.html" title="mod num::iter">iter</a></div></li><li><div class="item-name"><a class="mod" href="pow/index.html" title="mod num::pow">pow</a></div></li><li><div class="item-name"><a class="mod" href="rational/index.html" title="mod num::rational">rational</a></div></li><li><div class="item-name"><a class="mod" href="traits/index.html" title="mod num::traits">traits</a></div></li></ul><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.BigInt.html" title="struct num::BigInt">BigInt</a></div><div class="desc docblock-short">A big signed integer type.</div></li><li><div class="item-name"><a class="struct" href="struct.BigUint.html" title="struct num::BigUint">BigUint</a></div><div class="desc docblock-short">A big unsigned integer type.</div></li><li><div class="item-name"><a class="struct" href="struct.Complex.html" title="struct num::Complex">Complex</a></div><div class="desc docblock-short">A complex number in Cartesian form.</div></li></ul><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.Bounded.html" title="trait num::Bounded">Bounded</a></div><div class="desc docblock-short">Numbers which have upper and lower bounds</div></li><li><div class="item-name"><a class="trait" href="trait.CheckedAdd.html" title="trait num::CheckedAdd">CheckedAdd</a></div><div class="desc docblock-short">Performs addition that returns <code>None</code> instead of wrapping around on
|
||
overflow.</div></li><li><div class="item-name"><a class="trait" href="trait.CheckedDiv.html" title="trait num::CheckedDiv">CheckedDiv</a></div><div class="desc docblock-short">Performs division that returns <code>None</code> instead of panicking on division by zero and instead of
|
||
wrapping around on underflow and overflow.</div></li><li><div class="item-name"><a class="trait" href="trait.CheckedMul.html" title="trait num::CheckedMul">CheckedMul</a></div><div class="desc docblock-short">Performs multiplication that returns <code>None</code> instead of wrapping around on underflow or
|
||
overflow.</div></li><li><div class="item-name"><a class="trait" href="trait.CheckedSub.html" title="trait num::CheckedSub">CheckedSub</a></div><div class="desc docblock-short">Performs subtraction that returns <code>None</code> instead of wrapping around on underflow.</div></li><li><div class="item-name"><a class="trait" href="trait.Float.html" title="trait num::Float">Float</a></div><div class="desc docblock-short">Generic trait for floating point numbers</div></li><li><div class="item-name"><a class="trait" href="trait.FromPrimitive.html" title="trait num::FromPrimitive">FromPrimitive</a></div><div class="desc docblock-short">A generic trait for converting a number to a value.</div></li><li><div class="item-name"><a class="trait" href="trait.Integer.html" title="trait num::Integer">Integer</a></div></li><li><div class="item-name"><a class="trait" href="trait.Num.html" title="trait num::Num">Num</a></div><div class="desc docblock-short">The base trait for numeric types, covering <code>0</code> and <code>1</code> values,
|
||
comparisons, basic numeric operations, and string conversion.</div></li><li><div class="item-name"><a class="trait" href="trait.NumCast.html" title="trait num::NumCast">NumCast</a></div><div class="desc docblock-short">An interface for casting between machine scalars.</div></li><li><div class="item-name"><a class="trait" href="trait.One.html" title="trait num::One">One</a></div><div class="desc docblock-short">Defines a multiplicative identity element for <code>Self</code>.</div></li><li><div class="item-name"><a class="trait" href="trait.PrimInt.html" title="trait num::PrimInt">PrimInt</a></div><div class="desc docblock-short">Generic trait for primitive integers.</div></li><li><div class="item-name"><a class="trait" href="trait.Saturating.html" title="trait num::Saturating">Saturating</a></div><div class="desc docblock-short">Saturating math operations. Deprecated, use <code>SaturatingAdd</code>, <code>SaturatingSub</code> and
|
||
<code>SaturatingMul</code> instead.</div></li><li><div class="item-name"><a class="trait" href="trait.Signed.html" title="trait num::Signed">Signed</a></div><div class="desc docblock-short">Useful functions for signed numbers (i.e. numbers that can be negative).</div></li><li><div class="item-name"><a class="trait" href="trait.ToPrimitive.html" title="trait num::ToPrimitive">ToPrimitive</a></div><div class="desc docblock-short">A generic trait for converting a value to a number.</div></li><li><div class="item-name"><a class="trait" href="trait.Unsigned.html" title="trait num::Unsigned">Unsigned</a></div><div class="desc docblock-short">A trait for values which cannot be negative</div></li><li><div class="item-name"><a class="trait" href="trait.Zero.html" title="trait num::Zero">Zero</a></div><div class="desc docblock-short">Defines an additive identity element for <code>Self</code>.</div></li></ul><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.abs.html" title="fn num::abs">abs</a></div><div class="desc docblock-short">Computes the absolute value.</div></li><li><div class="item-name"><a class="fn" href="fn.abs_sub.html" title="fn num::abs_sub">abs_sub</a></div><div class="desc docblock-short">The positive difference of two numbers.</div></li><li><div class="item-name"><a class="fn" href="fn.cast.html" title="fn num::cast">cast</a></div><div class="desc docblock-short">Cast from one machine scalar to another.</div></li><li><div class="item-name"><a class="fn" href="fn.checked_pow.html" title="fn num::checked_pow">checked_pow</a></div><div class="desc docblock-short">Raises a value to the power of exp, returning <code>None</code> if an overflow occurred.</div></li><li><div class="item-name"><a class="fn" href="fn.clamp.html" title="fn num::clamp">clamp</a></div><div class="desc docblock-short">A value bounded by a minimum and a maximum</div></li><li><div class="item-name"><a class="fn" href="fn.one.html" title="fn num::one">one</a></div><div class="desc docblock-short">Returns the multiplicative identity, <code>1</code>.</div></li><li><div class="item-name"><a class="fn" href="fn.pow.html" title="fn num::pow">pow</a></div><div class="desc docblock-short">Raises a value to the power of exp, using exponentiation by squaring.</div></li><li><div class="item-name"><a class="fn" href="fn.range.html" title="fn num::range">range</a></div><div class="desc docblock-short">Returns an iterator over the given range [start, stop) (that is, starting
|
||
at start (inclusive), and ending at stop (exclusive)).</div></li><li><div class="item-name"><a class="fn" href="fn.range_inclusive.html" title="fn num::range_inclusive">range_inclusive</a></div><div class="desc docblock-short">Return an iterator over the range [start, stop]</div></li><li><div class="item-name"><a class="fn" href="fn.range_step.html" title="fn num::range_step">range_step</a></div><div class="desc docblock-short">Return an iterator over the range [start, stop) by <code>step</code>. It handles overflow by stopping.</div></li><li><div class="item-name"><a class="fn" href="fn.range_step_inclusive.html" title="fn num::range_step_inclusive">range_step_inclusive</a></div><div class="desc docblock-short">Return an iterator over the range [start, stop] by <code>step</code>. It handles overflow by stopping.</div></li><li><div class="item-name"><a class="fn" href="fn.signum.html" title="fn num::signum">signum</a></div><div class="desc docblock-short">Returns the sign of the number.</div></li><li><div class="item-name"><a class="fn" href="fn.zero.html" title="fn num::zero">zero</a></div><div class="desc docblock-short">Returns the additive identity, <code>0</code>.</div></li></ul><h2 id="types" class="small-section-header"><a href="#types">Type Aliases</a></h2><ul class="item-table"><li><div class="item-name"><a class="type" href="type.BigRational.html" title="type num::BigRational">BigRational</a></div><div class="desc docblock-short">Alias for arbitrary precision rationals.</div></li><li><div class="item-name"><a class="type" href="type.Rational.html" title="type num::Rational">Rational</a><span class="stab deprecated" title="">Deprecated</span></div><div class="desc docblock-short">Alias for a <code>Ratio</code> of machine-sized integers.</div></li><li><div class="item-name"><a class="type" href="type.Rational32.html" title="type num::Rational32">Rational32</a></div><div class="desc docblock-short">Alias for a <code>Ratio</code> of 32-bit-sized integers.</div></li><li><div class="item-name"><a class="type" href="type.Rational64.html" title="type num::Rational64">Rational64</a></div><div class="desc docblock-short">Alias for a <code>Ratio</code> of 64-bit-sized integers.</div></li></ul></section></div></main></body></html> |