ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_mathRound::pnint Interface Reference

Generate and return the probabilistically-rounded to the nearest integer value of the input real number. More...

Detailed Description

Generate and return the probabilistically-rounded to the nearest integer value of the input real number.

Unlike the intrinsic Fortran nint() function, the procedures of this generic interface round the input real number to the nearest integer value probabilistically.
The closer the input val is to its nint(val) result, the more likely it will be rounded to it.
See below for example usage.

Parameters
[in]val: The input scalar or array of the same rank as other array-like arguments, of
  1. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128)
containing the number to be rounded probabilistically.
Returns
whole : The output scalar or array of the same rank and shape as the input arguments, of default integer kind IK, containing the probabilistically-rounded input value to the nearest integer.


Possible calling interfaces

use pm_mathRound, only: pnint
whole = pnint(val)
whole = pnint(rng, val)
Generate and return the probabilistically-rounded to the nearest integer value of the input real numb...
This module contains procedures and generic interfaces for rounding real values to whole integers.
Remarks
The procedures under discussion are impure.
The procedures under discussion are elemental.
See also
getLog1p
get1mexp


Example usage

1program example
2
3 use pm_kind, only: RKG => RKS ! any real kind is supported.
4 use pm_kind, only: IKG => IKS ! any integer kind is supported.
5 use pm_kind, only: SK, IK, LK, RKD
6 use pm_distUnif, only: getUnifRand
7 use pm_io, only: display_type
8 use pm_mathRound, only: pnint
9 use pm_err, only: setAsserted
10
11 implicit none
12
13 integer(IK) :: i, ival, irep
14 real(RKG), allocatable :: val(:)
15 integer(IKG), allocatable :: whole(:)
16
17 type(display_type) :: disp
18 disp = display_type(file = "main.out.F90")
19
20 do i = 0, 1
21 call disp%skip()
22 call disp%show("i")
23 call disp%show( i )
24 call disp%show("val = (-1)**i * [5., 5.2, 5.5, 5.8, 6.]")
25 val = (-1)**i * [5., 5.2, 5.5, 5.8, 6.]
26 do ival = 1, size(val)
27 call disp%skip()
28 call disp%show("ival")
29 call disp%show( ival )
30 call disp%show("val(ival)")
31 call disp%show( val(ival) )
32 call disp%show("whole = pnint([(val(ival), irep = 1, 20)])")
33 whole = pnint([(val(ival), irep = 1, 20)])
34 call disp%show("whole")
35 call disp%show( whole )
36 call disp%show("call setAsserted(all(abs(val(ival) - whole) < 1))")
37 call setAsserted(all(abs(val(ival) - whole) < 1))
38 call disp%skip()
39 end do
40 end do
41
42end program example
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
Verify the input assertion holds and if it does not, print the (optional) input message on stdout and...
Definition: pm_err.F90:735
Generate and return an object of type stop_type with the user-specified input attributes.
Definition: pm_err.F90:1618
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for reporting and handling errors.
Definition: pm_err.F90:52
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IKS
The single-precision integer kind in Fortran mode. On most platforms, this is a 32-bit integer kind.
Definition: pm_kind.F90:563
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter RKD
The double precision real kind in Fortran mode. On most platforms, this is an 64-bit real kind.
Definition: pm_kind.F90:568
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2i
3+0
4val = (-1)**i * [5., 5.2, 5.5, 5.8, 6.]
5
6ival
7+1
8val(ival)
9+5.00000000
10whole = pnint([(val(ival), irep = 1, 20)])
11whole
12+5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5, +5
13call setAsserted(all(abs(val(ival) - whole) < 1))
14
15
16ival
17+2
18val(ival)
19+5.19999981
20whole = pnint([(val(ival), irep = 1, 20)])
21whole
22+5, +5, +5, +6, +6, +5, +5, +5, +5, +5, +5, +5, +6, +5, +5, +5, +5, +5, +5, +5
23call setAsserted(all(abs(val(ival) - whole) < 1))
24
25
26ival
27+3
28val(ival)
29+5.50000000
30whole = pnint([(val(ival), irep = 1, 20)])
31whole
32+5, +5, +5, +6, +5, +5, +6, +5, +5, +5, +6, +5, +5, +5, +6, +6, +5, +5, +5, +6
33call setAsserted(all(abs(val(ival) - whole) < 1))
34
35
36ival
37+4
38val(ival)
39+5.80000019
40whole = pnint([(val(ival), irep = 1, 20)])
41whole
42+6, +5, +5, +5, +6, +5, +6, +6, +6, +6, +6, +6, +6, +5, +6, +5, +6, +6, +6, +6
43call setAsserted(all(abs(val(ival) - whole) < 1))
44
45
46ival
47+5
48val(ival)
49+6.00000000
50whole = pnint([(val(ival), irep = 1, 20)])
51whole
52+6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6, +6
53call setAsserted(all(abs(val(ival) - whole) < 1))
54
55
56i
57+1
58val = (-1)**i * [5., 5.2, 5.5, 5.8, 6.]
59
60ival
61+1
62val(ival)
63-5.00000000
64whole = pnint([(val(ival), irep = 1, 20)])
65whole
66-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5
67call setAsserted(all(abs(val(ival) - whole) < 1))
68
69
70ival
71+2
72val(ival)
73-5.19999981
74whole = pnint([(val(ival), irep = 1, 20)])
75whole
76-5, -6, -5, -5, -5, -6, -5, -5, -5, -6, -6, -5, -6, -5, -5, -6, -5, -5, -5, -5
77call setAsserted(all(abs(val(ival) - whole) < 1))
78
79
80ival
81+3
82val(ival)
83-5.50000000
84whole = pnint([(val(ival), irep = 1, 20)])
85whole
86-6, -6, -6, -6, -5, -5, -6, -5, -5, -5, -5, -5, -6, -5, -6, -5, -5, -6, -5, -5
87call setAsserted(all(abs(val(ival) - whole) < 1))
88
89
90ival
91+4
92val(ival)
93-5.80000019
94whole = pnint([(val(ival), irep = 1, 20)])
95whole
96-6, -6, -6, -6, -6, -6, -6, -6, -6, -5, -6, -6, -6, -6, -6, -6, -5, -6, -6, -6
97call setAsserted(all(abs(val(ival) - whole) < 1))
98
99
100ival
101+5
102val(ival)
103-6.00000000
104whole = pnint([(val(ival), irep = 1, 20)])
105whole
106-6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6
107call setAsserted(all(abs(val(ival) - whole) < 1))
108
109
Test:
test_pm_mathIntSqrt
Todo:
Low Priority: This generic interface can be extended to output integer values of non-default kind.
Todo:
Low Priority: This generic interface can be extended to accept custom random number generators.


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:
Amir Shahmoradi, April 23, 2017, 1:36 AM, Institute for Computational Engineering and Sciences (ICES), University of Texas at Austin

Definition at line 99 of file pm_mathRound.F90.


The documentation for this interface was generated from the following file: