×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Floating-point exception using recursive function

Floating-point exception using recursive function

Floating-point exception using recursive function

(OP)
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 

RE: Floating-point exception using recursive function

Seems to work on gfortran 9.3.0 with and without the -f parameters.

RE: Floating-point exception using recursive function

(OP)
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.

RE: Floating-point exception using recursive function

(OP)
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?

RE: Floating-point exception using recursive function

Which version of gfortran are you using and on which linux/windows environment

RE: Floating-point exception using recursive function

It works for me too, I have this compiler version
GNU Fortran (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

RE: Floating-point exception using recursive function

(OP)
GNU Fortran (MinGW.org GCC Build-2) 9.2.0, on Windows. I'm just compiling using the terminal, no special environment.

RE: Floating-point exception using recursive function

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 
`

RE: Floating-point exception using recursive function

(OP)
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.

RE: Floating-point exception using recursive function

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.

RE: Floating-point exception using recursive function

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close