Re: same function giving different results

From: Rich Townsend (rhdt_at_barREMOVEtol.udel.edu)
Date: 01/06/05


Date: Thu, 06 Jan 2005 09:44:54 -0500

Bart Vandewoestyne wrote:
> In my main program, i call the following function two or more times:
>
> function radical_inverse(n, b) result (radinv)
>
> integer, intent(in) :: n, b
> double precision :: radinv
>
> integer :: i
> double precision :: b_factor
> integer, allocatable :: mydigits(:)
>
> radinv = 0.0
>
> allocate(mydigits(get_nb_digits(n, b)))
> call get_digits(n, b, mydigits)
>
> b_factor = real(1.0/b)
> do i=1,get_nb_digits(n, b)
> radinv = radinv + mydigits(i)*b_factor
> b_factor = b_factor/b
> end do
>
> deallocate(mydigits)
>
> end function radical_inverse
>
> The function comes from a submodule.
>
> When i run my testprogram which contains the following lines:
>
> print *, "radical_inverse(154754, 10) = ", radical_inverse(154754, 10)
> print *, "radical_inverse(154754, 10) = ", radical_inverse(154754, 10)
>
> I get two different results:
>
> radical_inverse(154754, 10) = 0.4574510000000000
> radical_inverse(154754, 10) = 5.7451000000000002E-02
>
> the first result is ok, the rest of the results is wrong.
>
> I have been looking over and over this again, and my guess would be that the problem is somewhere with the allocatable array I'm using inside my function, but
> i cannot find the real problem and how to fix it.

Bart, have you tried compiling your program with array-bounds checking
enabled? Look in your compiler manual to see how to do this. The
behaviour of your program indicates that you might be corrupting memory
somewhere, and bounds checking usually detects cases where the
corruption arises from an out-of-bounds array write.

cheers,

Rich