Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:ScribuntoUnit/doc
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== assertWithinDelta === <syntaxhighlight lang="lua"> self:assertWithinDelta(expected, actual, delta, message) </syntaxhighlight> For two numbers, this tests whether the first is within a given distance (delta) from the second. This is useful to compare [[floating point]] numbers, which are used to represent numbers in the standard installation of Lua. (To be precise, it uses [[double-precision floating point]] numbers.) For example, on the version of Scribunto installed on the English Wikipedia, the expression <code>0.3 β 0.2 == 0.1</code> evaluates to <code>false</code>. This is because in practice, the expression <code>0.3 β 0.2</code> equals <code>0.09999999999999997780β¦</code> and the number <code>0.1</code> equals <code>0.10000000000000000555β¦</code>. The slight error between the two means that Lua does not consider them equal. Therefore, to test for equality between two floating point numbers, we should accept values within a small distance (delta) of each other, not just equal values. Note that this problem does not affect integers, which can be represented exactly using double-precision floating point numbers up to values of 2^53. <syntaxhighlight lang="lua"> self:assertWithinDelta(0.1, calculator.subtract(0.3, 0.2), 1e-10) </syntaxhighlight>
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)