Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran 77 for Math/Phys -- subroutine problems 1

Status
Not open for further replies.

Turtleworld

Programmer
Jul 8, 2005
2
CA
Hi,

I have started to learn this language to work on some math/physics oriented programs. Being new to the language, I have some rather 'newbie' questions. Your help would be appreciated.

1) I want to output several things to the screen on one line. I have been using write(*,*) which seems to act similar to
print "<statement>\n" or system.out.println, or cout << "<statement>" << endl, in other languages. Is there a way I can print things without ending the line?

2) To declare large reals, I have used real*8 to reserve 8 bytes of memory for my real variable. Is there a way to reserve more memory for variables? (I need this for sensitive calculations with many iterations). For example, something like real*128 myrealvariable.

3)I made a quick program to test subroutines. The code is as follows (please skim it.. it's not very long),

c Tests the dot product program (main)

program vtest

integer n = 9

real*8 v1(*), v2(*)

do i=1, n
v1(i)=1
v2(i)=1
enddo

real*8 dotp

dotp = vectordot(n, v1, v2)

write(*,*) dotp

AND the subroutine

c Dot product of two vectors.

subroutine vectordot(n, v1, v2)

integer i, n
real*8 v1(*), v2(*)

real*8 dot
dot = 0

do i=1, n
dot = dot + v1(i)*v2(i)
enddo

return dot
end

When I compile I get -->
f77 -g vtest.f vectordot.f -o vtest
vtest.f:
MAIN vtest:
"vtest.f", line 7: Error: declaration among executables
"vtest.f", line 14: Error: declaration among executables
vectordot.f:
vectordot:

What am I doing wrong?

Also, why can't I declare the vectors in the main program as say, real*8 v1(n), v2(n)?


Thanks for your time.
 
1) Add advance='no' to your format statement. eg
Code:
write (*, '(F10.2)', advance='no') xxx

2) Do you really need real*128? Are there that many significant figures in your calcs? real*8 or double precision will give you 15 digit accuracy with a range of 1E308. Should be enough for any real world calcs. Otherwise you will have to go to one of the high precision libraries which store numbers over arrays.

3) Your initial declaration is incorrect. The equivalent of const in C++ is parameter. Unlike C++ which you are probably used to, you can't declare stuff all over the place. This is more like C. Move your declarations to the top.
Code:
        program vtest
        integer n
        [COLOR=RED]parameter (n=9)[/COLOR]
        real*8 v1([COLOR=RED]n[/COLOR]), v2([COLOR=RED]n[/COLOR])
        [COLOUR RED]real*8 dotp[/COLOR]

vectordot should be declared as a function: not a subroutine. A void function in C++ is called a subroutine. Anything else is called a function.
Code:
      real*8 function vectordot(n, v1, v2)
 
Should have been
Code:
        program vtest
        integer n
        [COLOR=RED]parameter (n=9)[/COLOR]
        real*8 v1([COLOR=RED]n[/COLOR]), v2([COLOR=RED]n[/COLOR])
        [COLOR=RED]real*8 dotp[/COLOR]
I always forget that this site uses the US spelling of colour.
 
It's a very interesting question about real*16. It's your calc method responsibility to provide a proper precision. In practice target machine arithmetics with more than real*8 floats is illusion of precision (however there are some special cases where this arithmetics is useful).

The key points in numerical calculations:
1. Computer floats are not math reals.
2. Correct method is more important thing than float vars precision (on condition that you have more than real*4;).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top