Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Floating-point exception using recursive function

Status
Not open for further replies.

alex422

Technical User
Dec 14, 2020
6
CH
Hi all,

I'm using the GNU compiler (gfortran) with the debugging options -finit-real=snan and -ffpe-trap=invalid. For a program with any real function that is using a separate result/return value (see example below) I get a floating-point exception (erroneous arithmetic operation). I don't understand why this is or how I could resolve the issue.

Thank you for any help.

Example code:

Code:
program main
    implicit none

    print *, real_function()     ! this works
    print *, real_function_res() ! raises the exception
    print *, rec_real_function() ! raises the exception

contains

    real function real_function()
        real_function = 3.
    end function real_function

    real function real_function_res() result(retval)
        retval = 3.
    end function real_function_res

    recursive real function rec_real_function() result(retval)
        retval = 3.
    end function rec_real_function
    
end program main
 
Seems to work on gfortran 9.3.0 with and without the -f parameters.
 
Yes, the problem is that it raises the error when using those debugging options. I have a code that I want to check for floating point exceptions that uses a recursive function. So I can't check if there are any actual floating point errors, since it just stops on that recursive function.
 
Oh, I read that it works *without* the -f parameters. Well it doesn't work with them for me, any idea why that might be?
 
Which version of gfortran are you using and on which linux/windows environment
 
It works for me too, I have this compiler version
GNU Fortran (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
 
GNU Fortran (MinGW.org GCC Build-2) 9.2.0, on Windows. I'm just compiling using the terminal, no special environment.
 
It's a strange error, I would try to reinstall the same compiler version, or try any different version - maybe an older too.

But just for curiosity, if you try this function definition, throws it the same error or does it work ?
Code:
function real_function_res2() result(retval)
    real::retval
    retval = 3.
end function real_function_res2

recursive function rec_real_function2() result(retval)
    real::retval
    retval = 3.
end function rec_real_function2
`
 
I actually already tried different compiler versions (forgot to mention this before), the 8.1.0 earlier and the 9.2.0 now, I get the error with both versions.

And I also get the error with your code mikrom.
 
I tried the code again on 2 platforms:
1) GNU Fortran (MinGW.org GCC-6.3.0-1) 6.3.0 running on Windows 10
2) GNU Fortran (GCC) 6.3.0 running on IBM i system
On both systems it works fine.

Your problem seems to be very strange. Try different computer.
 
Thank you for testing all this out!

I now tried it on a virtual Machine running on Linux (Ubuntu, with GNU Fortran 9.3.0) and had no issues there. I can use that as a workaround for now at least. It really is strange though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top