ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation. |
This module contains procedures and generic interfaces and generic interfaces for various operations with positive integers with results that have the same binary representation as an unsigned integer.
Such operations (like addition or subtraction) would normally cause runtime overflow errors within the default Fortran environment.
More...
Data Types | |
interface | operator(.uadd.) |
Generate and return a (possibly overflowed) signed integer that is the result of adding the two input (non-negative) integers without runtime overflow error. More... | |
Variables | |
character(*, SK), parameter | MODULE_NAME = "@pm_mathUnsigned" |
This module contains procedures and generic interfaces and generic interfaces for various operations with positive integers with results that have the same binary representation as an unsigned integer.
Such operations (like addition or subtraction) would normally cause runtime overflow errors within the default Fortran environment.
Consider the simple case of a 4-bits unsigned integer kind.
The binary representations of all possible values by such an integer are shown the left plot of the figure below.
Now, consider the case of a 4-bits signed integer kind (as illustrated in the right plot of the figure below).
Because all Fortran integer kinds are by default signed, the addition of any two (signed) integer values are guaranteed to not cause overflow if added as unsigned integers.
The trick to compute the binary representation of such overflowed signed additions is therefore to compute the negative number in the signed integer space that corresponds to the unsigned addition of the two positive numbers.
For example, consider the operation 4 + 6 = 10
where all numbers are 4-bit unsigned integers.
This addition would be invalid and cause runtime overflow if 4
and 6
are 4-bits signed integers as illustrated in the figure above.
However, the binary representation of the result (10
) of the unsigned addition operation can be readily computed by first rotating 4
and 6
by 180 degrees on the circular representation of the numbers in the figure to get -4
and -2
.
Then, adding the rotated numbers yields the correct binary representation for the result of 4 + 6 = 10
as an unsigned integer addition.
This rotation operation is equivalent to negating the binary signs of the two addition operands before adding them.
Note that an addition operation can never overflow if the two operands are of opposite signs.
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.
character(*, SK), parameter pm_mathUnsigned::MODULE_NAME = "@pm_mathUnsigned" |
Definition at line 71 of file pm_mathUnsigned.F90.