I think that the following code reveals a bug with the 32 bit compiler. The variable a2 must always be positive.
module random_numbers
implicit none
integer, parameter :: dp = kind(1.d0)
real(dp), parameter :: pi = 4.d0*atan(1.d0)
contains
real(dp) function random_std_uniform()
! Function to generate a random number from the standard uniform distribution.
do
call random_number(random_std_uniform)
if (random_std_uniform .gt. tiny(1.0)) exit
end do
end function random_std_uniform
real(dp) function random_std_normal()
! Function to generate a random number from the standard normal distributon.
random_std_normal = sqrt(-2.d0*log(random_std_uniform())) * cos(2.d0*pi*random_std_uniform())
end function random_std_normal
end module random_numbers
program bug
use random_numbers
use iso_fortran_env
implicit none
integer i
real(dp) a, a2
write(*,*) 'Vern : ',compiler_version ()
write(*,*) 'Options : ',compiler_options ()
write(*,*)
! This do loop runs as expected. a can be positive or negative, a**2 is always positive
write(*,*) 'OK with intermediate value'
do i = 1, 10
a = random_std_normal()
a2 = (a**2)
write(*,*) i, a, a2
end do
write(*,*)
! This do loop which does not have an intermediate set does not run as expected with the
! 32 bit compiler, it will on occasion return a negative number.
write(*,*) 'Bug appears without the intermediate value'
do i = 1, 10
a2 = (random_std_normal()**2)
if (a2 .gt. 0.d0) then
write(*,*) i, a2, '# OK #'
else
write(*,*) i, a2, '# BUG #'
end if
end do
end program bug
Output with 32 bit compiler:
Vern : FTN95 v9.03.0
Options : CHECKMATE;ERROR_NUMBERS;LINK;NO_BANNER;UNLIMITED_ERRORS;VS7;
OK with intermediate value
1 0.534344548168 0.285524096156
2 -0.124214116238 1.542914667290E-02
3 0.986227639562 0.972644957036
4 -1.01574729998 1.03174257742
5 0.876187316155 0.767704212991
6 0.689662288046 0.475634071553
7 0.236283819468 5.583004334239E-02
8 -0.172203452071 2.965402890512E-02
9 1.94609445045 3.78728361007
10 -7.806535318409E-02 6.094199367757E-03
Bug appears without the intermediate value
1 0.942003631129 # OK #
2 -0.647829889894 # BUG #
3 0.247587151446 # OK #
4 0.590547558638 # OK #
5 0.337313028513 # OK #
6 0.612344923211 # OK #
7 0.233890118419 # OK #
8 -0.526714233628 # BUG #
9 -9.626477024171E-02 # BUG #
10 0.296546433141 # OK #