101 lines
16 KiB
HTML
101 lines
16 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."><title>approx - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="approx" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../approx/index.html">approx</a><span class="version">0.5.1</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#implementing-approximate-equality-for-custom-types" title="Implementing approximate equality for custom types">Implementing approximate equality for custom types</a></li><li><a href="#references" title="References">References</a></li></ul><h3><a href="#macros">Crate Items</a></h3><ul class="block"><li><a href="#macros" title="Macros">Macros</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>approx</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/approx/lib.rs.html#15-388">Source</a> </span></div><details class="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="attr">#[macro_use]
|
|
</span><span class="kw">extern crate </span>approx;
|
|
|
|
<span class="kw">use </span>std::f64;
|
|
|
|
<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>, epsilon = f64::EPSILON);
|
|
|
|
<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>, epsilon = f64::EPSILON);
|
|
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, max_relative = <span class="number">1.0</span>);
|
|
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, epsilon = f64::EPSILON, max_relative = <span class="number">1.0</span>);
|
|
<span class="macro">relative_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, max_relative = <span class="number">1.0</span>, epsilon = f64::EPSILON);
|
|
|
|
<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>, epsilon = f64::EPSILON);
|
|
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, max_ulps = <span class="number">4</span>);
|
|
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, epsilon = f64::EPSILON, max_ulps = <span class="number">4</span>);
|
|
<span class="macro">ulps_eq!</span>(<span class="number">1.0</span>, <span class="number">1.0</span>, max_ulps = <span class="number">4</span>, epsilon = f64::EPSILON);</code></pre></div>
|
|
<h2 id="implementing-approximate-equality-for-custom-types"><a class="doc-anchor" href="#implementing-approximate-equality-for-custom-types">§</a>Implementing approximate equality for custom types</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="attr">#[macro_use]
|
|
</span><span class="kw">extern crate </span>approx;
|
|
|
|
<span class="attr">#[derive(Debug, PartialEq)]
|
|
</span><span class="kw">struct </span>Complex<T> {
|
|
x: T,
|
|
i: T,
|
|
}
|
|
|
|
<span class="kw">let </span>x = Complex { x: <span class="number">1.2</span>, i: <span class="number">2.3 </span>};
|
|
|
|
<span class="macro">assert_relative_eq!</span>(x, x);
|
|
<span class="macro">assert_ulps_eq!</span>(x, x, max_ulps = <span class="number">4</span>);</code></pre></div>
|
|
<p>To do this we can implement <a href="trait.AbsDiffEq.html" title="trait approx::AbsDiffEq"><code>AbsDiffEq</code></a>, <a href="trait.RelativeEq.html" title="trait approx::RelativeEq"><code>RelativeEq</code></a> and <a href="trait.UlpsEq.html" title="trait approx::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><T: AbsDiffEq> AbsDiffEq <span class="kw">for </span>Complex<T> <span class="kw">where
|
|
</span>T::Epsilon: Copy,
|
|
{
|
|
<span class="kw">type </span>Epsilon = T::Epsilon;
|
|
|
|
<span class="kw">fn </span>default_epsilon() -> T::Epsilon {
|
|
T::default_epsilon()
|
|
}
|
|
|
|
<span class="kw">fn </span>abs_diff_eq(<span class="kw-2">&</span><span class="self">self</span>, other: <span class="kw-2">&</span><span class="self">Self</span>, epsilon: T::Epsilon) -> bool {
|
|
T::abs_diff_eq(<span class="kw-2">&</span><span class="self">self</span>.x, <span class="kw-2">&</span>other.x, epsilon) &&
|
|
T::abs_diff_eq(<span class="kw-2">&</span><span class="self">self</span>.i, <span class="kw-2">&</span>other.i, epsilon)
|
|
}
|
|
}
|
|
|
|
<span class="kw">impl</span><T: RelativeEq> RelativeEq <span class="kw">for </span>Complex<T> <span class="kw">where
|
|
</span>T::Epsilon: Copy,
|
|
{
|
|
<span class="kw">fn </span>default_max_relative() -> T::Epsilon {
|
|
T::default_max_relative()
|
|
}
|
|
|
|
<span class="kw">fn </span>relative_eq(<span class="kw-2">&</span><span class="self">self</span>, other: <span class="kw-2">&</span><span class="self">Self</span>, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
|
|
T::relative_eq(<span class="kw-2">&</span><span class="self">self</span>.x, <span class="kw-2">&</span>other.x, epsilon, max_relative) &&
|
|
T::relative_eq(<span class="kw-2">&</span><span class="self">self</span>.i, <span class="kw-2">&</span>other.i, epsilon, max_relative)
|
|
}
|
|
}
|
|
|
|
<span class="kw">impl</span><T: UlpsEq> UlpsEq <span class="kw">for </span>Complex<T> <span class="kw">where
|
|
</span>T::Epsilon: Copy,
|
|
{
|
|
<span class="kw">fn </span>default_max_ulps() -> u32 {
|
|
T::default_max_ulps()
|
|
}
|
|
|
|
<span class="kw">fn </span>ulps_eq(<span class="kw-2">&</span><span class="self">self</span>, other: <span class="kw-2">&</span><span class="self">Self</span>, epsilon: T::Epsilon, max_ulps: u32) -> bool {
|
|
T::ulps_eq(<span class="kw-2">&</span><span class="self">self</span>.x, <span class="kw-2">&</span>other.x, epsilon, max_ulps) &&
|
|
T::ulps_eq(<span class="kw-2">&</span><span class="self">self</span>.i, <span class="kw-2">&</span>other.i, epsilon, max_ulps)
|
|
}
|
|
}</code></pre></div>
|
|
<h2 id="references"><a class="doc-anchor" href="#references">§</a>References</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="section-header">Macros<a href="#macros" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="macro" href="macro.abs_diff_eq.html" title="macro approx::abs_diff_eq">abs_<wbr>diff_<wbr>eq</a></div><div class="desc docblock-short">Approximate equality of using the absolute difference.</div></li><li><div class="item-name"><a class="macro" href="macro.abs_diff_ne.html" title="macro approx::abs_diff_ne">abs_<wbr>diff_<wbr>ne</a></div><div class="desc docblock-short">Approximate inequality of using the absolute difference.</div></li><li><div class="item-name"><a class="macro" href="macro.assert_abs_diff_eq.html" title="macro approx::assert_abs_diff_eq">assert_<wbr>abs_<wbr>diff_<wbr>eq</a></div><div class="desc docblock-short">An assertion that delegates to <a href="macro.abs_diff_eq.html" title="macro approx::abs_diff_eq"><code>abs_diff_eq!</code></a>, and panics with a helpful error on failure.</div></li><li><div class="item-name"><a class="macro" href="macro.assert_abs_diff_ne.html" title="macro approx::assert_abs_diff_ne">assert_<wbr>abs_<wbr>diff_<wbr>ne</a></div><div class="desc docblock-short">An assertion that delegates to <a href="macro.abs_diff_ne.html" title="macro approx::abs_diff_ne"><code>abs_diff_ne!</code></a>, and panics with a helpful error on failure.</div></li><li><div class="item-name"><a class="macro" href="macro.assert_relative_eq.html" title="macro approx::assert_relative_eq">assert_<wbr>relative_<wbr>eq</a></div><div class="desc docblock-short">An assertion that delegates to <a href="macro.relative_eq.html" title="macro approx::relative_eq"><code>relative_eq!</code></a>, and panics with a helpful error on failure.</div></li><li><div class="item-name"><a class="macro" href="macro.assert_relative_ne.html" title="macro approx::assert_relative_ne">assert_<wbr>relative_<wbr>ne</a></div><div class="desc docblock-short">An assertion that delegates to <a href="macro.relative_ne.html" title="macro approx::relative_ne"><code>relative_ne!</code></a>, and panics with a helpful error on failure.</div></li><li><div class="item-name"><a class="macro" href="macro.assert_ulps_eq.html" title="macro approx::assert_ulps_eq">assert_<wbr>ulps_<wbr>eq</a></div><div class="desc docblock-short">An assertion that delegates to <a href="macro.ulps_eq.html" title="macro approx::ulps_eq"><code>ulps_eq!</code></a>, and panics with a helpful error on failure.</div></li><li><div class="item-name"><a class="macro" href="macro.assert_ulps_ne.html" title="macro approx::assert_ulps_ne">assert_<wbr>ulps_<wbr>ne</a></div><div class="desc docblock-short">An assertion that delegates to <a href="macro.ulps_ne.html" title="macro approx::ulps_ne"><code>ulps_ne!</code></a>, and panics with a helpful error on failure.</div></li><li><div class="item-name"><a class="macro" href="macro.relative_eq.html" title="macro approx::relative_eq">relative_<wbr>eq</a></div><div class="desc docblock-short">Approximate equality using both the absolute difference and relative based comparisons.</div></li><li><div class="item-name"><a class="macro" href="macro.relative_ne.html" title="macro approx::relative_ne">relative_<wbr>ne</a></div><div class="desc docblock-short">Approximate inequality using both the absolute difference and relative based comparisons.</div></li><li><div class="item-name"><a class="macro" href="macro.ulps_eq.html" title="macro approx::ulps_eq">ulps_eq</a></div><div class="desc docblock-short">Approximate equality using both the absolute difference and ULPs (Units in Last Place).</div></li><li><div class="item-name"><a class="macro" href="macro.ulps_ne.html" title="macro approx::ulps_ne">ulps_ne</a></div><div class="desc docblock-short">Approximate inequality using both the absolute difference and ULPs (Units in Last Place).</div></li></ul><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.AbsDiff.html" title="struct approx::AbsDiff">AbsDiff</a></div><div class="desc docblock-short">The requisite parameters for testing for approximate equality using a
|
|
absolute difference based comparison.</div></li><li><div class="item-name"><a class="struct" href="struct.Relative.html" title="struct approx::Relative">Relative</a></div><div class="desc docblock-short">The requisite parameters for testing for approximate equality using a
|
|
relative based comparison.</div></li><li><div class="item-name"><a class="struct" href="struct.Ulps.html" title="struct approx::Ulps">Ulps</a></div><div class="desc docblock-short">The requisite parameters for testing for approximate equality using an ULPs
|
|
based comparison.</div></li></ul><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.AbsDiffEq.html" title="trait approx::AbsDiffEq">AbsDiff<wbr>Eq</a></div><div class="desc docblock-short">Equality that is defined using the absolute difference of two numbers.</div></li><li><div class="item-name"><a class="trait" href="trait.RelativeEq.html" title="trait approx::RelativeEq">Relative<wbr>Eq</a></div><div class="desc docblock-short">Equality comparisons between two numbers using both the absolute difference and
|
|
relative based comparisons.</div></li><li><div class="item-name"><a class="trait" href="trait.UlpsEq.html" title="trait approx::UlpsEq">UlpsEq</a></div><div class="desc docblock-short">Equality comparisons between two numbers using both the absolute difference and ULPs
|
|
(Units in Last Place) based comparisons.</div></li></ul></section></div></main></body></html> |