<!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="../static.files/SourceSerif4-Regular-1f7d512b176f0f72.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Bold-124a1ca42af929b6.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-6827029ac823cab7.css" id="mainThemeStyle"><link rel="stylesheet" id="themeStyle" href="../static.files/light-ebce58d0a40c3431.css"><link rel="stylesheet" disabled href="../static.files/dark-f23faae4a2daf9a6.css"><link rel="stylesheet" disabled href="../static.files/ayu-8af5e100b21cd173.css"><script id="default-settings" ></script><script src="../static.files/storage-d43fa987303ecbbb.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-c55e1eb52e1886b4.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-13285aec31fa243e.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../approx/index.html"><div class="logo-container"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../approx/index.html"><div class="logo-container"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate approx</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.5.1</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-5ec35bf9ca753509.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">approx</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/approx/lib.rs.html#15-388">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><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="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 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="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="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><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 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">Approximate equality of using the absolute difference.</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">Approximate inequality of using the absolute difference.</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">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.</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">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.</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">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.</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">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.</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">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.</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">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.</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">Approximate equality using both the absolute difference and relative based comparisons.</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">Approximate inequality using both the absolute difference and relative based comparisons.</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">Approximate equality using both the absolute difference and ULPs (Units in Last Place).</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">Approximate inequality using both the absolute difference and ULPs (Units in Last Place).</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">The requisite parameters for testing for approximate equality using a
absolute difference based comparison.</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">The requisite parameters for testing for approximate equality using a
relative based comparison.</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">The requisite parameters for testing for approximate equality using an ULPs
based comparison.</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">Equality that is defined using the absolute difference of two numbers.</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">Equality comparisons between two numbers using both the absolute difference and
relative based comparisons.</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">Equality comparisons between two numbers using both the absolute difference and ULPs
(Units in Last Place) based comparisons.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="approx" data-themes="" data-resource-suffix="" data-rustdoc-version="1.67.1 (d5a82bbd2 2023-02-07)" data-search-js="search-444266647c4dba98.js" data-settings-js="settings-bebeae96e00e4617.js" data-settings-css="settings-af96d9e2fc13e081.css" ></div></body></html>