[net.lang.f77] f77 Bug

draine@astrovax.UUCP (Bruce Draine) (02/25/84)

Subject: Bug in 4.2BSD f77 complex arithmetic

Description:
	Incorrect evaluation of complex arithmetic expressions
	involving library functions CCOS and CSIN.  For example,
	the fortran expression
		A=CCOS(Y)/CSIN(Y)
	with A and Y having been previously "typed" by a
		COMPLEX A,Y
	statement, is not being evaluated correctly for arbitrary
	values of the complex variable Y.
	It is not known to what extent this problem may also be
	endemic to other constructions [e.g., A=CSIN(Y)/CCOS(Y) ].

Repeat-By:
	Compiling and running the following simple program:

C PROGRAM TO TEST APPARENT BUG IN F77 COMPILER
C B.T.DRAINE,PRINCETON UNIV. OBS., 84/2/19
      COMPLEX Y,A,B,C
      Y=(4.601765,-3.839218)
      WRITE(6,9900)Y
 9900 FORMAT(' Y=',1P2E14.7)
C INDIRECT EVALUATION OF COTANGENT(Y)
      A=CCOS(Y)
      B=CSIN(Y)
      C=A/B
      WRITE(6,9969)A,B
 9969 FORMAT(' A=CCOS(Y)=',1P2E14.7,' B=CSIN(Y)=',2E14.7)
      WRITE(6,9970)C
 9970 FORMAT(' C=A/B=',1P2E14.7)
C DIRECT EVALUATION
      C=CCOS(Y)/CSIN(Y)
      WRITE(6,9971)C
 9971 FORMAT(' C=CCOS(Y)/CSIN(Y)=',1P2E14.7)
      STOP
      END

The following output is generated by this program running under
4.2 BSD on our Vax-11/750 with FPA:

***** testf77 compiled with no options 84/2/19 *****
output follows:
 Y= 4.6017652e+00-3.8392179e+00
 A=CCOS(Y)=-2.5673470e+00-2.3091778e+01 B=CSIN(Y)=-2.3113155e+01 2.5649724e+00
 C=A/B= 2.0288443e-04 9.9909759e-01
 C=CCOS(Y)/CSIN(Y)= 1.0000000e+00-1.4885653e-09

***** testf77 compiled with -C option 84/2/19 *****
output follows:
 Y= 4.6017652e+00-3.8392179e+00
 A=CCOS(Y)=-2.5673470e+00-2.3091778e+01 B=CSIN(Y)=-2.3113155e+01 2.5649724e+00
 C=A/B= 2.0288443e-04 9.9909759e-01
 C=CCOS(Y)/CSIN(Y)= 1.0000000e+00-1.4885653e-09

Fix:
	Unknown to me.  Has been reported to ucbvax\!bsd-bugs.

ray@utcsrgv.UUCP (Raymond Allen) (02/27/84)

	This is not a bug, FORTRAN protocol insists that the the functions
as well as the variables have to be typed, so you need another declaration:

	COMPLEX CSIN,CCOS

added to your program (otherwise FORTRAN assumes that CSIN and CCOS return
REAL data type.

	You might also use the -u option to f77.  With this option, any
variable or function appearing in the program that is not defined in
a type definition statement causes a fatal error.

	(Note: I did not actually run the corrected program but we use
lotsa complex fortran here and it seems to work all right.)

					from the FORTRAN infested
					keyboard of:

					Ray Allen
					utcsrgv!ray
					(416) 978-5036

pam@lanl-a.UUCP (08/02/84)

Can anyone provide a fix to this bug in f77 on 4.2BSD?  Please reply
directly to me, and I'll summarize to net.lang.f77.  The bug does not
occur if b=cmplx(abs(real(a)),abs(imag(a))) is replaced with
b=cmplx(real(a),abs(imag(a))).

      complex a, b

      a = (1.0, 2.0)
      b = cmplx (abs(real(a)), abs(imag(a)))

      print *, a, b

      stop
      end
	.data	0
	.align	2
L15:
	.long	0x4080
	.long	0x4100
	.align	2
L22:
	.long	0x0
	.align	2
L23:
	.long	6
	.align	2
L24:
	.long	1
	.align	2
L27:
.byte 00,00
	.set	LWM1,0x800
	.data	2
	.data	1
	.data	0
	.globl	_MAIN_
	.globl	_r_imag
	.globl	_s_wsle
	.globl	_do_lio
	.globl	_e_wsle
	.globl	_s_stop
	.align	2
	.stabs	"complex.f",0x64,0,0,0

.text
	.globl	_MAIN_
_MAIN_:
	.word	LWM1
	subl2	$LF1,sp
	jmp	L12
L13:
	movf	L15,v.2-v.1(r11)
	movf	L15+4,v.2-v.1+4(r11)
	movf	v.2-v.1(r11),-4(fp)
	cvtfd	-4(fp),-24(fp)
	cmpf	L22,-4(fp)
	jgtr	L9999
	movf	-4(fp),r0
	jbr	L9998
L9999:
	mnegf	-4(fp),r0
L9998:
	cvtfd	r0,-40(fp)
	cvtdf	-40(fp),-32(fp)
	clrf	-28(fp)
	movf	-32(fp),v.3-v.1(r11)
	movf	-28(fp),v.3-v.1+4(r11)
	pushl	$v.4
	calls	$1,_s_wsle
	pushl	$8
	addl3	$v.2-v.1,r11,r0
	pushl	r0
	pushl	$L24
	pushl	$L23
	calls	$4,_do_lio
	pushl	$8
	addl3	$v.3-v.1,r11,r0
	pushl	r0
	pushl	$L24
	pushl	$L23
	calls	$4,_do_lio
	calls	$0,_e_wsle
	pushl	$0
	pushl	$L27
	calls	$2,_s_stop
L14:
	ret
	.align	1
L12:
	movl	$v.1,r11
	jmp	L13
	.set	LF1,40
.text
	.data	2
	.align	2
v.4:
	.long	0
	.long	6
	.space	4
	.long	0
	.align	2
v.2:
	.space	8
	.align	2
v.3:
	.space	8
	.set	v.1,v.3

output from executable: 

  (   1.00000,   2.00000)  (   1.00000,  0.)

The above is incorrect.  The correct output should be:

  (   1.00000,   2.00000)  (   1.00000,  2.00000)