Fortran Type mismatch problem FORTRAN 77

  • Thread starter Thread starter Peptid
  • Start date Start date
  • Tags Tags
    Fortran Type
AI Thread Summary
The discussion centers on a type mismatch error encountered while compiling a FORTRAN 77 code, specifically related to the function 'cmod'. The error arises because the function 'cmod' is not explicitly declared in the 'ERREV' function, leading the compiler to assume it is of type REAL*4 instead of REAL*8. To resolve this, adding the line "REAL*8 CMOD" in the 'ERREV' function is recommended, which successfully eliminates the error. Participants also suggest modernizing the code structure for better readability and maintenance, given its outdated style. The user expresses intent to implement these suggestions while ensuring compliance with licensing.
Peptid
Messages
4
Reaction score
0
Hi all,

I am trying to run a long FORTRAN 77 code in my laptop. Document contains a makefile and it is instructed to use 'make pgi' command to compile. Beside many warnings, each time I enter that command, it returns me ' Return type mismatch of function 'cmod' at (1) (REAL(4)/REAL(8))
analsubs.f90:766.13'

The relevant part of the code is:
REAL*8 FUNCTION ERREV(NN,QR,QI,MS,MP,ARE,MRE)
REAL*8 QI(NN), QR(NN),MS,MP,ARE,MRE,E
E =CMOD(QR(1),QI(1))*MRE/(ARE+MRE)
DO 10 I=1,NN
E = E*MS+CMOD(QR(I),QI(I))
10 CONTINUE
ERREV = E*(ARE+MRE)-MP*MRE
END FUNCTION errev

and part for 'cmod' is:

REAL*8 FUNCTION CMOD(R,I)
REAL*8 R,I,AR,AI,DABS,DSQRT
AR = DABS(R)
AI = DABS(I)
IF (AR .GE. AI) GO TO 10
CMOD = AI*DSQRT(1.0D0+(AR/AI)**2)
RETURN
10 IF (AR .LE. AI) GO TO 20
CMOD = AR*DSQRT(1.0D0+(AI/AR)**2)
RETURN
20 CMOD = AR*DSQRT(2.0D0)
END FUNCTION cmod

I don't understand the reason of the error since I believe that both AR and AI in cmod are defined in type REAL*8. Is there anyone who has an idea about it?

Thanks in advance,
Batu
 
Technology news on Phys.org
Stop using "D" as a prefix in the intrinsic functions and tell me what happens.

Also, please post the entire error message...I am sure that "1" is strategically placed under some line to indicate exactly where fortran thinks the problem is.
 
I don't thinkg gsal's advaice is relevant. (the D prefixes might not be necessary, but that doesn't make them wrong!)

You need to add the line
REAL*8 CMOD
in function ERREV. Each function or subroutine of a Fortran program is compiled independet of everyting else. You didn't declare the type of CMOD in ERREV, so the compiler assumes it is REAL*4. Later, when all the functions are linked together to make the executable program, that turns out to be wrong and you get the error.
 
I admit, maybe the removal of "D" is not quite the best thing to do...I forgot that you are using Fortran77...I think in Fortran90 (and beyond), the "D" would be unnecessary since Fortran90 can choose the correct function for you automatically.

By the way, how long is you code? I don't know about you but, after a better look at the source, I wouldn't resist the urge to modernize it. You might be using Fortran77, but it looks like this code was written in Fortran66 at most...that spaghetti code with the GOTO's gives it away...it's confusing to figure out what's supposed to be done when. I would also stop using uppercase all over, it's just make the screen so crowded.

You can start by explicitly writing it out and THEN optimize a bit. For example, first shot:
Code:
real*8 function cmod(r,i)
    real*8 r,i,ar,ai,dabs,dsqrt
    
    ar = dabs(r)
    ai = dabs(i)
    
    if (ar .lt. ai) then 
        cmod = ai*dsqrt(1.0d0+(ar/ai)**2)
        return
    end if
    
    if (ar .gt. ai) then 
        cmod = ar*dsqrt(1.0d0+(ai/ar)**2)
        return
    end if

    if (ar .eq. ai) then 
        cmod = ar*dsqrt(2.0d0)
        return
    end if    
    
end function cmod

Then again, the "if (ar .eq. ai) then" is not quite necessary since both of the previous equations would turn into this one for the case when ar.eq.ai; so, we could turn one of the other two into ".le." or ".ge."

Code:
real*8 function cmod(r,i)
    real*8 r,i,ar,ai,dabs,dsqrt
    
    ar = dabs(r)
    ai = dabs(i)
    
    if (ar .le. ai) then 
        cmod = ai*dsqrt(1.0d0+(ar/ai)**2)
        return
    end if
    
    if (ar .gt. ai) then 
        cmod = ar*dsqrt(1.0d0+(ai/ar)**2)
        return
    end if
    
end function cmod

Now, the two "IF" statements lend themselves to be put into a single "IF-THEN-ELSE" block

Code:
real*8 function cmod(r,i)
    real*8 r,i,ar,ai,dabs,dsqrt    
    
    ar = dabs(r)
    ai = dabs(i)
    
    if (ar .le. ai) then 
        cmod = ai*dsqrt(1.0d0+(ar/ai)**2)
    else
        cmod = ar*dsqrt(1.0d0+(ai/ar)**2)
    end if
    
    return
end function cmod

...now, that's a nice looking function :-)

my 2 cents
 
Adding the line REAL*8 CMOD solved the problem and this time I didn't get any error message. I thank you very much for the advice AlephZero.

gsal, the file is about 1500 lines long and worse is that I didn't write the code. This is a part of my project and I am trying to make it run first. Fortran 77, says the manual:) I do appreciate your help and will most probably change the parts as the way you have suggested if there will be no license issues.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
8
Views
2K
Replies
9
Views
5K
Replies
22
Views
5K
Replies
54
Views
5K
Replies
4
Views
3K
Back
Top