ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_mathUnsigned Module Reference

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"
 

Detailed Description

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.

Warning
The procedures of this module work based on the plausible assumption that integers are represented in two's complement format.
Note
The sum of two integer values with opposite signs will never cause arithmetic overflow.
See also
pm_except
Test:
test_pm_mathUnsigned


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.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

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.

Author:
Fatemeh Bagheri, 12:27 AM Tuesday, February 22, 2022, Dallas, TX Amir Shahmoradi, April 23, 2017, 1:36 AM, Institute for Computational Engineering and Sciences (ICES), University of Texas at Austin

Variable Documentation

◆ MODULE_NAME

character(*, SK), parameter pm_mathUnsigned::MODULE_NAME = "@pm_mathUnsigned"

Definition at line 71 of file pm_mathUnsigned.F90.