125 lines
22 KiB
HTML
125 lines
22 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="A crate that provides facilities for testing the approximate equality of floating-point based types, using either relative difference, or units in the last place (ULPs) comparisons."><meta name="keywords" content="rust, rustlang, rust-lang, approx"><title>approx - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.js"></script><script defer src="../main.js"></script>
|
||
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">☰</div><a class="sidebar-logo" href="../approx/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.png" alt="logo"></div>
|
||
</a><h2 class="location">Crate approx</h2><div class="block version"><div class="narrow-helper"></div><p>Version 0.5.1</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all approx's items</p></a><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div><div id="sidebar-vars" data-name="approx" data-ty="mod" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../approx/index.html"><img class="rust-logo" src="../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">approx</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></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/approx/lib.rs.html#15-388" title="goto source code">[src]</a></span></h1><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A crate that provides facilities for testing the approximate equality of floating-point
|
||
based types, using either relative difference, or units in the last place (ULPs)
|
||
comparisons.</p>
|
||
<p>You can also use the <code>*_{eq, ne}!</code> and <code>assert_*_{eq, ne}!</code> macros to test for equality using a
|
||
more positional style:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">macro_use</span>]</span>
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">approx</span>;
|
||
|
||
<span class="kw">use</span> <span class="ident">std::f64</span>;
|
||
|
||
<span class="macro">abs_diff_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>);
|
||
<span class="macro">abs_diff_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>);
|
||
|
||
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>);
|
||
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>);
|
||
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">max_relative</span> <span class="op">=</span> <span class="number">1.0</span>);
|
||
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>, <span class="ident">max_relative</span> <span class="op">=</span> <span class="number">1.0</span>);
|
||
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">max_relative</span> <span class="op">=</span> <span class="number">1.0</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>);
|
||
|
||
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>);
|
||
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>);
|
||
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">max_ulps</span> <span class="op">=</span> <span class="number">4</span>);
|
||
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>, <span class="ident">max_ulps</span> <span class="op">=</span> <span class="number">4</span>);
|
||
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, <span class="ident">max_ulps</span> <span class="op">=</span> <span class="number">4</span>, <span class="ident">epsilon</span> <span class="op">=</span> <span class="ident">f64::EPSILON</span>);</code></pre></div>
|
||
<h2 id="implementing-approximate-equality-for-custom-types" class="section-header"><a href="#implementing-approximate-equality-for-custom-types">Implementing approximate equality for custom types</a></h2>
|
||
<p>The <code>*Eq</code> traits allow approximate equalities to be implemented on types, based on the
|
||
fundamental floating point implementations.</p>
|
||
<p>For example, we might want to be able to do approximate assertions on a complex number type:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">macro_use</span>]</span>
|
||
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">approx</span>;
|
||
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Debug</span>, <span class="ident">PartialEq</span>)]</span>
|
||
<span class="kw">struct</span> <span class="ident">Complex</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> {
|
||
<span class="ident">x</span>: <span class="ident">T</span>,
|
||
<span class="ident">i</span>: <span class="ident">T</span>,
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">Complex</span> { <span class="ident">x</span>: <span class="number">1.2</span>, <span class="ident">i</span>: <span class="number">2.3</span> };
|
||
|
||
<span class="macro">assert_relative_eq!</span>(<span class="ident">x</span>, <span class="ident">x</span>);
|
||
<span class="macro">assert_ulps_eq!</span>(<span class="ident">x</span>, <span class="ident">x</span>, <span class="ident">max_ulps</span> <span class="op">=</span> <span class="number">4</span>);</code></pre></div>
|
||
<p>To do this we can implement <a href="trait.AbsDiffEq.html" title="AbsDiffEq"><code>AbsDiffEq</code></a>, <a href="trait.RelativeEq.html" title="RelativeEq"><code>RelativeEq</code></a> and <a href="trait.UlpsEq.html" title="UlpsEq"><code>UlpsEq</code></a> generically in terms
|
||
of a type parameter that also implements <code>AbsDiffEq</code>, <code>RelativeEq</code> and <code>UlpsEq</code> respectively.
|
||
This means that we can make comparisons for either <code>Complex<f32></code> or <code>Complex<f64></code>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">impl</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">AbsDiffEq</span><span class="op">></span> <span class="ident">AbsDiffEq</span> <span class="kw">for</span> <span class="ident">Complex</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="kw">where</span>
|
||
<span class="ident">T::Epsilon</span>: <span class="ident">Copy</span>,
|
||
{
|
||
<span class="kw">type</span> <span class="ident">Epsilon</span> <span class="op">=</span> <span class="ident">T::Epsilon</span>;
|
||
|
||
<span class="kw">fn</span> <span class="ident">default_epsilon</span>() -> <span class="ident">T::Epsilon</span> {
|
||
<span class="ident">T::default_epsilon</span>()
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">abs_diff_eq</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">other</span>: <span class="kw-2">&</span><span class="self">Self</span>, <span class="ident">epsilon</span>: <span class="ident">T::Epsilon</span>) -> <span class="ident">bool</span> {
|
||
<span class="ident">T::abs_diff_eq</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="ident">x</span>, <span class="kw-2">&</span><span class="ident">other</span>.<span class="ident">x</span>, <span class="ident">epsilon</span>) <span class="op">&&</span>
|
||
<span class="ident">T::abs_diff_eq</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="ident">i</span>, <span class="kw-2">&</span><span class="ident">other</span>.<span class="ident">i</span>, <span class="ident">epsilon</span>)
|
||
}
|
||
}
|
||
|
||
<span class="kw">impl</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">RelativeEq</span><span class="op">></span> <span class="ident">RelativeEq</span> <span class="kw">for</span> <span class="ident">Complex</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="kw">where</span>
|
||
<span class="ident">T::Epsilon</span>: <span class="ident">Copy</span>,
|
||
{
|
||
<span class="kw">fn</span> <span class="ident">default_max_relative</span>() -> <span class="ident">T::Epsilon</span> {
|
||
<span class="ident">T::default_max_relative</span>()
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">relative_eq</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">other</span>: <span class="kw-2">&</span><span class="self">Self</span>, <span class="ident">epsilon</span>: <span class="ident">T::Epsilon</span>, <span class="ident">max_relative</span>: <span class="ident">T::Epsilon</span>) -> <span class="ident">bool</span> {
|
||
<span class="ident">T::relative_eq</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="ident">x</span>, <span class="kw-2">&</span><span class="ident">other</span>.<span class="ident">x</span>, <span class="ident">epsilon</span>, <span class="ident">max_relative</span>) <span class="op">&&</span>
|
||
<span class="ident">T::relative_eq</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="ident">i</span>, <span class="kw-2">&</span><span class="ident">other</span>.<span class="ident">i</span>, <span class="ident">epsilon</span>, <span class="ident">max_relative</span>)
|
||
}
|
||
}
|
||
|
||
<span class="kw">impl</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">UlpsEq</span><span class="op">></span> <span class="ident">UlpsEq</span> <span class="kw">for</span> <span class="ident">Complex</span><span class="op"><</span><span class="ident">T</span><span class="op">></span> <span class="kw">where</span>
|
||
<span class="ident">T::Epsilon</span>: <span class="ident">Copy</span>,
|
||
{
|
||
<span class="kw">fn</span> <span class="ident">default_max_ulps</span>() -> <span class="ident">u32</span> {
|
||
<span class="ident">T::default_max_ulps</span>()
|
||
}
|
||
|
||
<span class="kw">fn</span> <span class="ident">ulps_eq</span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">other</span>: <span class="kw-2">&</span><span class="self">Self</span>, <span class="ident">epsilon</span>: <span class="ident">T::Epsilon</span>, <span class="ident">max_ulps</span>: <span class="ident">u32</span>) -> <span class="ident">bool</span> {
|
||
<span class="ident">T::ulps_eq</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="ident">x</span>, <span class="kw-2">&</span><span class="ident">other</span>.<span class="ident">x</span>, <span class="ident">epsilon</span>, <span class="ident">max_ulps</span>) <span class="op">&&</span>
|
||
<span class="ident">T::ulps_eq</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="ident">i</span>, <span class="kw-2">&</span><span class="ident">other</span>.<span class="ident">i</span>, <span class="ident">epsilon</span>, <span class="ident">max_ulps</span>)
|
||
}
|
||
}</code></pre></div>
|
||
<h2 id="references" class="section-header"><a href="#references">References</a></h2>
|
||
<p>Floating point is hard! Thanks goes to these links for helping to make things a <em>little</em>
|
||
easier to understand:</p>
|
||
<ul>
|
||
<li><a href="https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/">Comparing Floating Point Numbers, 2012 Edition</a></li>
|
||
<li><a href="http://floating-point-gui.de/errors/comparison/">The Floating Point Guide - Comparison</a></li>
|
||
<li><a href="https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a></li>
|
||
</ul>
|
||
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.abs_diff_eq.html" title="approx::abs_diff_eq macro">abs_diff_eq</a></div><div class="item-right docblock-short"><p>Approximate equality of using the absolute difference.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.abs_diff_ne.html" title="approx::abs_diff_ne macro">abs_diff_ne</a></div><div class="item-right docblock-short"><p>Approximate inequality of using the absolute difference.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.assert_abs_diff_eq.html" title="approx::assert_abs_diff_eq macro">assert_abs_diff_eq</a></div><div class="item-right docblock-short"><p>An assertion that delegates to <a href="macro.abs_diff_eq.html" title="abs_diff_eq!"><code>abs_diff_eq!</code></a>, and panics with a helpful error on failure.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.assert_abs_diff_ne.html" title="approx::assert_abs_diff_ne macro">assert_abs_diff_ne</a></div><div class="item-right docblock-short"><p>An assertion that delegates to <a href="macro.abs_diff_ne.html" title="abs_diff_ne!"><code>abs_diff_ne!</code></a>, and panics with a helpful error on failure.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.assert_relative_eq.html" title="approx::assert_relative_eq macro">assert_relative_eq</a></div><div class="item-right docblock-short"><p>An assertion that delegates to <a href="macro.relative_eq.html" title="relative_eq!"><code>relative_eq!</code></a>, and panics with a helpful error on failure.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.assert_relative_ne.html" title="approx::assert_relative_ne macro">assert_relative_ne</a></div><div class="item-right docblock-short"><p>An assertion that delegates to <a href="macro.relative_ne.html" title="relative_ne!"><code>relative_ne!</code></a>, and panics with a helpful error on failure.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.assert_ulps_eq.html" title="approx::assert_ulps_eq macro">assert_ulps_eq</a></div><div class="item-right docblock-short"><p>An assertion that delegates to <a href="macro.ulps_eq.html" title="ulps_eq!"><code>ulps_eq!</code></a>, and panics with a helpful error on failure.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.assert_ulps_ne.html" title="approx::assert_ulps_ne macro">assert_ulps_ne</a></div><div class="item-right docblock-short"><p>An assertion that delegates to <a href="macro.ulps_ne.html" title="ulps_ne!"><code>ulps_ne!</code></a>, and panics with a helpful error on failure.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.relative_eq.html" title="approx::relative_eq macro">relative_eq</a></div><div class="item-right docblock-short"><p>Approximate equality using both the absolute difference and relative based comparisons.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.relative_ne.html" title="approx::relative_ne macro">relative_ne</a></div><div class="item-right docblock-short"><p>Approximate inequality using both the absolute difference and relative based comparisons.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.ulps_eq.html" title="approx::ulps_eq macro">ulps_eq</a></div><div class="item-right docblock-short"><p>Approximate equality using both the absolute difference and ULPs (Units in Last Place).</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.ulps_ne.html" title="approx::ulps_ne macro">ulps_ne</a></div><div class="item-right docblock-short"><p>Approximate inequality using both the absolute difference and ULPs (Units in Last Place).</p>
|
||
</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.AbsDiff.html" title="approx::AbsDiff struct">AbsDiff</a></div><div class="item-right docblock-short"><p>The requisite parameters for testing for approximate equality using a
|
||
absolute difference based comparison.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Relative.html" title="approx::Relative struct">Relative</a></div><div class="item-right docblock-short"><p>The requisite parameters for testing for approximate equality using a
|
||
relative based comparison.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Ulps.html" title="approx::Ulps struct">Ulps</a></div><div class="item-right docblock-short"><p>The requisite parameters for testing for approximate equality using an ULPs
|
||
based comparison.</p>
|
||
</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2>
|
||
<div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.AbsDiffEq.html" title="approx::AbsDiffEq trait">AbsDiffEq</a></div><div class="item-right docblock-short"><p>Equality that is defined using the absolute difference of two numbers.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.RelativeEq.html" title="approx::RelativeEq trait">RelativeEq</a></div><div class="item-right docblock-short"><p>Equality comparisons between two numbers using both the absolute difference and
|
||
relative based comparisons.</p>
|
||
</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.UlpsEq.html" title="approx::UlpsEq trait">UlpsEq</a></div><div class="item-right docblock-short"><p>Equality comparisons between two numbers using both the absolute difference and ULPs
|
||
(Units in Last Place) based comparisons.</p>
|
||
</div></div></div></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="approx" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |