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!

Problem in Reading Real Number

Status
Not open for further replies.

engr20009

Technical User
May 17, 2009
2
DE
Hallo,

i have a problem while reading real number in fortran. here i am converting a character into a real number by reading it in. while the value of 'F' is equal to '0.01' in the text file, when I read this in it shows a value of '9.99999998E-03',. i want that this should have value of '0.01' in the memory. following is the part of code.

character :: value*20
real :: F
value='0.01'
value=trim(value)
READ (value,*) F

please anyone can help me out!!!
 
I tried your code
Code:
program pok
character :: value*20
real :: F
value='0.01'
value=trim(value)
read (value,*) F
write(*,*) "F = ", F
end program pok

I compiled and executed it with g95 and gfortran
Code:
$ g95 pok.f90 -o pok
$ pok
 F =  0.01
$ gfortran pok.f90 -o pok
$ pok
 F =   9.99999978E-03

As you see in g95 it's ok - the value is 0.01, but in gfortran it shows what you said.
The floating point number has in both fortran compiler the right internal representation, the compilers differs only in the output formatting.
The solution is using the the format-statement, to define exactly the output format you want:
Code:
[COLOR=#a020f0]program[/color] pok
[COLOR=#2e8b57][b]character[/b][/color] :: value[COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]20[/color]
[COLOR=#2e8b57][b]real[/b][/color] :: F
value[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'0.01'[/color]
value[COLOR=#804040][b]=[/b][/color][COLOR=#008080]trim[/color](value)
[COLOR=#804040][b]read[/b][/color] (value,[COLOR=#804040][b]*[/b][/color]) F
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]10[/color]) F
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color]([COLOR=#ff00ff]'F ='[/color],[COLOR=#008080]F4.2[/color])
[COLOR=#a020f0]end program[/color] pok
then both fortran compilers shows the same - look:
Code:
$ g95 pok.f90 -o pok
$ pok
F =0.01

$ gfortran pok.f90 -o pok
$ pok
F =0.01
 
thanks for the reply.
you are right that using format specifier, it makes no difference.

I have again a question; if i use this value of 'F' in the calculation, will it treat as '0.01' or the '9.99999998E-03'.
because i multiplied this 'F' with '1000.0' then result was simply '10.0' but when I multiplied it with '.9' then it was '8.99999998E-03' rather '0.09'

 
Hi both

This is interesting. I tried the same program on my old F77 Microsoft compiler (for DOS on a PC) with the (*,*) format in the write statement, and the result was:

F = 1.000000E-02 (rather than 0,999999E-03)

Then I changed to format to (E14.8) to force 8 digits after the point and the result was still:

F = .10000000E-01


 
I thing the same as before:
The internal representation of the numbers is correct, the compilers differs only in the displayed format, which however you can define exactly in your program:

Code:
[COLOR=#a020f0]program[/color] pok
[COLOR=#2e8b57][b]character[/b][/color] :: value[COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]20[/color]
[COLOR=#2e8b57][b]real[/b][/color] :: F, V
value[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'0.01'[/color]
value[COLOR=#804040][b]=[/b][/color][COLOR=#008080]trim[/color](value)
[COLOR=#804040][b]read[/b][/color] (value,[COLOR=#804040][b]*[/b][/color]) F
V [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]9[/color] [COLOR=#804040][b]*[/b][/color] F
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]10[/color]) F, V
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color]([COLOR=#ff00ff]'F = '[/color],[COLOR=#008080]F4.2[/color], [COLOR=#ff00ff]', V = 9 * F = '[/color], [COLOR=#008080]F4.2[/color])
[COLOR=#a020f0]end program[/color] pok
Output:
Code:
$ g95 pok.f90 -o pok
$ pok
F = 0.01, V = 9 * F = 0.09

$ gfortran pok.f90 -o pok
$ pok
F = 0.01, V = 9 * F = 0.09
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top