FORTRAN adding trailing zeros 0s

  • Fortran
  • Thread starter byrdman1982
  • Start date
  • Tags
    Fortran
In summary: The "read" statement looks like this:read(infile, *, skipping.);The "format" statement looks like this:format(infile, *, y=x);The "data" statement looks like this:data(infile, *, y=x);
  • #1
byrdman1982
1
0
Aloha all,

I am reading in real data values in a FORTRAN program that are something like 23.134. The problem is that the program adds extra numbers on the end to make it 8bit, so I end up with 23.13451. This is throwing everything off. Is there a way to make the values have trailing 0s instead of random numbers, so that they read 23.13400? I unfortunately need to have 3 decimal places for this particular project, so using 4 bit reals isn't going to work.

Thanks for the help,
Tom
 
Technology news on Phys.org
  • #2
Hi byrdman, welcome to PF!

If I understand correcty, you have a file containing something like

23.134

which you read into a real, and printing out that real results in

23.13451

I have never seen that behaviour. Which compiler are you using?
It would be helpful if you could post some sample data and the part of your code where you read in the data.

For now, the only thing I can think of is that ff the data file is structured ("neat" columns), using a formatted read might help.
 
  • #3
byrdman1982 said:
Aloha all,

I am reading in real data values in a FORTRAN program that are something like 23.134. The problem is that the program adds extra numbers on the end to make it 8bit, so I end up with 23.13451. This is throwing everything off. Is there a way to make the values have trailing 0s instead of random numbers, so that they read 23.13400? I unfortunately need to have 3 decimal places for this particular project, so using 4 bit reals isn't going to work.

Thanks for the help,
Tom
What you mean are 8 bytes and 4 bytes, not the 8 bits and 4 bits that you wrote.
 
  • #4
Perhaps he's using real*4 and real*8 specifications?

The "real*4" statement specifies the variable names to be single precision 4-byte real numbers which has 7 digits of accuracy ...

The "real*8" statement specifies the variable names to be double precision 8-byte real numbers which has 15 digits of accuracy ...

Quotes by http://www-classes.usc.edu/engr/ce/108/text/fbk01.htm
 
  • #5
byrdman1982 said:
Aloha all,

I am reading in real data values in a FORTRAN program that are something like 23.134. The problem is that the program adds extra numbers on the end to make it 8bit, so I end up with 23.13451. This is throwing everything off. Is there a way to make the values have trailing 0s instead of random numbers, so that they read 23.13400? I unfortunately need to have 3 decimal places for this particular project, so using 4 bit reals isn't going to work.

Thanks for the help,
Tom
My guess is that you are reading these value in incorrectly.
What do your read statement, format statement, and data line look like?
 
Last edited by a moderator:

1. How do I add trailing zeros to a FORTRAN number?

To add trailing zeros to a FORTRAN number, you can use the FORMAT statement in your code. For example, if you want to display a number with 3 decimal places, you can use the FORMAT statement as follows: WRITE(*, '(F3.0)') num, where num is your number. This will add trailing zeros to the number if necessary.

2. Can I add trailing zeros to a FORTRAN string?

No, trailing zeros can only be added to numbers in FORTRAN. If you want to add trailing zeros to a string, you will need to convert the string to a number first.

3. How do I remove trailing zeros from a FORTRAN number?

To remove trailing zeros from a FORTRAN number, you can use the TRIM function. This function removes all trailing spaces from a string, including trailing zeros. For example, TRIM('10.000') = '10'.

4. Is there a way to specify the number of trailing zeros to add in FORTRAN?

Yes, you can specify the number of trailing zeros to add in the FORMAT statement. For example, if you want to display a number with 5 decimal places, you can use the FORMAT statement as follows: WRITE(*, '(F5.0)') num.

5. Can I add trailing zeros to a FORTRAN number without displaying it?

Yes, you can use the TRIM function to add trailing zeros to a FORTRAN number without displaying it. For example, TRIM(CHAR(num)) will convert the number to a string and add trailing zeros to it.

Similar threads

  • Programming and Computer Science
Replies
4
Views
608
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top