[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::fortran

Title:Digital Fortran
Notice:Read notes 1.* for important information
Moderator:QUARK::LIONEL
Created:Thu Jun 01 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1333
Total number of notes:6734

1326.0. "Unary vs Binary Operator Precedence" by AUBER::LORENZO (Philippe LORENZO: EVRY FRANCE TSSC Ultrix) Mon Jun 02 1997 12:09

	Hello,

	Dec Fortran 90 LRM says that "Unary + or -" operator have an higher
precedence than "Binary + or -" operator.

	Here is a sample program in which we do the same calculation between 2
real values, one time using basic real*8 type, the other time using structure
members.

	The results are not the same.
	
	I use Digital Fortran 90 V4.1-270 on D.U V4.0

=============================================

program toto
implicit none

structure /erreur/
	real*8 r1(3)
	real*8 r2
end structure

structure /err_bis/
	record /erreur/ err1
	real*8 l
end structure

record /err_bis/ err(3)
real*8 res(5)
integer i

real*8 a
real*8 b
real*8 sres(5)

do i=1,3
	err(i).err1.r1(:) = 0.0d0
	err(i).err1.r2 = 0.0d0
	err(i).l = 0.0d0
end do

! These variables will produce a bad result
err(1).err1.r2 = 0.2d0
err(1).err1.r1(3) = 9.0d0

! These variables will produce a good result
b = err(1).err1.r2
a = err(1).err1.r1(3)

res(1) = - err(1).err1.r1(3) - err(1).err1.r2	! - 9 - 0.2
sres(1) = - a - b

res(2) = -(err(1).err1.r1(3))- err(1).err1.r2	! -(9)- 0.2
sres(2) = -(a)- b

res(3) = -(err(1).err1.r1(3) + err(1).err1.r2)	! -(9 + 0.2)
sres(3) = -(a + b)

res(4) = - err(1).err1.r1(3) + err(1).err1.r2	! - 9 + 0.2 
sres(4) = - a + b

res(5) = - err(1).err1.r1(3) -(err(1).err1.r2)	! - 9 -(0.2)
sres(5) = - a -(b)

!write(*,*) err(1).err1.r1(3), err(1).err1.r2

write(*,*) '- 9 - 0.2  =>' ,res(1) ,sres(1)
write(*,*) '-(9)- 0.2  =>' ,res(2) ,sres(2)
write(*,*) '-(9 + 0.2) =>' ,res(3) ,sres(3)
write(*,*) '- 9 + 0.2  =>' ,res(4) ,sres(4)
write(*,*) '- 9 -(0.2) =>' ,res(5) ,sres(5)

end

=============================================================
Here is the result:
 - 9 - 0.2  =>  -8.80000000000000       -9.20000000000000
 -(9)- 0.2  =>  -9.20000000000000       -9.20000000000000
 -(9 + 0.2) =>  -9.20000000000000       -9.20000000000000
 - 9 + 0.2  =>  -9.20000000000000       -8.80000000000000
 - 9 -(0.2) =>  -8.80000000000000       -9.20000000000000
			^			^
			|			|
			WRONG ?			CORRECT ?

	I know that some of these lines are not good programming practice 
(something like "- a - b" isn't very clean) but why do we get a difference ?

	Thanks for help.

Philippe
T.RTitleUserPersonal
Name
DateLines
1326.1QUARK::LIONELFree advice is worth every centMon Jun 02 1997 12:513
Looks like a bug.  Thanks, we will investigate.

			Steve