914 lines
109 KiB
HTML
914 lines
109 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="API documentation for the Rust `Float` trait in crate `num`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, Float">
|
||
|
||
<title>num::Float - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../normalize.css">
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../main.css">
|
||
|
||
|
||
<link rel="shortcut icon" href="https://rust-num.github.io/num/favicon.ico">
|
||
|
||
</head>
|
||
<body class="rustdoc trait">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
<a href='../num/index.html'><img src='https://rust-num.github.io/num/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
|
||
<p class='location'>Trait Float</p><div class="block items"><ul><li><a href="#required-methods">Required Methods</a></li><li><a href="#provided-methods">Provided Methods</a></li><li><a href="#implementors">Implementors</a></li></ul></div><p class='location'><a href='index.html'>num</a></p><script>window.sidebarCurrent = {name: 'Float', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<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">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content">
|
||
<h1 class='fqn'><span class='in-band'>Trait <a href='index.html'>num</a>::<wbr><a class="trait" href=''>Float</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'>−</span>]
|
||
</a>
|
||
</span><a class='srclink' href='../src/num_traits/float.rs.html#13-926' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust trait'>pub trait Float: <a class="trait" href="../num/cast/trait.NumCast.html" title="trait num::cast::NumCast">NumCast</a> + <a class="trait" href="../num/trait.Num.html" title="trait num::Num">Num</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Copy.html" title="trait core::marker::Copy">Copy</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.Neg.html" title="trait core::ops::arith::Neg">Neg</a><Output = Self> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a><Self> {
|
||
fn <a href='#tymethod.nan' class='fnname'>nan</a>() -> Self;
|
||
fn <a href='#tymethod.infinity' class='fnname'>infinity</a>() -> Self;
|
||
fn <a href='#tymethod.neg_infinity' class='fnname'>neg_infinity</a>() -> Self;
|
||
fn <a href='#tymethod.neg_zero' class='fnname'>neg_zero</a>() -> Self;
|
||
fn <a href='#tymethod.min_value' class='fnname'>min_value</a>() -> Self;
|
||
fn <a href='#tymethod.min_positive_value' class='fnname'>min_positive_value</a>() -> Self;
|
||
fn <a href='#tymethod.max_value' class='fnname'>max_value</a>() -> Self;
|
||
fn <a href='#tymethod.is_nan' class='fnname'>is_nan</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
|
||
fn <a href='#tymethod.is_infinite' class='fnname'>is_infinite</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
|
||
fn <a href='#tymethod.is_finite' class='fnname'>is_finite</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
|
||
fn <a href='#tymethod.is_normal' class='fnname'>is_normal</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
|
||
fn <a href='#tymethod.classify' class='fnname'>classify</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/num/enum.FpCategory.html" title="enum core::num::FpCategory">FpCategory</a>;
|
||
fn <a href='#tymethod.floor' class='fnname'>floor</a>(self) -> Self;
|
||
fn <a href='#tymethod.ceil' class='fnname'>ceil</a>(self) -> Self;
|
||
fn <a href='#tymethod.round' class='fnname'>round</a>(self) -> Self;
|
||
fn <a href='#tymethod.trunc' class='fnname'>trunc</a>(self) -> Self;
|
||
fn <a href='#tymethod.fract' class='fnname'>fract</a>(self) -> Self;
|
||
fn <a href='#tymethod.abs' class='fnname'>abs</a>(self) -> Self;
|
||
fn <a href='#tymethod.signum' class='fnname'>signum</a>(self) -> Self;
|
||
fn <a href='#tymethod.is_sign_positive' class='fnname'>is_sign_positive</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
|
||
fn <a href='#tymethod.is_sign_negative' class='fnname'>is_sign_negative</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a>;
|
||
fn <a href='#tymethod.mul_add' class='fnname'>mul_add</a>(self, a: Self, b: Self) -> Self;
|
||
fn <a href='#tymethod.recip' class='fnname'>recip</a>(self) -> Self;
|
||
fn <a href='#tymethod.powi' class='fnname'>powi</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -> Self;
|
||
fn <a href='#tymethod.powf' class='fnname'>powf</a>(self, n: Self) -> Self;
|
||
fn <a href='#tymethod.sqrt' class='fnname'>sqrt</a>(self) -> Self;
|
||
fn <a href='#tymethod.exp' class='fnname'>exp</a>(self) -> Self;
|
||
fn <a href='#tymethod.exp2' class='fnname'>exp2</a>(self) -> Self;
|
||
fn <a href='#tymethod.ln' class='fnname'>ln</a>(self) -> Self;
|
||
fn <a href='#tymethod.log' class='fnname'>log</a>(self, base: Self) -> Self;
|
||
fn <a href='#tymethod.log2' class='fnname'>log2</a>(self) -> Self;
|
||
fn <a href='#tymethod.log10' class='fnname'>log10</a>(self) -> Self;
|
||
fn <a href='#tymethod.max' class='fnname'>max</a>(self, other: Self) -> Self;
|
||
fn <a href='#tymethod.min' class='fnname'>min</a>(self, other: Self) -> Self;
|
||
fn <a href='#tymethod.abs_sub' class='fnname'>abs_sub</a>(self, other: Self) -> Self;
|
||
fn <a href='#tymethod.cbrt' class='fnname'>cbrt</a>(self) -> Self;
|
||
fn <a href='#tymethod.hypot' class='fnname'>hypot</a>(self, other: Self) -> Self;
|
||
fn <a href='#tymethod.sin' class='fnname'>sin</a>(self) -> Self;
|
||
fn <a href='#tymethod.cos' class='fnname'>cos</a>(self) -> Self;
|
||
fn <a href='#tymethod.tan' class='fnname'>tan</a>(self) -> Self;
|
||
fn <a href='#tymethod.asin' class='fnname'>asin</a>(self) -> Self;
|
||
fn <a href='#tymethod.acos' class='fnname'>acos</a>(self) -> Self;
|
||
fn <a href='#tymethod.atan' class='fnname'>atan</a>(self) -> Self;
|
||
fn <a href='#tymethod.atan2' class='fnname'>atan2</a>(self, other: Self) -> Self;
|
||
fn <a href='#tymethod.sin_cos' class='fnname'>sin_cos</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>Self, Self<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>;
|
||
fn <a href='#tymethod.exp_m1' class='fnname'>exp_m1</a>(self) -> Self;
|
||
fn <a href='#tymethod.ln_1p' class='fnname'>ln_1p</a>(self) -> Self;
|
||
fn <a href='#tymethod.sinh' class='fnname'>sinh</a>(self) -> Self;
|
||
fn <a href='#tymethod.cosh' class='fnname'>cosh</a>(self) -> Self;
|
||
fn <a href='#tymethod.tanh' class='fnname'>tanh</a>(self) -> Self;
|
||
fn <a href='#tymethod.asinh' class='fnname'>asinh</a>(self) -> Self;
|
||
fn <a href='#tymethod.acosh' class='fnname'>acosh</a>(self) -> Self;
|
||
fn <a href='#tymethod.atanh' class='fnname'>atanh</a>(self) -> Self;
|
||
fn <a href='#tymethod.integer_decode' class='fnname'>integer_decode</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>;
|
||
|
||
fn <a href='#method.epsilon' class='fnname'>epsilon</a>() -> Self { ... }
|
||
fn <a href='#method.to_degrees' class='fnname'>to_degrees</a>(self) -> Self { ... }
|
||
fn <a href='#method.to_radians' class='fnname'>to_radians</a>(self) -> Self { ... }
|
||
}</pre>
|
||
<h2 id='required-methods'>Required Methods</h2>
|
||
<div class='methods'>
|
||
<h3 id='tymethod.nan' class='method'><span id='nan.v' class='invisible'><code>fn <a href='#tymethod.nan' class='fnname'>nan</a>() -> Self</code></span></h3><div class='docblock'><p>Returns the <code>NaN</code> value.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">nan</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">nan</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">nan</span>.<span class="ident">is_nan</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20nan%3A%20f32%20%3D%20Float%3A%3Anan()%3B%0A%0Aassert!(nan.is_nan())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.infinity' class='method'><span id='infinity.v' class='invisible'><code>fn <a href='#tymethod.infinity' class='fnname'>infinity</a>() -> Self</code></span></h3><div class='docblock'><p>Returns the infinite value.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f32</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">infinity</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">infinity</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">infinity</span>.<span class="ident">is_infinite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">infinity</span>.<span class="ident">is_finite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">infinity</span> <span class="op">></span> <span class="ident">f32</span>::<span class="ident">MAX</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af32%3B%0A%0Alet%20infinity%3A%20f32%20%3D%20Float%3A%3Ainfinity()%3B%0A%0Aassert!(infinity.is_infinite())%3B%0Aassert!(!infinity.is_finite())%3B%0Aassert!(infinity%20%3E%20f32%3A%3AMAX)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.neg_infinity' class='method'><span id='neg_infinity.v' class='invisible'><code>fn <a href='#tymethod.neg_infinity' class='fnname'>neg_infinity</a>() -> Self</code></span></h3><div class='docblock'><p>Returns the negative infinite value.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f32</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">neg_infinity</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">neg_infinity</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">neg_infinity</span>.<span class="ident">is_infinite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">neg_infinity</span>.<span class="ident">is_finite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">neg_infinity</span> <span class="op"><</span> <span class="ident">f32</span>::<span class="ident">MIN</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af32%3B%0A%0Alet%20neg_infinity%3A%20f32%20%3D%20Float%3A%3Aneg_infinity()%3B%0A%0Aassert!(neg_infinity.is_infinite())%3B%0Aassert!(!neg_infinity.is_finite())%3B%0Aassert!(neg_infinity%20%3C%20f32%3A%3AMIN)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.neg_zero' class='method'><span id='neg_zero.v' class='invisible'><code>fn <a href='#tymethod.neg_zero' class='fnname'>neg_zero</a>() -> Self</code></span></h3><div class='docblock'><p>Returns <code>-0.0</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::{<span class="ident">Zero</span>, <span class="ident">Float</span>};
|
||
|
||
<span class="kw">let</span> <span class="ident">inf</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">infinity</span>();
|
||
<span class="kw">let</span> <span class="ident">zero</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Zero</span>::<span class="ident">zero</span>();
|
||
<span class="kw">let</span> <span class="ident">neg_zero</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">neg_zero</span>();
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">zero</span>, <span class="ident">neg_zero</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="number">7.0f32</span><span class="op">/</span><span class="ident">inf</span>, <span class="ident">zero</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">zero</span> <span class="op">*</span> <span class="number">10.0</span>, <span class="ident">zero</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3A%7BZero%2C%20Float%7D%3B%0A%0Alet%20inf%3A%20f32%20%3D%20Float%3A%3Ainfinity()%3B%0Alet%20zero%3A%20f32%20%3D%20Zero%3A%3Azero()%3B%0Alet%20neg_zero%3A%20f32%20%3D%20Float%3A%3Aneg_zero()%3B%0A%0Aassert_eq!(zero%2C%20neg_zero)%3B%0Aassert_eq!(7.0f32%2Finf%2C%20zero)%3B%0Aassert_eq!(zero%20*%2010.0%2C%20zero)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.min_value' class='method'><span id='min_value.v' class='invisible'><code>fn <a href='#tymethod.min_value' class='fnname'>min_value</a>() -> Self</code></span></h3><div class='docblock'><p>Returns the smallest finite value that this type can represent.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">min_value</span>();
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="ident">f64</span>::<span class="ident">MIN</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%3A%20f64%20%3D%20Float%3A%3Amin_value()%3B%0A%0Aassert_eq!(x%2C%20f64%3A%3AMIN)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.min_positive_value' class='method'><span id='min_positive_value.v' class='invisible'><code>fn <a href='#tymethod.min_positive_value' class='fnname'>min_positive_value</a>() -> Self</code></span></h3><div class='docblock'><p>Returns the smallest positive, normalized value that this type can represent.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">min_positive_value</span>();
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="ident">f64</span>::<span class="ident">MIN_POSITIVE</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%3A%20f64%20%3D%20Float%3A%3Amin_positive_value()%3B%0A%0Aassert_eq!(x%2C%20f64%3A%3AMIN_POSITIVE)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.max_value' class='method'><span id='max_value.v' class='invisible'><code>fn <a href='#tymethod.max_value' class='fnname'>max_value</a>() -> Self</code></span></h3><div class='docblock'><p>Returns the largest finite value that this type can represent.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">max_value</span>();
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="ident">f64</span>::<span class="ident">MAX</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%3A%20f64%20%3D%20Float%3A%3Amax_value()%3B%0Aassert_eq!(x%2C%20f64%3A%3AMAX)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.is_nan' class='method'><span id='is_nan.v' class='invisible'><code>fn <a href='#tymethod.is_nan' class='fnname'>is_nan</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Returns <code>true</code> if this value is <code>NaN</code> and false otherwise.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">nan</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">NAN</span>;
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">7.0</span>;
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">nan</span>.<span class="ident">is_nan</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">f</span>.<span class="ident">is_nan</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20nan%20%3D%20f64%3A%3ANAN%3B%0Alet%20f%20%3D%207.0%3B%0A%0Aassert!(nan.is_nan())%3B%0Aassert!(!f.is_nan())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.is_infinite' class='method'><span id='is_infinite.v' class='invisible'><code>fn <a href='#tymethod.is_infinite' class='fnname'>is_infinite</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Returns <code>true</code> if this value is positive infinity or negative infinity and
|
||
false otherwise.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f32</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">7.0f32</span>;
|
||
<span class="kw">let</span> <span class="ident">inf</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">infinity</span>();
|
||
<span class="kw">let</span> <span class="ident">neg_inf</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">neg_infinity</span>();
|
||
<span class="kw">let</span> <span class="ident">nan</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">f32</span>::<span class="ident">NAN</span>;
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">f</span>.<span class="ident">is_infinite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">nan</span>.<span class="ident">is_infinite</span>());
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">inf</span>.<span class="ident">is_infinite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">neg_inf</span>.<span class="ident">is_infinite</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af32%3B%0A%0Alet%20f%20%3D%207.0f32%3B%0Alet%20inf%3A%20f32%20%3D%20Float%3A%3Ainfinity()%3B%0Alet%20neg_inf%3A%20f32%20%3D%20Float%3A%3Aneg_infinity()%3B%0Alet%20nan%3A%20f32%20%3D%20f32%3A%3ANAN%3B%0A%0Aassert!(!f.is_infinite())%3B%0Aassert!(!nan.is_infinite())%3B%0A%0Aassert!(inf.is_infinite())%3B%0Aassert!(neg_inf.is_infinite())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.is_finite' class='method'><span id='is_finite.v' class='invisible'><code>fn <a href='#tymethod.is_finite' class='fnname'>is_finite</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Returns <code>true</code> if this number is neither infinite nor <code>NaN</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f32</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">7.0f32</span>;
|
||
<span class="kw">let</span> <span class="ident">inf</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">infinity</span>();
|
||
<span class="kw">let</span> <span class="ident">neg_inf</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">neg_infinity</span>();
|
||
<span class="kw">let</span> <span class="ident">nan</span>: <span class="ident">f32</span> <span class="op">=</span> <span class="ident">f32</span>::<span class="ident">NAN</span>;
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">is_finite</span>());
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">nan</span>.<span class="ident">is_finite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">inf</span>.<span class="ident">is_finite</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">neg_inf</span>.<span class="ident">is_finite</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af32%3B%0A%0Alet%20f%20%3D%207.0f32%3B%0Alet%20inf%3A%20f32%20%3D%20Float%3A%3Ainfinity()%3B%0Alet%20neg_inf%3A%20f32%20%3D%20Float%3A%3Aneg_infinity()%3B%0Alet%20nan%3A%20f32%20%3D%20f32%3A%3ANAN%3B%0A%0Aassert!(f.is_finite())%3B%0A%0Aassert!(!nan.is_finite())%3B%0Aassert!(!inf.is_finite())%3B%0Aassert!(!neg_inf.is_finite())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.is_normal' class='method'><span id='is_normal.v' class='invisible'><code>fn <a href='#tymethod.is_normal' class='fnname'>is_normal</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Returns <code>true</code> if the number is neither zero, infinite,
|
||
<a href="http://en.wikipedia.org/wiki/Denormal_number">subnormal</a>, or <code>NaN</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f32</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">min</span> <span class="op">=</span> <span class="ident">f32</span>::<span class="ident">MIN_POSITIVE</span>; <span class="comment">// 1.17549435e-38f32</span>
|
||
<span class="kw">let</span> <span class="ident">max</span> <span class="op">=</span> <span class="ident">f32</span>::<span class="ident">MAX</span>;
|
||
<span class="kw">let</span> <span class="ident">lower_than_min</span> <span class="op">=</span> <span class="number">1.0e-40_f32</span>;
|
||
<span class="kw">let</span> <span class="ident">zero</span> <span class="op">=</span> <span class="number">0.0f32</span>;
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">min</span>.<span class="ident">is_normal</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">max</span>.<span class="ident">is_normal</span>());
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">zero</span>.<span class="ident">is_normal</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">f32</span>::<span class="ident">NAN</span>.<span class="ident">is_normal</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">f32</span>::<span class="ident">INFINITY</span>.<span class="ident">is_normal</span>());
|
||
<span class="comment">// Values between `0` and `min` are Subnormal.</span>
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">lower_than_min</span>.<span class="ident">is_normal</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af32%3B%0A%0Alet%20min%20%3D%20f32%3A%3AMIN_POSITIVE%3B%20%2F%2F%201.17549435e-38f32%0Alet%20max%20%3D%20f32%3A%3AMAX%3B%0Alet%20lower_than_min%20%3D%201.0e-40_f32%3B%0Alet%20zero%20%3D%200.0f32%3B%0A%0Aassert!(min.is_normal())%3B%0Aassert!(max.is_normal())%3B%0A%0Aassert!(!zero.is_normal())%3B%0Aassert!(!f32%3A%3ANAN.is_normal())%3B%0Aassert!(!f32%3A%3AINFINITY.is_normal())%3B%0A%2F%2F%20Values%20between%20%600%60%20and%20%60min%60%20are%20Subnormal.%0Aassert!(!lower_than_min.is_normal())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.classify' class='method'><span id='classify.v' class='invisible'><code>fn <a href='#tymethod.classify' class='fnname'>classify</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/nightly/core/num/enum.FpCategory.html" title="enum core::num::FpCategory">FpCategory</a></code></span></h3><div class='docblock'><p>Returns the floating point category of the number. If only one property
|
||
is going to be tested, it is generally faster to use the specific
|
||
predicate instead.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">num</span>::<span class="ident">FpCategory</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f32</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">num</span> <span class="op">=</span> <span class="number">12.4f32</span>;
|
||
<span class="kw">let</span> <span class="ident">inf</span> <span class="op">=</span> <span class="ident">f32</span>::<span class="ident">INFINITY</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num</span>.<span class="ident">classify</span>(), <span class="ident">FpCategory</span>::<span class="ident">Normal</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">inf</span>.<span class="ident">classify</span>(), <span class="ident">FpCategory</span>::<span class="ident">Infinite</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Anum%3A%3AFpCategory%3B%0Ause%20std%3A%3Af32%3B%0A%0Alet%20num%20%3D%2012.4f32%3B%0Alet%20inf%20%3D%20f32%3A%3AINFINITY%3B%0A%0Aassert_eq!(num.classify()%2C%20FpCategory%3A%3ANormal)%3B%0Aassert_eq!(inf.classify()%2C%20FpCategory%3A%3AInfinite)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.floor' class='method'><span id='floor.v' class='invisible'><code>fn <a href='#tymethod.floor' class='fnname'>floor</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the largest integer less than or equal to a number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">3.99</span>;
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> <span class="number">3.0</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">floor</span>(), <span class="number">3.0</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">g</span>.<span class="ident">floor</span>(), <span class="number">3.0</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20f%20%3D%203.99%3B%0Alet%20g%20%3D%203.0%3B%0A%0Aassert_eq!(f.floor()%2C%203.0)%3B%0Aassert_eq!(g.floor()%2C%203.0)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.ceil' class='method'><span id='ceil.v' class='invisible'><code>fn <a href='#tymethod.ceil' class='fnname'>ceil</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the smallest integer greater than or equal to a number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">3.01</span>;
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> <span class="number">4.0</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">ceil</span>(), <span class="number">4.0</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">g</span>.<span class="ident">ceil</span>(), <span class="number">4.0</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20f%20%3D%203.01%3B%0Alet%20g%20%3D%204.0%3B%0A%0Aassert_eq!(f.ceil()%2C%204.0)%3B%0Aassert_eq!(g.ceil()%2C%204.0)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.round' class='method'><span id='round.v' class='invisible'><code>fn <a href='#tymethod.round' class='fnname'>round</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the nearest integer to a number. Round half-way cases away from
|
||
<code>0.0</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">3.3</span>;
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.3</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">round</span>(), <span class="number">3.0</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">g</span>.<span class="ident">round</span>(), <span class="op">-</span><span class="number">3.0</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20f%20%3D%203.3%3B%0Alet%20g%20%3D%20-3.3%3B%0A%0Aassert_eq!(f.round()%2C%203.0)%3B%0Aassert_eq!(g.round()%2C%20-3.0)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.trunc' class='method'><span id='trunc.v' class='invisible'><code>fn <a href='#tymethod.trunc' class='fnname'>trunc</a>(self) -> Self</code></span></h3><div class='docblock'><p>Return the integer part of a number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">3.3</span>;
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.7</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">trunc</span>(), <span class="number">3.0</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">g</span>.<span class="ident">trunc</span>(), <span class="op">-</span><span class="number">3.0</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20f%20%3D%203.3%3B%0Alet%20g%20%3D%20-3.7%3B%0A%0Aassert_eq!(f.trunc()%2C%203.0)%3B%0Aassert_eq!(g.trunc()%2C%20-3.0)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.fract' class='method'><span id='fract.v' class='invisible'><code>fn <a href='#tymethod.fract' class='fnname'>fract</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the fractional part of a number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">3.5</span>;
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.5</span>;
|
||
<span class="kw">let</span> <span class="ident">abs_difference_x</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">fract</span>() <span class="op">-</span> <span class="number">0.5</span>).<span class="ident">abs</span>();
|
||
<span class="kw">let</span> <span class="ident">abs_difference_y</span> <span class="op">=</span> (<span class="ident">y</span>.<span class="ident">fract</span>() <span class="op">-</span> (<span class="op">-</span><span class="number">0.5</span>)).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_x</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_y</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%203.5%3B%0Alet%20y%20%3D%20-3.5%3B%0Alet%20abs_difference_x%20%3D%20(x.fract()%20-%200.5).abs()%3B%0Alet%20abs_difference_y%20%3D%20(y.fract()%20-%20(-0.5)).abs()%3B%0A%0Aassert!(abs_difference_x%20%3C%201e-10)%3B%0Aassert!(abs_difference_y%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.abs' class='method'><span id='abs.v' class='invisible'><code>fn <a href='#tymethod.abs' class='fnname'>abs</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the absolute value of <code>self</code>. Returns <code>Float::nan()</code> if the
|
||
number is <code>Float::nan()</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">3.5</span>;
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.5</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference_x</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">abs</span>() <span class="op">-</span> <span class="ident">x</span>).<span class="ident">abs</span>();
|
||
<span class="kw">let</span> <span class="ident">abs_difference_y</span> <span class="op">=</span> (<span class="ident">y</span>.<span class="ident">abs</span>() <span class="op">-</span> (<span class="op">-</span><span class="ident">y</span>)).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_x</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_y</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">f64</span>::<span class="ident">NAN</span>.<span class="ident">abs</span>().<span class="ident">is_nan</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%20%3D%203.5%3B%0Alet%20y%20%3D%20-3.5%3B%0A%0Alet%20abs_difference_x%20%3D%20(x.abs()%20-%20x).abs()%3B%0Alet%20abs_difference_y%20%3D%20(y.abs()%20-%20(-y)).abs()%3B%0A%0Aassert!(abs_difference_x%20%3C%201e-10)%3B%0Aassert!(abs_difference_y%20%3C%201e-10)%3B%0A%0Aassert!(f64%3A%3ANAN.abs().is_nan())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.signum' class='method'><span id='signum.v' class='invisible'><code>fn <a href='#tymethod.signum' class='fnname'>signum</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns a number that represents the sign of <code>self</code>.</p>
|
||
|
||
<ul>
|
||
<li><code>1.0</code> if the number is positive, <code>+0.0</code> or <code>Float::infinity()</code></li>
|
||
<li><code>-1.0</code> if the number is negative, <code>-0.0</code> or <code>Float::neg_infinity()</code></li>
|
||
<li><code>Float::nan()</code> if the number is <code>Float::nan()</code></li>
|
||
</ul>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">3.5</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">signum</span>(), <span class="number">1.0</span>);
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">f64</span>::<span class="ident">NEG_INFINITY</span>.<span class="ident">signum</span>(), <span class="op">-</span><span class="number">1.0</span>);
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">f64</span>::<span class="ident">NAN</span>.<span class="ident">signum</span>().<span class="ident">is_nan</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20f%20%3D%203.5%3B%0A%0Aassert_eq!(f.signum()%2C%201.0)%3B%0Aassert_eq!(f64%3A%3ANEG_INFINITY.signum()%2C%20-1.0)%3B%0A%0Aassert!(f64%3A%3ANAN.signum().is_nan())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.is_sign_positive' class='method'><span id='is_sign_positive.v' class='invisible'><code>fn <a href='#tymethod.is_sign_positive' class='fnname'>is_sign_positive</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Returns <code>true</code> if <code>self</code> is positive, including <code>+0.0</code> and
|
||
<code>Float::infinity()</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">nan</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">NAN</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">7.0</span>;
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> <span class="op">-</span><span class="number">7.0</span>;
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">is_sign_positive</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">g</span>.<span class="ident">is_sign_positive</span>());
|
||
<span class="comment">// Requires both tests to determine if is `NaN`</span>
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">nan</span>.<span class="ident">is_sign_positive</span>() <span class="op">&&</span> <span class="op">!</span><span class="ident">nan</span>.<span class="ident">is_sign_negative</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20nan%3A%20f64%20%3D%20f64%3A%3ANAN%3B%0A%0Alet%20f%20%3D%207.0%3B%0Alet%20g%20%3D%20-7.0%3B%0A%0Aassert!(f.is_sign_positive())%3B%0Aassert!(!g.is_sign_positive())%3B%0A%2F%2F%20Requires%20both%20tests%20to%20determine%20if%20is%20%60NaN%60%0Aassert!(!nan.is_sign_positive()%20%26%26%20!nan.is_sign_negative())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.is_sign_negative' class='method'><span id='is_sign_negative.v' class='invisible'><code>fn <a href='#tymethod.is_sign_negative' class='fnname'>is_sign_negative</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.bool.html">bool</a></code></span></h3><div class='docblock'><p>Returns <code>true</code> if <code>self</code> is negative, including <code>-0.0</code> and
|
||
<code>Float::neg_infinity()</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">nan</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">NAN</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">7.0</span>;
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> <span class="op">-</span><span class="number">7.0</span>;
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">f</span>.<span class="ident">is_sign_negative</span>());
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">g</span>.<span class="ident">is_sign_negative</span>());
|
||
<span class="comment">// Requires both tests to determine if is `NaN`.</span>
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="op">!</span><span class="ident">nan</span>.<span class="ident">is_sign_positive</span>() <span class="op">&&</span> <span class="op">!</span><span class="ident">nan</span>.<span class="ident">is_sign_negative</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20nan%20%3D%20f64%3A%3ANAN%3B%0A%0Alet%20f%20%3D%207.0%3B%0Alet%20g%20%3D%20-7.0%3B%0A%0Aassert!(!f.is_sign_negative())%3B%0Aassert!(g.is_sign_negative())%3B%0A%2F%2F%20Requires%20both%20tests%20to%20determine%20if%20is%20%60NaN%60.%0Aassert!(!nan.is_sign_positive()%20%26%26%20!nan.is_sign_negative())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.mul_add' class='method'><span id='mul_add.v' class='invisible'><code>fn <a href='#tymethod.mul_add' class='fnname'>mul_add</a>(self, a: Self, b: Self) -> Self</code></span></h3><div class='docblock'><p>Fused multiply-add. Computes <code>(self * a) + b</code> with only one rounding
|
||
error. This produces a more accurate result with better performance than
|
||
a separate multiplication operation followed by an add.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">m</span> <span class="op">=</span> <span class="number">10.0</span>;
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">4.0</span>;
|
||
<span class="kw">let</span> <span class="ident">b</span> <span class="op">=</span> <span class="number">60.0</span>;
|
||
|
||
<span class="comment">// 100.0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">m</span>.<span class="ident">mul_add</span>(<span class="ident">x</span>, <span class="ident">b</span>) <span class="op">-</span> (<span class="ident">m</span><span class="kw-2">*</span><span class="ident">x</span> <span class="op">+</span> <span class="ident">b</span>)).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20m%20%3D%2010.0%3B%0Alet%20x%20%3D%204.0%3B%0Alet%20b%20%3D%2060.0%3B%0A%0A%2F%2F%20100.0%0Alet%20abs_difference%20%3D%20(m.mul_add(x%2C%20b)%20-%20(m*x%20%2B%20b)).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.recip' class='method'><span id='recip.v' class='invisible'><code>fn <a href='#tymethod.recip' class='fnname'>recip</a>(self) -> Self</code></span></h3><div class='docblock'><p>Take the reciprocal (inverse) of a number, <code>1/x</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">recip</span>() <span class="op">-</span> (<span class="number">1.0</span><span class="op">/</span><span class="ident">x</span>)).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%202.0%3B%0Alet%20abs_difference%20%3D%20(x.recip()%20-%20(1.0%2Fx)).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.powi' class='method'><span id='powi.v' class='invisible'><code>fn <a href='#tymethod.powi' class='fnname'>powi</a>(self, n: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i32.html">i32</a>) -> Self</code></span></h3><div class='docblock'><p>Raise a number to an integer power.</p>
|
||
|
||
<p>Using this function is generally faster than using <code>powf</code></p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">powi</span>(<span class="number">2</span>) <span class="op">-</span> <span class="ident">x</span><span class="kw-2">*</span><span class="ident">x</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%202.0%3B%0Alet%20abs_difference%20%3D%20(x.powi(2)%20-%20x*x).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.powf' class='method'><span id='powf.v' class='invisible'><code>fn <a href='#tymethod.powf' class='fnname'>powf</a>(self, n: Self) -> Self</code></span></h3><div class='docblock'><p>Raise a number to a floating point power.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">powf</span>(<span class="number">2.0</span>) <span class="op">-</span> <span class="ident">x</span><span class="kw-2">*</span><span class="ident">x</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%202.0%3B%0Alet%20abs_difference%20%3D%20(x.powf(2.0)%20-%20x*x).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.sqrt' class='method'><span id='sqrt.v' class='invisible'><code>fn <a href='#tymethod.sqrt' class='fnname'>sqrt</a>(self) -> Self</code></span></h3><div class='docblock'><p>Take the square root of a number.</p>
|
||
|
||
<p>Returns NaN if <code>self</code> is a negative number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">positive</span> <span class="op">=</span> <span class="number">4.0</span>;
|
||
<span class="kw">let</span> <span class="ident">negative</span> <span class="op">=</span> <span class="op">-</span><span class="number">4.0</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">positive</span>.<span class="ident">sqrt</span>() <span class="op">-</span> <span class="number">2.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">negative</span>.<span class="ident">sqrt</span>().<span class="ident">is_nan</span>());<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20positive%20%3D%204.0%3B%0Alet%20negative%20%3D%20-4.0%3B%0A%0Alet%20abs_difference%20%3D%20(positive.sqrt()%20-%202.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0Aassert!(negative.sqrt().is_nan())%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.exp' class='method'><span id='exp.v' class='invisible'><code>fn <a href='#tymethod.exp' class='fnname'>exp</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns <code>e^(self)</code>, (the exponential function).</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">one</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="comment">// e^1</span>
|
||
<span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="ident">one</span>.<span class="ident">exp</span>();
|
||
|
||
<span class="comment">// ln(e) - 1 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">e</span>.<span class="ident">ln</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20one%20%3D%201.0%3B%0A%2F%2F%20e%5E1%0Alet%20e%20%3D%20one.exp()%3B%0A%0A%2F%2F%20ln(e)%20-%201%20%3D%3D%200%0Alet%20abs_difference%20%3D%20(e.ln()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.exp2' class='method'><span id='exp2.v' class='invisible'><code>fn <a href='#tymethod.exp2' class='fnname'>exp2</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns <code>2^(self)</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
|
||
<span class="comment">// 2^2 - 4 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span>.<span class="ident">exp2</span>() <span class="op">-</span> <span class="number">4.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20f%20%3D%202.0%3B%0A%0A%2F%2F%202%5E2%20-%204%20%3D%3D%200%0Alet%20abs_difference%20%3D%20(f.exp2()%20-%204.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.ln' class='method'><span id='ln.v' class='invisible'><code>fn <a href='#tymethod.ln' class='fnname'>ln</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the natural logarithm of the number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">one</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="comment">// e^1</span>
|
||
<span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="ident">one</span>.<span class="ident">exp</span>();
|
||
|
||
<span class="comment">// ln(e) - 1 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">e</span>.<span class="ident">ln</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20one%20%3D%201.0%3B%0A%2F%2F%20e%5E1%0Alet%20e%20%3D%20one.exp()%3B%0A%0A%2F%2F%20ln(e)%20-%201%20%3D%3D%200%0Alet%20abs_difference%20%3D%20(e.ln()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.log' class='method'><span id='log.v' class='invisible'><code>fn <a href='#tymethod.log' class='fnname'>log</a>(self, base: Self) -> Self</code></span></h3><div class='docblock'><p>Returns the logarithm of the number with respect to an arbitrary base.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">ten</span> <span class="op">=</span> <span class="number">10.0</span>;
|
||
<span class="kw">let</span> <span class="ident">two</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
|
||
<span class="comment">// log10(10) - 1 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference_10</span> <span class="op">=</span> (<span class="ident">ten</span>.<span class="ident">log</span>(<span class="number">10.0</span>) <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="comment">// log2(2) - 1 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference_2</span> <span class="op">=</span> (<span class="ident">two</span>.<span class="ident">log</span>(<span class="number">2.0</span>) <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_10</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_2</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20ten%20%3D%2010.0%3B%0Alet%20two%20%3D%202.0%3B%0A%0A%2F%2F%20log10(10)%20-%201%20%3D%3D%200%0Alet%20abs_difference_10%20%3D%20(ten.log(10.0)%20-%201.0).abs()%3B%0A%0A%2F%2F%20log2(2)%20-%201%20%3D%3D%200%0Alet%20abs_difference_2%20%3D%20(two.log(2.0)%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference_10%20%3C%201e-10)%3B%0Aassert!(abs_difference_2%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.log2' class='method'><span id='log2.v' class='invisible'><code>fn <a href='#tymethod.log2' class='fnname'>log2</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the base 2 logarithm of the number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">two</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
|
||
<span class="comment">// log2(2) - 1 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">two</span>.<span class="ident">log2</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20two%20%3D%202.0%3B%0A%0A%2F%2F%20log2(2)%20-%201%20%3D%3D%200%0Alet%20abs_difference%20%3D%20(two.log2()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.log10' class='method'><span id='log10.v' class='invisible'><code>fn <a href='#tymethod.log10' class='fnname'>log10</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns the base 10 logarithm of the number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">ten</span> <span class="op">=</span> <span class="number">10.0</span>;
|
||
|
||
<span class="comment">// log10(10) - 1 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">ten</span>.<span class="ident">log10</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20ten%20%3D%2010.0%3B%0A%0A%2F%2F%20log10(10)%20-%201%20%3D%3D%200%0Alet%20abs_difference%20%3D%20(ten.log10()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.max' class='method'><span id='max.v' class='invisible'><code>fn <a href='#tymethod.max' class='fnname'>max</a>(self, other: Self) -> Self</code></span></h3><div class='docblock'><p>Returns the maximum of the two numbers.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>.<span class="ident">max</span>(<span class="ident">y</span>), <span class="ident">y</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%201.0%3B%0Alet%20y%20%3D%202.0%3B%0A%0Aassert_eq!(x.max(y)%2C%20y)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.min' class='method'><span id='min.v' class='invisible'><code>fn <a href='#tymethod.min' class='fnname'>min</a>(self, other: Self) -> Self</code></span></h3><div class='docblock'><p>Returns the minimum of the two numbers.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>.<span class="ident">min</span>(<span class="ident">y</span>), <span class="ident">x</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%201.0%3B%0Alet%20y%20%3D%202.0%3B%0A%0Aassert_eq!(x.min(y)%2C%20x)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.abs_sub' class='method'><span id='abs_sub.v' class='invisible'><code>fn <a href='#tymethod.abs_sub' class='fnname'>abs_sub</a>(self, other: Self) -> Self</code></span></h3><div class='docblock'><p>The positive difference of two numbers.</p>
|
||
|
||
<ul>
|
||
<li>If <code>self <= other</code>: <code>0:0</code></li>
|
||
<li>Else: <code>self - other</code></li>
|
||
</ul>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">3.0</span>;
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.0</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference_x</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">abs_sub</span>(<span class="number">1.0</span>) <span class="op">-</span> <span class="number">2.0</span>).<span class="ident">abs</span>();
|
||
<span class="kw">let</span> <span class="ident">abs_difference_y</span> <span class="op">=</span> (<span class="ident">y</span>.<span class="ident">abs_sub</span>(<span class="number">1.0</span>) <span class="op">-</span> <span class="number">0.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_x</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_y</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%203.0%3B%0Alet%20y%20%3D%20-3.0%3B%0A%0Alet%20abs_difference_x%20%3D%20(x.abs_sub(1.0)%20-%202.0).abs()%3B%0Alet%20abs_difference_y%20%3D%20(y.abs_sub(1.0)%20-%200.0).abs()%3B%0A%0Aassert!(abs_difference_x%20%3C%201e-10)%3B%0Aassert!(abs_difference_y%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.cbrt' class='method'><span id='cbrt.v' class='invisible'><code>fn <a href='#tymethod.cbrt' class='fnname'>cbrt</a>(self) -> Self</code></span></h3><div class='docblock'><p>Take the cubic root of a number.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">8.0</span>;
|
||
|
||
<span class="comment">// x^(1/3) - 2 == 0</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">cbrt</span>() <span class="op">-</span> <span class="number">2.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%208.0%3B%0A%0A%2F%2F%20x%5E(1%2F3)%20-%202%20%3D%3D%200%0Alet%20abs_difference%20%3D%20(x.cbrt()%20-%202.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.hypot' class='method'><span id='hypot.v' class='invisible'><code>fn <a href='#tymethod.hypot' class='fnname'>hypot</a>(self, other: Self) -> Self</code></span></h3><div class='docblock'><p>Calculate the length of the hypotenuse of a right-angle triangle given
|
||
legs of length <code>x</code> and <code>y</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">2.0</span>;
|
||
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="number">3.0</span>;
|
||
|
||
<span class="comment">// sqrt(x^2 + y^2)</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">hypot</span>(<span class="ident">y</span>) <span class="op">-</span> (<span class="ident">x</span>.<span class="ident">powi</span>(<span class="number">2</span>) <span class="op">+</span> <span class="ident">y</span>.<span class="ident">powi</span>(<span class="number">2</span>)).<span class="ident">sqrt</span>()).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%202.0%3B%0Alet%20y%20%3D%203.0%3B%0A%0A%2F%2F%20sqrt(x%5E2%20%2B%20y%5E2)%0Alet%20abs_difference%20%3D%20(x.hypot(y)%20-%20(x.powi(2)%20%2B%20y.powi(2)).sqrt()).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.sin' class='method'><span id='sin.v' class='invisible'><code>fn <a href='#tymethod.sin' class='fnname'>sin</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the sine of a number (in radians).</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span><span class="op">/</span><span class="number">2.0</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">sin</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%20%3D%20f64%3A%3Aconsts%3A%3API%2F2.0%3B%0A%0Alet%20abs_difference%20%3D%20(x.sin()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.cos' class='method'><span id='cos.v' class='invisible'><code>fn <a href='#tymethod.cos' class='fnname'>cos</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the cosine of a number (in radians).</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">2.0</span><span class="kw-2">*</span><span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">cos</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%20%3D%202.0*f64%3A%3Aconsts%3A%3API%3B%0A%0Alet%20abs_difference%20%3D%20(x.cos()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.tan' class='method'><span id='tan.v' class='invisible'><code>fn <a href='#tymethod.tan' class='fnname'>tan</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the tangent of a number (in radians).</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span><span class="op">/</span><span class="number">4.0</span>;
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">tan</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-14</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%20%3D%20f64%3A%3Aconsts%3A%3API%2F4.0%3B%0Alet%20abs_difference%20%3D%20(x.tan()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-14)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.asin' class='method'><span id='asin.v' class='invisible'><code>fn <a href='#tymethod.asin' class='fnname'>asin</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the arcsine of a number. Return value is in radians in
|
||
the range [-pi/2, pi/2] or NaN if the number is outside the range
|
||
[-1, 1].</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span> <span class="op">/</span> <span class="number">2.0</span>;
|
||
|
||
<span class="comment">// asin(sin(pi/2))</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span>.<span class="ident">sin</span>().<span class="ident">asin</span>() <span class="op">-</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span> <span class="op">/</span> <span class="number">2.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20f%20%3D%20f64%3A%3Aconsts%3A%3API%20%2F%202.0%3B%0A%0A%2F%2F%20asin(sin(pi%2F2))%0Alet%20abs_difference%20%3D%20(f.sin().asin()%20-%20f64%3A%3Aconsts%3A%3API%20%2F%202.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.acos' class='method'><span id='acos.v' class='invisible'><code>fn <a href='#tymethod.acos' class='fnname'>acos</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the arccosine of a number. Return value is in radians in
|
||
the range [0, pi] or NaN if the number is outside the range
|
||
[-1, 1].</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span> <span class="op">/</span> <span class="number">4.0</span>;
|
||
|
||
<span class="comment">// acos(cos(pi/4))</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span>.<span class="ident">cos</span>().<span class="ident">acos</span>() <span class="op">-</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span> <span class="op">/</span> <span class="number">4.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20f%20%3D%20f64%3A%3Aconsts%3A%3API%20%2F%204.0%3B%0A%0A%2F%2F%20acos(cos(pi%2F4))%0Alet%20abs_difference%20%3D%20(f.cos().acos()%20-%20f64%3A%3Aconsts%3A%3API%20%2F%204.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.atan' class='method'><span id='atan.v' class='invisible'><code>fn <a href='#tymethod.atan' class='fnname'>atan</a>(self) -> Self</code></span></h3><div class='docblock'><p>Computes the arctangent of a number. Return value is in radians in the
|
||
range [-pi/2, pi/2];</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
|
||
<span class="comment">// atan(tan(1))</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span>.<span class="ident">tan</span>().<span class="ident">atan</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20f%20%3D%201.0%3B%0A%0A%2F%2F%20atan(tan(1))%0Alet%20abs_difference%20%3D%20(f.tan().atan()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.atan2' class='method'><span id='atan2.v' class='invisible'><code>fn <a href='#tymethod.atan2' class='fnname'>atan2</a>(self, other: Self) -> Self</code></span></h3><div class='docblock'><p>Computes the four quadrant arctangent of <code>self</code> (<code>y</code>) and <code>other</code> (<code>x</code>).</p>
|
||
|
||
<ul>
|
||
<li><code>x = 0</code>, <code>y = 0</code>: <code>0</code></li>
|
||
<li><code>x >= 0</code>: <code>arctan(y/x)</code> -> <code>[-pi/2, pi/2]</code></li>
|
||
<li><code>y >= 0</code>: <code>arctan(y/x) + pi</code> -> <code>(pi/2, pi]</code></li>
|
||
<li><code>y < 0</code>: <code>arctan(y/x) - pi</code> -> <code>(-pi, -pi/2)</code></li>
|
||
</ul>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">pi</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span>;
|
||
<span class="comment">// All angles from horizontal right (+x)</span>
|
||
<span class="comment">// 45 deg counter-clockwise</span>
|
||
<span class="kw">let</span> <span class="ident">x1</span> <span class="op">=</span> <span class="number">3.0</span>;
|
||
<span class="kw">let</span> <span class="ident">y1</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.0</span>;
|
||
|
||
<span class="comment">// 135 deg clockwise</span>
|
||
<span class="kw">let</span> <span class="ident">x2</span> <span class="op">=</span> <span class="op">-</span><span class="number">3.0</span>;
|
||
<span class="kw">let</span> <span class="ident">y2</span> <span class="op">=</span> <span class="number">3.0</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference_1</span> <span class="op">=</span> (<span class="ident">y1</span>.<span class="ident">atan2</span>(<span class="ident">x1</span>) <span class="op">-</span> (<span class="op">-</span><span class="ident">pi</span><span class="op">/</span><span class="number">4.0</span>)).<span class="ident">abs</span>();
|
||
<span class="kw">let</span> <span class="ident">abs_difference_2</span> <span class="op">=</span> (<span class="ident">y2</span>.<span class="ident">atan2</span>(<span class="ident">x2</span>) <span class="op">-</span> <span class="number">3.0</span><span class="kw-2">*</span><span class="ident">pi</span><span class="op">/</span><span class="number">4.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_1</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_2</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20pi%20%3D%20f64%3A%3Aconsts%3A%3API%3B%0A%2F%2F%20All%20angles%20from%20horizontal%20right%20(%2Bx)%0A%2F%2F%2045%20deg%20counter-clockwise%0Alet%20x1%20%3D%203.0%3B%0Alet%20y1%20%3D%20-3.0%3B%0A%0A%2F%2F%20135%20deg%20clockwise%0Alet%20x2%20%3D%20-3.0%3B%0Alet%20y2%20%3D%203.0%3B%0A%0Alet%20abs_difference_1%20%3D%20(y1.atan2(x1)%20-%20(-pi%2F4.0)).abs()%3B%0Alet%20abs_difference_2%20%3D%20(y2.atan2(x2)%20-%203.0*pi%2F4.0).abs()%3B%0A%0Aassert!(abs_difference_1%20%3C%201e-10)%3B%0Aassert!(abs_difference_2%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.sin_cos' class='method'><span id='sin_cos.v' class='invisible'><code>fn <a href='#tymethod.sin_cos' class='fnname'>sin_cos</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>Self, Self<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a></code></span></h3><div class='docblock'><p>Simultaneously computes the sine and cosine of the number, <code>x</code>. Returns
|
||
<code>(sin(x), cos(x))</code>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">PI</span><span class="op">/</span><span class="number">4.0</span>;
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">sin_cos</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference_0</span> <span class="op">=</span> (<span class="ident">f</span>.<span class="number">0</span> <span class="op">-</span> <span class="ident">x</span>.<span class="ident">sin</span>()).<span class="ident">abs</span>();
|
||
<span class="kw">let</span> <span class="ident">abs_difference_1</span> <span class="op">=</span> (<span class="ident">f</span>.<span class="number">1</span> <span class="op">-</span> <span class="ident">x</span>.<span class="ident">cos</span>()).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_0</span> <span class="op"><</span> <span class="number">1e-10</span>);
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference_0</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%20%3D%20f64%3A%3Aconsts%3A%3API%2F4.0%3B%0Alet%20f%20%3D%20x.sin_cos()%3B%0A%0Alet%20abs_difference_0%20%3D%20(f.0%20-%20x.sin()).abs()%3B%0Alet%20abs_difference_1%20%3D%20(f.1%20-%20x.cos()).abs()%3B%0A%0Aassert!(abs_difference_0%20%3C%201e-10)%3B%0Aassert!(abs_difference_0%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.exp_m1' class='method'><span id='exp_m1.v' class='invisible'><code>fn <a href='#tymethod.exp_m1' class='fnname'>exp_m1</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns <code>e^(self) - 1</code> in a way that is accurate even if the
|
||
number is close to zero.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">7.0</span>;
|
||
|
||
<span class="comment">// e^(ln(7)) - 1</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">ln</span>().<span class="ident">exp_m1</span>() <span class="op">-</span> <span class="number">6.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%207.0%3B%0A%0A%2F%2F%20e%5E(ln(7))%20-%201%0Alet%20abs_difference%20%3D%20(x.ln().exp_m1()%20-%206.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.ln_1p' class='method'><span id='ln_1p.v' class='invisible'><code>fn <a href='#tymethod.ln_1p' class='fnname'>ln_1p</a>(self) -> Self</code></span></h3><div class='docblock'><p>Returns <code>ln(1+n)</code> (natural logarithm) more accurately than if
|
||
the operations were performed separately.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">E</span> <span class="op">-</span> <span class="number">1.0</span>;
|
||
|
||
<span class="comment">// ln(1 + (e - 1)) == ln(e) == 1</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">x</span>.<span class="ident">ln_1p</span>() <span class="op">-</span> <span class="number">1.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%20%3D%20f64%3A%3Aconsts%3A%3AE%20-%201.0%3B%0A%0A%2F%2F%20ln(1%20%2B%20(e%20-%201))%20%3D%3D%20ln(e)%20%3D%3D%201%0Alet%20abs_difference%20%3D%20(x.ln_1p()%20-%201.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.sinh' class='method'><span id='sinh.v' class='invisible'><code>fn <a href='#tymethod.sinh' class='fnname'>sinh</a>(self) -> Self</code></span></h3><div class='docblock'><p>Hyperbolic sine function.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">E</span>;
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">sinh</span>();
|
||
<span class="comment">// Solving sinh() at 1 gives `(e^2-1)/(2e)`</span>
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> (<span class="ident">e</span><span class="kw-2">*</span><span class="ident">e</span> <span class="op">-</span> <span class="number">1.0</span>)<span class="op">/</span>(<span class="number">2.0</span><span class="kw-2">*</span><span class="ident">e</span>);
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span> <span class="op">-</span> <span class="ident">g</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20e%20%3D%20f64%3A%3Aconsts%3A%3AE%3B%0Alet%20x%20%3D%201.0%3B%0A%0Alet%20f%20%3D%20x.sinh()%3B%0A%2F%2F%20Solving%20sinh()%20at%201%20gives%20%60(e%5E2-1)%2F(2e)%60%0Alet%20g%20%3D%20(e*e%20-%201.0)%2F(2.0*e)%3B%0Alet%20abs_difference%20%3D%20(f%20-%20g).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.cosh' class='method'><span id='cosh.v' class='invisible'><code>fn <a href='#tymethod.cosh' class='fnname'>cosh</a>(self) -> Self</code></span></h3><div class='docblock'><p>Hyperbolic cosine function.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">E</span>;
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">cosh</span>();
|
||
<span class="comment">// Solving cosh() at 1 gives this result</span>
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> (<span class="ident">e</span><span class="kw-2">*</span><span class="ident">e</span> <span class="op">+</span> <span class="number">1.0</span>)<span class="op">/</span>(<span class="number">2.0</span><span class="kw-2">*</span><span class="ident">e</span>);
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span> <span class="op">-</span> <span class="ident">g</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="comment">// Same result</span>
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1.0e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20e%20%3D%20f64%3A%3Aconsts%3A%3AE%3B%0Alet%20x%20%3D%201.0%3B%0Alet%20f%20%3D%20x.cosh()%3B%0A%2F%2F%20Solving%20cosh()%20at%201%20gives%20this%20result%0Alet%20g%20%3D%20(e*e%20%2B%201.0)%2F(2.0*e)%3B%0Alet%20abs_difference%20%3D%20(f%20-%20g).abs()%3B%0A%0A%2F%2F%20Same%20result%0Aassert!(abs_difference%20%3C%201.0e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.tanh' class='method'><span id='tanh.v' class='invisible'><code>fn <a href='#tymethod.tanh' class='fnname'>tanh</a>(self) -> Self</code></span></h3><div class='docblock'><p>Hyperbolic tangent function.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">E</span>;
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">tanh</span>();
|
||
<span class="comment">// Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`</span>
|
||
<span class="kw">let</span> <span class="ident">g</span> <span class="op">=</span> (<span class="number">1.0</span> <span class="op">-</span> <span class="ident">e</span>.<span class="ident">powi</span>(<span class="op">-</span><span class="number">2</span>))<span class="op">/</span>(<span class="number">1.0</span> <span class="op">+</span> <span class="ident">e</span>.<span class="ident">powi</span>(<span class="op">-</span><span class="number">2</span>));
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span> <span class="op">-</span> <span class="ident">g</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1.0e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20e%20%3D%20f64%3A%3Aconsts%3A%3AE%3B%0Alet%20x%20%3D%201.0%3B%0A%0Alet%20f%20%3D%20x.tanh()%3B%0A%2F%2F%20Solving%20tanh()%20at%201%20gives%20%60(1%20-%20e%5E(-2))%2F(1%20%2B%20e%5E(-2))%60%0Alet%20g%20%3D%20(1.0%20-%20e.powi(-2))%2F(1.0%20%2B%20e.powi(-2))%3B%0Alet%20abs_difference%20%3D%20(f%20-%20g).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201.0e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.asinh' class='method'><span id='asinh.v' class='invisible'><code>fn <a href='#tymethod.asinh' class='fnname'>asinh</a>(self) -> Self</code></span></h3><div class='docblock'><p>Inverse hyperbolic sine function.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">sinh</span>().<span class="ident">asinh</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span> <span class="op">-</span> <span class="ident">x</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1.0e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%201.0%3B%0Alet%20f%20%3D%20x.sinh().asinh()%3B%0A%0Alet%20abs_difference%20%3D%20(f%20-%20x).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201.0e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.acosh' class='method'><span id='acosh.v' class='invisible'><code>fn <a href='#tymethod.acosh' class='fnname'>acosh</a>(self) -> Self</code></span></h3><div class='docblock'><p>Inverse hyperbolic cosine function.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="number">1.0</span>;
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">x</span>.<span class="ident">cosh</span>().<span class="ident">acosh</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span> <span class="op">-</span> <span class="ident">x</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1.0e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20x%20%3D%201.0%3B%0Alet%20f%20%3D%20x.cosh().acosh()%3B%0A%0Alet%20abs_difference%20%3D%20(f%20-%20x).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201.0e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.atanh' class='method'><span id='atanh.v' class='invisible'><code>fn <a href='#tymethod.atanh' class='fnname'>atanh</a>(self) -> Self</code></span></h3><div class='docblock'><p>Inverse hyperbolic tangent function.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">e</span> <span class="op">=</span> <span class="ident">f64</span>::<span class="ident">consts</span>::<span class="ident">E</span>;
|
||
<span class="kw">let</span> <span class="ident">f</span> <span class="op">=</span> <span class="ident">e</span>.<span class="ident">tanh</span>().<span class="ident">atanh</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">f</span> <span class="op">-</span> <span class="ident">e</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1.0e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20e%20%3D%20f64%3A%3Aconsts%3A%3AE%3B%0Alet%20f%20%3D%20e.tanh().atanh()%3B%0A%0Alet%20abs_difference%20%3D%20(f%20-%20e).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201.0e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='tymethod.integer_decode' class='method'><span id='integer_decode.v' class='invisible'><code>fn <a href='#tymethod.integer_decode' class='fnname'>integer_decode</a>(self) -> <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u64.html">u64</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i16.html">i16</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.i8.html">i8</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a></code></span></h3><div class='docblock'><p>Returns the mantissa, base 2 exponent, and sign as integers, respectively.
|
||
The original number can be recovered by <code>sign * mantissa * 2 ^ exponent</code>.
|
||
The floating point encoding is documented in the <a href="../../../../../reference.html#machine-types">Reference</a>.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">num</span> <span class="op">=</span> <span class="number">2.0f32</span>;
|
||
|
||
<span class="comment">// (8388608, -22, 1)</span>
|
||
<span class="kw">let</span> (<span class="ident">mantissa</span>, <span class="ident">exponent</span>, <span class="ident">sign</span>) <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">integer_decode</span>(<span class="ident">num</span>);
|
||
<span class="kw">let</span> <span class="ident">sign_f</span> <span class="op">=</span> <span class="ident">sign</span> <span class="kw">as</span> <span class="ident">f32</span>;
|
||
<span class="kw">let</span> <span class="ident">mantissa_f</span> <span class="op">=</span> <span class="ident">mantissa</span> <span class="kw">as</span> <span class="ident">f32</span>;
|
||
<span class="kw">let</span> <span class="ident">exponent_f</span> <span class="op">=</span> <span class="ident">num</span>.<span class="ident">powf</span>(<span class="ident">exponent</span> <span class="kw">as</span> <span class="ident">f32</span>);
|
||
|
||
<span class="comment">// 1 * 8388608 * 2^(-22) == 2</span>
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">sign_f</span> <span class="op">*</span> <span class="ident">mantissa_f</span> <span class="op">*</span> <span class="ident">exponent_f</span> <span class="op">-</span> <span class="ident">num</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0A%0Alet%20num%20%3D%202.0f32%3B%0A%0A%2F%2F%20(8388608%2C%20-22%2C%201)%0Alet%20(mantissa%2C%20exponent%2C%20sign)%20%3D%20Float%3A%3Ainteger_decode(num)%3B%0Alet%20sign_f%20%3D%20sign%20as%20f32%3B%0Alet%20mantissa_f%20%3D%20mantissa%20as%20f32%3B%0Alet%20exponent_f%20%3D%20num.powf(exponent%20as%20f32)%3B%0A%0A%2F%2F%201%20*%208388608%20*%202%5E(-22)%20%3D%3D%202%0Alet%20abs_difference%20%3D%20(sign_f%20*%20mantissa_f%20*%20exponent_f%20-%20num).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div></div>
|
||
<h2 id='provided-methods'>Provided Methods</h2>
|
||
<div class='methods'>
|
||
<h3 id='method.epsilon' class='method'><span id='epsilon.v' class='invisible'><code>fn <a href='#method.epsilon' class='fnname'>epsilon</a>() -> Self</code></span></h3><div class='docblock'><p>Returns epsilon, a small positive value.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">num_traits</span>::<span class="ident">Float</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span>: <span class="ident">f64</span> <span class="op">=</span> <span class="ident">Float</span>::<span class="ident">epsilon</span>();
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">x</span>, <span class="ident">f64</span>::<span class="ident">EPSILON</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=extern%20crate%20num%3B%0Afn%20main()%20%7B%0Ause%20num_traits%3A%3AFloat%3B%0Ause%20std%3A%3Af64%3B%0A%0Alet%20x%3A%20f64%20%3D%20Float%3A%3Aepsilon()%3B%0A%0Aassert_eq!(x%2C%20f64%3A%3AEPSILON)%3B%0A%7D">Run</a></pre>
|
||
|
||
<h1 id='panics' class='section-header'><a href='#panics'>Panics</a></h1>
|
||
<p>The default implementation will panic if <code>f32::EPSILON</code> cannot
|
||
be cast to <code>Self</code>.</p>
|
||
</div><h3 id='method.to_degrees' class='method'><span id='to_degrees.v' class='invisible'><code>fn <a href='#method.to_degrees' class='fnname'>to_degrees</a>(self) -> Self</code></span></h3><div class='docblock'><p>Converts radians to degrees.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>::<span class="ident">consts</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">angle</span> <span class="op">=</span> <span class="ident">consts</span>::<span class="ident">PI</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">angle</span>.<span class="ident">to_degrees</span>() <span class="op">-</span> <span class="number">180.0</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=fn%20main()%20%7B%0Ause%20std%3A%3Af64%3A%3Aconsts%3B%0A%0Alet%20angle%20%3D%20consts%3A%3API%3B%0A%0Alet%20abs_difference%20%3D%20(angle.to_degrees()%20-%20180.0).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div><h3 id='method.to_radians' class='method'><span id='to_radians.v' class='invisible'><code>fn <a href='#method.to_radians' class='fnname'>to_radians</a>(self) -> Self</code></span></h3><div class='docblock'><p>Converts degrees to radians.</p>
|
||
|
||
<pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">f64</span>::<span class="ident">consts</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">angle</span> <span class="op">=</span> <span class="number">180.0_f64</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">abs_difference</span> <span class="op">=</span> (<span class="ident">angle</span>.<span class="ident">to_radians</span>() <span class="op">-</span> <span class="ident">consts</span>::<span class="ident">PI</span>).<span class="ident">abs</span>();
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">abs_difference</span> <span class="op"><</span> <span class="number">1e-10</span>);<a class="test-arrow" target="_blank" href="http://play.integer32.com/?code=fn%20main()%20%7B%0Ause%20std%3A%3Af64%3A%3Aconsts%3B%0A%0Alet%20angle%20%3D%20180.0_f64%3B%0A%0Alet%20abs_difference%20%3D%20(angle.to_radians()%20-%20consts%3A%3API).abs()%3B%0A%0Aassert!(abs_difference%20%3C%201e-10)%3B%0A%7D">Run</a></pre>
|
||
</div></div>
|
||
<h2 id='implementors'>Implementors</h2>
|
||
<ul class='item-list' id='implementors-list'>
|
||
<li><code>impl Float for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f32.html">f32</a></code></li>
|
||
<li><code>impl Float for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.f64.html">f64</a></code></li>
|
||
</ul><script type="text/javascript" async
|
||
src="../implementors/num_traits/float/trait.Float.js">
|
||
</script></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>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</dd>
|
||
<dt>+</dt>
|
||
<dd>Collapse/expand 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>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../";
|
||
window.currentCrate = "num";
|
||
</script>
|
||
<script src="../main.js"></script>
|
||
<script defer src="../search-index.js"></script>
|
||
</body>
|
||
</html> |