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!

numerical constant definition does not work ! 1

Status
Not open for further replies.

tservas

Programmer
Mar 29, 2005
6
DE
hello everybody,

during an intense programming session I stumbled accross a very strange problem. Have a look at the following simple program:

program test

implicit none

real(kind(1.0d0)), parameter :: real_const = 3.14159265358979
real(kind(1.0d0)) :: real_variable
real(kind(1.0d0)) :: real_variable_readin
character(128) :: real_char

real_char = '3.14159265358979'
real_variable = 3.14159265358979
read(real_char,*), real_variable_readin

write(*,*), real_variable
write(*,*), 2*acos(0.0d0)
write(*,*), real_variable_readin

end program test

The Ouptput should be three times the same value:

3.14159265358979
3.14159265358979
3.14159265358979

BUT IT IS NOT --> on my computer the output looks like this:

3.14159274101257
3.14159265358979
3.14159265358979

I'm using g95 (newest version 2005-07-14) on a dual opteron workstation running under linux ...

Can anybody tell me what's going on ...

thx

tservas
 
The * in the read statement assumes G format which probably defaults to real instead of double precision. You probably need to specify something like F17.15
 
... I'm happy with the behaviour of the read statement, the problem is the output of the real_variable = 3.14159265358979 which should give

3.14159265358979

but instead it gives

3.14159274101257


... ???
 
Consult your compiler docs: what's a type of float literals. It seems you have real*4 value. Add D0 in
Code:
real_variable = 3.14159265358979D0
and try again.

It's in C/C++ languages float literals have a type of double. I don't remember what's its type in Fortran, but it does not depend on the left side of assignment, of course.
 
... thank you, this did the trick ... :)))))))))))))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top