<!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="Assert a number is within epsilon of another number."><title>assert_in_epsilon in lib - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.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-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-c5d6553a23f1e5a6.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="lib" data-themes="" data-resource-suffix="" data-rustdoc-version="1.81.0 (eeb90cda1 2024-09-04)" data-channel="1.81.0" data-search-js="search-d234aafac6c221dd.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-118b08c4c78b968e.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-d2fab2bf619172d3.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-df360f571f6edeae.css"></noscript><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 macro"><!--[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="../lib/index.html">lib</a></h2></div><div class="sidebar-elems"></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>Macro <a href="index.html">lib</a>::<wbr><a class="macro" href="#">assert_in_epsilon</a><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><span class="out-of-band"><a class="src" href="../src/lib/assert_in_epsilon.rs.html#251-264">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><pre class="rust item-decl"><span class="macro">macro_rules!</span> assert_in_epsilon {
(<span class="macro-nonterminal">$a</span>:expr, <span class="macro-nonterminal">$b</span>:expr, <span class="macro-nonterminal">$epsilon</span>:expr $(,)<span class="question-mark">?</span>) => { ... };
(<span class="macro-nonterminal">$a</span>:expr, <span class="macro-nonterminal">$b</span>:expr, <span class="macro-nonterminal">$epsilon</span>:expr, $(<span class="macro-nonterminal">$message</span>:tt)+) => { ... };
}</pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Assert a number is within epsilon of another number.</p>
<p>Pseudocode:<br>
| a - b | ≤ ε * min(a, b)</p>
<ul>
<li>
<p>If true, return <code>()</code>.</p>
</li>
<li>
<p>Otherwise, call <a href="https://doc.rust-lang.org/1.81.0/std/macro.panic.html" title="macro std::panic"><code>panic!</code></a> with a message and the values of the
expressions with their debug representations.</p>
</li>
</ul>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>assertables::<span class="kw-2">*</span>;
<span class="kw">let </span>a: i8 = <span class="number">10</span>;
<span class="kw">let </span>b: i8 = <span class="number">20</span>;
<span class="kw">let </span>epsilon: i8 = <span class="number">1</span>;
<span class="macro">assert_in_epsilon!</span>(a, b, epsilon);
<span class="kw">let </span>a: i8 = <span class="number">10</span>;
<span class="kw">let </span>b: i8 = <span class="number">30</span>;
<span class="kw">let </span>e: i8 = <span class="number">1</span>;
<span class="macro">assert_in_epsilon!</span>(a, b, e);
<span class="comment">// assertion failed: `assert_in_epsilon!(a, b, epsilon)`
// https://docs.rs/assertables/8.14.0/assertables/macro.assert_in_epsilon.html
// a label: `a`,
// a debug: `10`,
// b label: `b`,
// b debug: `30`,
// epsilon label: `e`,
// epsilon debug: `1`,
// | a - b |: `20`,
// epsilon * min(a, b): `10`,\n",
// | a - b | ≤ epsilon * min(a, b): false"</span></code></pre></div>
<p>The macros <code>assert_in_delta</code> and <code>assert_in_epsilon</code> can test
approximations:</p>
<ul>
<li>
<p>For an approximation, the absolute error (i.e. delta) is the magnitude of
the difference between the exact value and the approximation. For this,
use the macro</p>
</li>
<li>
<p>For an approximation, the relative error (i.e. epsilon) is the absolute
error divided by the magnitude of the exact value. This can be used to
compare approximations of numbers of wildly differing size.</p>
</li>
<li>
<p>For example, approximating the number 1,000 with an absolute error of 3
is, in most applications, much worse than approximating the number
1,000,000 with an absolute error of 3; in the first case the relative
error is 0.003 and in the second it is only 0.000003.</p>
</li>
<li>
<p>Thanks to Ruby minitest for the example and documentation.</p>
</li>
</ul>
<h2 id="module-macros"><a class="doc-anchor" href="#module-macros">§</a>Module macros</h2>
<ul>
<li><a href="macro.assert_in_epsilon.html" title="macro lib::assert_in_epsilon"><code>assert_in_epsilon</code></a></li>
<li><a href="macro.assert_in_epsilon_as_result.html" title="macro lib::assert_in_epsilon_as_result"><code>assert_in_epsilon_as_result</code></a></li>
<li><a href="macro.debug_assert_in_epsilon.html" title="macro lib::debug_assert_in_epsilon"><code>debug_assert_in_epsilon</code></a></li>
</ul>
</div></details></section></div></main></body></html>