How to get first digit of a double precision number?

  • Thread starter Thread starter aaron2
  • Start date Start date
  • Tags Tags
    Precision
Click For Summary

Discussion Overview

The discussion revolves around extracting the first digit of a double precision number in Fortran, specifically in the context of generating and analyzing the Fibonacci sequence. Participants explore methods for reading and processing data from a text file.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant describes their goal of generating the first 1476 terms of the Fibonacci sequence and counting the occurrences of each first digit.
  • Another participant suggests reading each line of the output file as a text string and parsing it to find the first digit.
  • A later reply confirms they are attempting to implement the string parsing method and seeks advice on finding the first character of a string in Fortran.
  • Another participant proposes using the : operator to extract the first character from a string.

Areas of Agreement / Disagreement

Participants generally agree on the approach of reading the output as a string to extract the first digit, but there is no consensus on the specific implementation details.

Contextual Notes

Limitations include potential issues with formatting in the output file and the need for clarity on string manipulation in Fortran.

Who May Find This Useful

Readers interested in Fortran programming, particularly those working with numerical sequences and string manipulation in data processing.

aaron2
Messages
5
Reaction score
0
EDIT: Sorry, forgot to mention 'FORTRAN' in the subject line...

Hi, I'm new to fortran but I'm trying to generate the first 1476 terms of the fibonacci sequence, then examine the first digit of each term and store the number of 1s, 2s, 3s, ..., 9s that occur into an array. I have it to where it prints all of the terms to a text file, and I was going to try to read the first number from each new line.

However, it has been several hours and I have not been able to use this method, so I've decided to just get the first digit of the real number before actually writing it to the text document. Then, it's just a matter of reading the text document line by line without having to deal with all of the formatting business.

So, my question boils down to extracting the first digit of a double precision number (real*8) and storing it into an integer. Any ideas of how to do this?
 
Technology news on Phys.org
For the file, you could read in each line as a text string, then parse that string to find the "first" digit.
 
Jeff Reid said:
For the file, you could read in each line as a text string, then parse that string to find the "first" digit.

Yeah, I just started trying that. Currently I'm looking into how to find the first character of a string in fortran..
 
I think you use the : operator:

print *,'first character is ' // ch(1:1)
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
23K
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
8K