|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
problem reading REALs from *
real a,b,c write(*,10) 10 format('enter a: ',/) read(*.*)a write(*.15)a 15 format('a is ',F5.7) I think the output of writing a as F5.7 is bad - that means width 5 with 7 decimals precision....impossible, but that shouldn't cause the x1 output to be nan. I'm helping a poor girl finish her last semester in college from her hospital bed, and I have experience in C/java and visual basic. I figured we'd do a practice program that simply read a, b, and c from standard input and calculated x1 and x2 from the quadratic formula. Problem is, all I ever get is nan (not a number) from the calculations, and the test writes of a, b, and c immediately after they're 'read' are all *****. What am I missing? I understand there are different fortran syntaxes available, but from the class projects so far, this is the style they use. also, x1= (-b+((b**2)-(4*a*c))**0.5)/(2*a) Thanks, JB
"J" <nospamplease> wrote in message
news:6a-dnUAxt7LSzKDbnZ2dnUVZ_rKvnZ2d@adelphia.com...
> real a,b,c > write(*,10) > 10 format('enter a: ',/) > read(*.*)a > write(*.15)a > 15 format('a is ',F5.7) > I think the output of writing a as F5.7 is bad - that means width 5 with 7 > decimals precision....impossible, but that shouldn't cause the x1 output > to be nan. > I'm helping a poor girl finish her last semester in college from her > hospital bed, and I have experience in C/java and visual basic. I figured > we'd do a practice program that simply read a, b, and c from standard > input and calculated x1 and x2 from the quadratic formula. Problem is, > all I ever get is nan (not a number) from the calculations, and the test > writes of a, b, and c immediately after they're 'read' are all *****. > What am I missing? > I understand there are different fortran syntaxes available, but from the > class projects so far, this is the style they use. > also, > x1= (-b+((b**2)-(4*a*c))**0.5)/(2*a)
You needed commas where you had periods. real a write(*,10) 10 format('enter a: ') read(*,*)a write(*,15)a 15 format('a is ',F10.5) end You need 3 reals for the quadratic formula, which looks to be coded correctly. It compiles but still needs some work. What if the discriminant is less than zero? -- ww
On May 5, 10:53 pm, "J" <nospamplease> wrote: > real a,b,c > write(*,10) > 10 format('enter a: ',/) > read(*.*)a > write(*.15)a > 15 format('a is ',F5.7)
A beginner should not fuss with FORMAT statements. The program below shows how to read and then display a real variable. real a write(*,*) "enter a" read (*,*) a write (*,*) "a =",a end
In article <4LCdnX8nEM5wxKDbnZ2dneKdnZydn@comcast.com>, Wade Ward <inva @invalid.nyet> wrote: >"J" <nospamplease> wrote in message >news:6a-dnUAxt7LSzKDbnZ2dnUVZ_rKvnZ2d@adelphia.com... >> x1= (-b+((b**2)-(4*a*c))**0.5)/(2*a) ... >You need 3 reals for the quadratic formula, which looks to be coded >correctly. It compiles but still needs some work.
((b**2)-4*a*c))**0.5 is not wrong but sqrt((b**2)-4*a*c) should run faster. There are many ways to get substandard answewrs from that formula, though, beautifully explained by George E. Forsythe, "Solving a Quadratic Equation on a Computer, in "The Mathematical Sciences: Essays for COSRIMS" p.138-152 (MIT Press 1969) -- John Harper, School of Mathematics, Statistics and Computer Science, Victoria University, PO Box 600, Wellington 6140, New Zealand e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
"J" <nospamplease> wrote in message
news:6a-dnUAxt7LSzKDbnZ2dnUVZ_rKvnZ2d@adelphia.com...
> real a,b,c > write(*,10) > 10 format('enter a: ',/) > read(*.*)a > write(*.15)a > 15 format('a is ',F5.7) > I think the output of writing a as F5.7 is bad - that means width 5 with 7 > decimals precision....impossible, but that shouldn't cause the x1 output > to be nan. > I'm helping a poor girl finish her last semester in college from her > hospital bed, and I have experience in C/java and visual basic. I figured > we'd do a practice program that simply read a, b, and c from standard > input and calculated x1 and x2 from the quadratic formula. Problem is, > all I ever get is nan (not a number) from the calculations, and the test > writes of a, b, and c immediately after they're 'read' are all *****. > What am I missing? > I understand there are different fortran syntaxes available, but from the > class projects so far, this is the style they use. > also, > x1= (-b+((b**2)-(4*a*c))**0.5)/(2*a) > Thanks, > JB
I want to thank you all for your input. I believe i had 2 problems masking each other - the formatting made me think I wasn't reading floats properly (how do you fit 7 decimals of precision in only 5 spaces?) and the numbers I was using to test the formula were bogus so of course i came up with 'nan'. It seems my program was fine. Just stupid mistakes. JB
|
 |
 |
 |
 |
|