How to get first digit of a double precision number?

  • Thread starter aaron2
  • Start date
  • Tags
    Precision
In summary, The conversation is about trying to generate the first 1476 terms of the fibonacci sequence and then examining the first digit of each term and storing the number of 1s, 2s, 3s, ..., 9s that occur into an array. The person has been trying to use a method of reading the first number from each new line in a text document, but has not been successful. They have now decided to extract the first digit of a double precision number and store it as an integer. They are discussing possible ways to do this, such as reading each line as a text string and then using the : operator to find the first character of the string.
  • #1
aaron2
5
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
  • #2
For the file, you could read in each line as a text string, then parse that string to find the "first" digit.
 
  • #3
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..
 
  • #4
I think you use the : operator:

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

Related to How to get first digit of a double precision number?

1. How do you extract the first digit of a double precision number?

To extract the first digit of a double precision number, you can use the modulus operator (%) with a divisor of 10. This will give you the remainder when the number is divided by 10, which will be the first digit of the number. For example, if you have the number 123.45, the first digit would be 1 as 123.45 divided by 10 leaves a remainder of 1.

2. Can you use mathematical operations to get the first digit of a double precision number?

Yes, you can use mathematical operations to get the first digit of a double precision number. As mentioned before, using the modulus operator with a divisor of 10 will give you the first digit. You can also use other mathematical operations such as floor or ceiling functions to round down or up to the nearest integer, which would also give you the first digit.

3. Is there a difference between the first digit of an integer and a double precision number?

Yes, there is a difference between the first digit of an integer and a double precision number. The first digit of an integer is always the leftmost digit, while the first digit of a double precision number can vary depending on the number's magnitude and decimal point placement. For example, the first digit of 123 would always be 1, but the first digit of 0.123 could be either 1 or 0 depending on how you interpret the decimal point.

4. How can you get the first digit of a negative double precision number?

To get the first digit of a negative double precision number, you can use the absolute value function to remove the negative sign before using any other mathematical operations. This will ensure that you are working with the positive version of the number, and the first digit will remain the same regardless of the number's sign.

5. Are there any built-in functions in programming languages to get the first digit of a double precision number?

Yes, many programming languages have built-in functions for extracting the first digit of a double precision number. These functions may vary in name or implementation, but they typically involve using mathematical operations or string manipulation to retrieve the first digit. Some examples include the floor() function in Python, the parseInt() function in JavaScript, and the LEFT() function in SQL.

Similar threads

Replies
4
Views
407
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
10
Views
22K
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
976
  • Computing and Technology
Replies
4
Views
904
  • Programming and Computer Science
Replies
11
Views
1K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top