Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran error

  1. Nov 30, 2006 #1

    EL

    User Avatar
    Science Advisor

    Being a newbie on Fortran I need some help with localizing an error.
    When I run my executable I get the message:
    I get that somewhere in my code there's a division by zero, which obviously will not be accepted, but my question is where?
    Is there a way to get the position of the error pointed out?
     
  2. jcsd
  3. Nov 30, 2006 #2
    Are you getting a core file? If so, open it up with gdb/other debugger; the state of the stack might give you some idea where it's barfing.

    Is the error always in the same place? If it's not a huge run, maybe you can run under gdb or another debugger until it fails.

    There's always printlining.

    These will help you locate where the div by 0 happens; finding where the zero gets injected is tougher.

    If you're running IEEE 754 shouldn't you be able to register your own error handlers? It's not something I've ever done so I can't give you details, but it might be doable.
     
  4. Nov 30, 2006 #3

    0rthodontist

    User Avatar
    Science Advisor

    It's not on line 73, is it?
     
  5. Nov 30, 2006 #4

    EL

    User Avatar
    Science Advisor

    Thanks, I got the same advice from a colleague earlier today, and it pointed out the command where it all went wrong. I'm just about to try to locate in which file this "command" is.
    Btw I really hate Linux :devil: :biggrin: (For example, how logical is it to use a command called "grep" to do a search?...)
     
  6. Nov 30, 2006 #5

    EL

    User Avatar
    Science Advisor

    No, at first I thought something like that too. But I think (73) is just a code for "dividing by zero".
     
  7. Nov 30, 2006 #6

    berkeman

    User Avatar

    Staff: Mentor

    That seems the most likely candidate. But EL, if it's not 73, then are there any obvious candidates for a math divide by zero? Especially if it's a floating point complaint, it sounds like it's in a math operation. How many places are you doing floating point math divides? It could be from some other error, but the math sections would be the first to look at, it would seem. Also, can you control the execution with different data sets? What are the input and output data sets for this program?
     
  8. Nov 30, 2006 #7
    Here is a good reason for sub-programs -- it makes it easier to cut it down. Can you put your calculations into some modules and then see which sub causes the error?
     
  9. Nov 30, 2006 #8
    It might not always be as obvious as it may seem. For example, raising 0 to a negative power almost seems innocent.
     
    Last edited: Nov 30, 2006
  10. Nov 30, 2006 #9

    EL

    User Avatar
    Science Advisor

    No, as said, 73 has nothing to do with any line number.
    I'm modifying an already existing code, so I'm really not in full control of what's happening all the way. At least I've checked that there's no division by zero directly in the parts I've added, but the code is quite complex, with a lot of going back and forth between different files doing different stuff. Probably there's something I've missed to assign a value too, or something similar.
    Anyway, I'm quite sure nmtim's advice is the right way to go. It gave me the name of the object causing the problem, so now I just have to find where (and what) it is ...
    However, I'll have to wait for tomorrow since I discovered I couldn't access my files from home.
     
  11. Nov 30, 2006 #10
    If this is a run-time error, the best thing you could do is identify the position of the error. Seclude the area it is zero-dividing at by outputting a flag at several points in your program. See what the last flag it outputs is, and then place several more flags between that and the next flag. Keep repeating this until you have ioslated the position of the zero-division, then perhaps you can see what expression(s) are the cause of the error.
     
    Last edited: Nov 30, 2006
  12. Dec 1, 2006 #11

    EL

    User Avatar
    Science Advisor

    I have now found the subroutine "ffset" which gdb pointed out as the source of the error:
    I cannot see where there could be any division by zero?
     
  13. Dec 1, 2006 #12

    EL

    User Avatar
    Science Advisor

    Btw, what does "inplicit none" do?
     
  14. Dec 1, 2006 #13
    May it be that the "source" of the error is where the initiation of the variables involved took place? In other words, it would be after this called function, wherever res is used.

    Output each value before and after the call to ffset takes place. See what's happening.

    "implicit none" is an extention to Fortran 77, acceptable by most compilers. The command defines the variable declaration to be explicitely defined only. In other words, if the compiler finds a variable name that has not been defined, it will throw up your trash appropriately. Otherwise, its behaviour is undefined. It's there for your benefit.
     
    Last edited: Dec 1, 2006
  15. Dec 1, 2006 #14

    EL

    User Avatar
    Science Advisor

    I appreciate your help Sane.
    Here's the part of the code where ffset is called:
    Can you spot any potential "zeros"?
     
    Last edited: Dec 1, 2006
  16. Dec 1, 2006 #15
    I'm almost positive it has to do with what's happening right after the call to ffset. There seems to be some funny stuff going on. I can't exactly see where the problem is, but if "DCMPLX" divides by s at any given time, it will be dividing by zero.

    Are you able to isolate the problem with flags, as I had suggested earlier?
     
  17. Dec 1, 2006 #16

    EL

    User Avatar
    Science Advisor

    Well, I guess my biggest problem here is that I really don't now how to programe. I simply don't know what a "flag" is...:blushing: ...neither how to use them...
    But you're right I should try to check DCMPLX. (And get help to do some "flagging".)
     
  18. Dec 1, 2006 #17
    Oh, it's really nothing special at all.

    When I say "output a flag", all I mean is print some unique information out, so you know where it currently is inside the program. For example, you could do the following...

    Code (Text):
    *
    * the precision to which real calculations are done is
    *
    [b]print *, "Reached Block 1"[/b]

    precx = 1
    sold = 0
    do 1 i=1,1000
    precx = precx/2
    call ffset(s, 1 + precx)
    s = exp(log(s))
    if ( s .eq. sold ) goto 2
    sold = s
    1 continue
    2 continue
    precx = precx*8
    * (take three bits for safety)

    *
    * the precision to which complex calculations are done is
    *
    [b]print *, "Reached Block 2"[/b]

    precc = 1
    sold = 0
    do 3 i=1,1000
    precc = precc/2
    call ffset(s, 1 + precc)
    cs = exp(log(DCMPLX(s)))
    if ( DBLE(cs) .eq. sold ) goto 4
    sold = DBLE(cs)
    3 continue
    4 continue
    precc = precc*8
    * (take three bits for safety)

    *
    * for efficiency take them equal if they are not too different
    *
    [b]print *, "Reached Block 3"[/b]

    if ( precx/precc .lt. 4 .and. precx/precc .gt. .25 ) then
    precx = max(precc,precx)
    precc = max(precc,precx)
    endif
    *
    * and the minimum value the logarithm accepts without complaining
    * about arguments zero is (DOUBLE PRECISION cq DOUBLE COMPLEX)
    *
    [b]print *, "Reached Block 4"[/b]

    s = 1
    xalogm = 1
    do 5 i=1,10000
    call ffset(s, s/2)
    if ( 2*s .ne. xalogm ) goto 6
    xalogm = s
    5 continue
    6 continue
    if ( xalogm.eq.0 ) xalogm = 1d-307

    s = 1
    xclogm = abs(DCMPLX(s))
    do 7 i=1,10000
    call ffset(s, s/2)
    if ( 2*abs(DCMPLX(s)) .ne. xclogm ) goto 8
    xclogm = abs(DCMPLX(s))
    7 continue
    8 continue
    if ( xclogm.eq.0 ) xclogm = 1d-307
    You see which print statement was the last to occur. Then place more print statements after the location the last one was outputted. Keep repeating until you've isolated the area where execution is terminated. Simple. Right?

    Basically, this helps pin-point the problematic area. Then all attention can be focused on the potential of one expression being invalid, rather than the entire program.
     
  19. Dec 4, 2006 #18

    EL

    User Avatar
    Science Advisor

    Thanks again Sane, I'll go for it directly!
     
  20. Dec 4, 2006 #19

    EL

    User Avatar
    Science Advisor

    :frown: No, nothing happened. Just the same old
    It didn't print anything out...
     
  21. Dec 4, 2006 #20

    EL

    User Avatar
    Science Advisor

    Ok, learned I had to recompile alot of stuff first to make it work. It's looking better...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Fortran error
  1. Fortran NAN error (Replies: 4)

  2. [FORTRAN] Runtime error (Replies: 12)

Loading...