ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation. |
This module contains the procedures and interfaces for evaluating the relative or absolute proximity of two numeric values.
More...
Data Types | |
interface | isClose |
Generate and return .true. if the two input values are sufficiently close to each other within the specified tolerances.More... | |
type | mean_type |
This is an empty derived type that is exclusively used to differentiate the procedures within the generic interface isClose(). More... | |
type | reference_type |
This is an empty derived type that is exclusively used to differentiate the procedures within the generic interface isClose(). More... | |
type | strong_type |
This is an empty derived type that is exclusively used to differentiate the procedures within the generic interface isClose(). More... | |
type | weak_type |
This is an empty derived type that is exclusively used to differentiate the procedures within the generic interface isClose(). More... | |
Variables | |
character(*, SK), parameter | MODULE_NAME = "@pm_mathCompare" |
type(reference_type), parameter | REFERENCE = reference_type() |
This is a convenience constant object of type reference_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose(). More... | |
type(strong_type), parameter | STRONG = strong_type() |
This is a convenience constant object of type strong_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose(). More... | |
type(weak_type), parameter | WEAK = weak_type() |
This is a convenience constant object of type weak_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose(). More... | |
type(mean_type), parameter | MEAN = mean_type() |
This is a convenience constant object of type mean_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose(). More... | |
This module contains the procedures and interfaces for evaluating the relative or absolute proximity of two numeric values.
Floating point values contain limited precision.
As such, an accurate equality comparison of real
or complex
values is frequently impossible or insufficient.
Instead, one frequently needs to determine whether a computed value is close enough to an expected value, without requiring them to be exactly equal.
The generic interfaces of this module implement different methods of determining whether two numeric values is sufficiently close to each other,
where,
x
and y
are the two values to be tested for relative closeness,method
is a scalar parameter that determines the method of computing the relative error,reltol
is the relative tolerance for the default or the specified choice of method
,abstol
is a minimum absolute tolerance level, useful for comparisons near zero.NaN
, inf
, and -inf
are handled according to IEEE rules.NaN
is not considered close to any other value, including NaN
.+inf
values are considered close to each other at all times.-inf
values are considered close to each other at all times.real
and complex
values, values integer
type can be also compared with each other by first converting them to real
values.complex
values, the relative and absolute tolerances are specified as real
and the absolute value of the complex
values will be used for scaling and comparison.complex
tolerances are desired, simply pass the absolute values (abs(reltol)
and abs(abstol)
) of the complex
tolerances.Behavior near zero
Relative comparison is problematic if either value is zero.
Mathematically speaking, no value is small relative to zero.
Computationally speaking, if either value is zero, the difference is the absolute value of the other non-zero value, and the computed absolute tolerance will be reltol
times that value.
When reltol < 1
, the difference will never be less than the tolerance.
Nevertheless, there are many instances where there is a need to know if a computed value is close to zero.
This calls for an absolute tolerance test.
Behavior for small values straddling zero
There is a similar issue if the two values to be compared straddle zero.
If x
is approximately equal to -y
, then x
and y
will never be computed as close.
To handle such cases, the optional parameter, abstol
can be used to set a minimum tolerance used in the case of very small or zero computed relative tolerance.
That is, the values will be always be considered close if the difference between them is less than abstol
.
If the input reltol
argument is set 0.
, then only the absolute tolerance will effect the result.
Although trivial, this allows the generic interfaces to be also used for purely absolute tolerance checks.
Relative vs. Absolute Difference
There are two ways to define the proximity of two numeric values:
abs(x - y)
,abs(x - y) / sigma
, where sigma
is called the scale factor.x
and y
, are close to each other.abs(x - y) <= reltol * abs(x)
.abs(x - y) <= reltol * max(abs(x), abs(y))
.abs(x - y) <= reltol * min(abs(x), abs(y))
.abs(x - y) <= reltol * (x + y) / 2
.(abs(x - y) <= abs(reltol * x)) .or. (abs(x - y) <= abs(reltol * y))
.(abs(x - y) <= abs(reltol * x)) .and. (abs(x - y) <= abs(reltol * y))
.reltol
is small compared to the two input values, the differences between the methods are negligible, frequently less than available floating point precision.delta
is the difference between the allowable absolute tolerance defined by the larger value and that defined by the smaller value.delta
is the amount that the two input values need to be different in order to get a different result from the two weak and strong scaling methods.x
is the larger value and that both x
and y
are positive. Therefore,(x - y)
, equals the tolerance times the larger value.x = 10; y = 9; reltol = 0.1
, then,reltol * x == 0.1 * 10 == 1.0
.reltol * y == 0.1 * 9.0 == 0.9
.delta = (1.0 - 0.9) = 0.1
or reltol**2 * x = 0.1**2 * 10 = .1
.1e-8
is about half the precision available in a real64
.1e-8**2 * x or 1e-16 * x
, which is close to the limit of precision of real64
.reltol = 1e-9
(or smaller) would eliminate the difference between the two tests down to the limits of a real64
precision.x
and y
values.isClose(x, y, reltol, abstol)
always yields the same result as isClose(y, x)
.isClose(x, y, reltol, abstol)
is not necessarily the same as isClose(y,x)
.y
within x
percentage of this known value x
? or,(x, b)
.y
is within a particular range of a known value x
, then the following test can be implemented directly,x
within 200%
(reltol = 2.0
) of y
?.true.
if one value is zero.0
within 200%
of 10
?200%
of 10
is 20
. Therefore, the range within 200%
of 10
is -10
to +30
..true.
.The logic behind the specific choices of absolute and relative tolerances.
epsilon(x)
.reltol = sqrt(epsilon(x))
is typically the largest relative tolerance for which the various possible testing methods above will yield the similar results.tiny(x)
seems most appropriate.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
This is a convenience constant object of type mean_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose().
Passing this constant object to the procedures of the generic interface isClose() is equivalent to requesting the mean Scaling method of testing the proximity of two numbers.
See the documentation of pm_mathCompare for extensive details of this and other possible testing modes.
See the documentation of isClose() for example usage.
Possible calling interfaces ⛓
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
Definition at line 447 of file pm_mathCompare.F90.
character(*, SK), parameter pm_mathCompare::MODULE_NAME = "@pm_mathCompare" |
Definition at line 210 of file pm_mathCompare.F90.
type(reference_type), parameter pm_mathCompare::REFERENCE = reference_type() |
This is a convenience constant object of type reference_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose().
Passing this constant object to the procedures of the generic interface isClose() is equivalent to requesting the Reference Scaling method of testing the proximity of two numbers.
See the documentation of pm_mathCompare for extensive details of this and other possible testing modes.
See the documentation of isClose() for example usage.
Possible calling interfaces ⛓
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
Definition at line 267 of file pm_mathCompare.F90.
type(strong_type), parameter pm_mathCompare::STRONG = strong_type() |
This is a convenience constant object of type strong_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose().
Passing this constant object to the procedures of the generic interface isClose() is equivalent to requesting the Strong Scaling method of testing the proximity of two numbers.
See the documentation of pm_mathCompare for extensive details of this and other possible testing modes.
See the documentation of isClose() for example usage.
Possible calling interfaces ⛓
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
Definition at line 327 of file pm_mathCompare.F90.
This is a convenience constant object of type weak_type() that is exclusively provided to distinguish and facilitate the use of the procedures under the generic interface isClose().
Passing this constant object to the procedures of the generic interface isClose() is equivalent to requesting the Weak Scaling method of testing the proximity of two numbers.
See the documentation of pm_mathCompare for extensive details of this and other possible testing modes.
See the documentation of isClose() for example usage.
Possible calling interfaces ⛓
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
Definition at line 387 of file pm_mathCompare.F90.