In Fortran, the double precision type is used to represent double-precision floating-point numbers, which provide higher precision than single-precision floating-point numbers.
Method #1 [double precision keyword]
The double precision keyword is used to declare variables that require double-precision floating-point representation, which typically provides higher precision than the default real type.
Double precision variables are represented using more bits, resulting in increased precision for numerical calculations.
Example
In this program, we declare three double precision variables x, y, and z. We assign the values 3.14159 and 2.71828 to x and y, respectively.
We then perform an arithmetic operation, adding x and y, and store the result in z.
Finally, we print the values of x, y, and z using the print statement.
program double_precision_example
implicit none
! Declare double precision variables
double precision :: x, y, z
! Assign values to the variables
x = 3.14159
y = 2.71828
! Perform arithmetic operations
z = x + y
! Print the results
print *, "x =", x
print *, "y =", y
print *, "z =", z
end program double_precision_example
Method #2 [using real(kind=8)]
In addition to using the double precision keyword, Fortran also provides the real(kind=8) declaration to specify double-precision variables explicitly. This allows you to have control over the precision of floating-point numbers.
Example
Here, we declare x, y, and z as real variables with kind=8, indicating double precision.
The kind=8 specifies that these variables should use 8 bytes of memory, which typically corresponds to double-precision floating-point representation.
The rest of the program is similar to the previous example. We assign values to x and y, perform a multiplication operation, and print the result.
program double_precision_example
implicit none
! Declare double precision variables using real(kind=8)
real(kind=8) :: x, y, z
! Assign values to variables
x = 3.14159
y = 2.71828
! Perform calculations
z = x * y
! Print the result
print *, "Result: ", z
end program double_precision_example