bug with double precision function

2 views
Skip to first unread message

beli...@aol.com

unread,
Jan 31, 2007, 6:45:04 PM1/31/07
to gg95
For the code

double precision function two()
two = 2.0d0
end function two

program xx
print*,two(),kind(two())
end program xx

mingw g95 Jan 23 2007 says

In file bug_print.f90:1

double precision function two()
1
In file bug_print.f90:6

print*,two(),kind(two())
2
Error: FUNCTION 'two' is of type REAL(8) at (1) and type REAL(4) at
(2)

but gfortran and Intel Fortran accept it and give output

2.000000 4

, and I think they are correct. If one replaces "double precision"
with "real" the code works.

Fly Away

unread,
Jan 31, 2007, 8:50:25 PM1/31/07
to gg95
In the absence of "implicit none" a varible that has a name starting
with "t" is considered REAL. And since you didn't declare the "two"
function in your main program in any way, it sees it as REAL while it
is in fact DOUBLE PRESICION. So it's a matter of choice between a
warning and an error, really.

My gfortran and ifort (linux) give the following ouput:
0.0000000E+00 4

You can compile it with -r8 option to make it work with g95.

Joost

unread,
Feb 1, 2007, 3:35:07 AM2/1/07
to gg95
'Fly away' is correct in its analysis. The program above is incorrect,
but typically compilers won't notice the error (depending on compiler
flags), g95, however, notices this kind of error if the declaration
and the call is in the same file. If the error goes unnoticed the
result of the calling 'two()' can be right or wrong depending on how
the calling convention is / how the floating point format is (i.e. the
function two returns a double precision number, but (part of) the
memory location is read as a single precision number...). Also the NAG
compiler errors on this:

f95 mytest.f90
Error: mytest.f90: Wrong data type for reference to function TWO from
XX

Cheers,

Joost

beli...@aol.com

unread,
Feb 1, 2007, 10:03:25 AM2/1/07
to gg95
Thanks to you and Fly Away for your replies. Intel Visual Fortran 9.1
also gives an error when the

-warn:all

option is used.

Reply all
Reply to author
Forward
0 new messages