Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to get first digit of a double precision number?

  1. Nov 1, 2009 #1
    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?
  2. jcsd
  3. Nov 1, 2009 #2


    User Avatar
    Homework Helper

    For the file, you could read in each line as a text string, then parse that string to find the "first" digit.
  4. Nov 1, 2009 #3
    Yeah, I just started trying that. Currently I'm looking into how to find the first character of a string in fortran..
  5. Nov 2, 2009 #4
    I think you use the : operator:

    print *,'first character is ' // ch(1:1)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook