fortran real data type
- User
fortran real data type
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
fortran real data type
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In Fortran, the REAL data type is used to represent floating-point numbers (i.e., numbers with decimal points). Fortran supports multiple kinds of REAL variables to control precision and storage size, which is essential for scientific and numerical computations.
By default, a REAL variable in Fortran uses single-precision (typically 4 bytes), which provides:
Example:
REAL :: x, y
x = 1.2345
y = -9.876543E2 ! Scientific notation
Fortran allows you to specify the kind of REAL to control precision and storage size. This is especially important in numerical simulations where accuracy matters.
Each kind corresponds to a specific representation. The exact mapping (e.g., 4-byte, 8-byte) depends on the compiler.
INTEGER, PARAMETER :: sp = KIND(1.0) ! Single precision
INTEGER, PARAMETER :: dp = KIND(1.0d0) ! Double precision
REAL(KIND=sp) :: a ! Single-precision
REAL(KIND=dp) :: b ! Double-precision
SELECTED_REAL_KINDFor portability, use SELECTED_REAL_KIND to request a real kind with at least a certain number of decimal digits and exponent range.
INTEGER, PARAMETER :: rkind = SELECTED_REAL_KIND(p=15, r=307)
REAL(KIND=rkind) :: c
p=15: At least 15 decimal digits of precisionr=307: Exponent range up to at least 10³⁰⁷| Kind | Bytes | Precision (digits) | Range (approx) | Declaration Syntax |
|---|---|---|---|---|
| 4 | 4 | ~7 | ±10³⁸ | REAL*4, REAL(KIND=4) |
| 8 | 8 | ~15 | ±10³⁰⁸ | REAL*8, REAL(KIND=8) or DOUBLE PRECISION |
⚠️ Note:
REAL*4andREAL*8are non-standard and discouraged in modern Fortran. UseKINDinstead.
Literal constants must match the kind of the variable they're assigned to. Use kind suffixes to specify precision.
REAL(KIND=4) :: x = 1.0 ! Default kind (could be 4 or 8)
REAL(KIND=8) :: y = 1.0_8 ! Explicit kind 8
REAL(KIND=dp) :: z = 1.0_dp ! Using named constant dp
REAL(KIND=dp) :: pi = 3.1415926535897932_dp
Use intrinsic functions to inspect properties of a real kind:
PRINT *, "Precision of sp:", PRECISION(1.0_sp) ! ~6-7
PRINT *, "Precision of dp:", PRECISION(1.0_dp) ! ~15-17
PRINT *, "Exponent range:", RANGE(1.0_dp) ! ~307
Mixing different kinds in operations can lead to loss of precision or compiler errors.
REAL(KIND=4) :: a = 1.0
REAL(KIND=8) :: b = 1.0
REAL(KIND=8) :: c
c = a + b ! Mixed kinds: a is converted to double precision
REAL(KIND=8) :: x
x = 1.0 ! Could be single precision (default)
x = 1.0_8 ! Correct: explicitly double precision
selected_real_kind() for portable precision control.REAL*4 or REAL*8; prefer REAL(KIND=...).1.0_dp).INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(15, 307)
REAL(KIND=dp) :: velocity, pressure
PROGRAM real_example
IMPLICIT NONE
INTEGER, PARAMETER :: sp = KIND(1.0)
INTEGER, PARAMETER :: dp = KIND(1.0d0)
INTEGER, PARAMETER :: qp = SELECTED_REAL_KIND(33, 4931)
REAL(KIND=sp) :: a = 1.0_sp
REAL(KIND=dp) :: b = 1.0_dp
REAL(KIND=qp) :: c = 1.0_qp
PRINT *, "Precision of sp: ", PRECISION(a)
PRINT *, "Precision of dp: ", PRECISION(b)
PRINT *, "Precision of qp: ", PRECISION(c)
END PROGRAM real_example
REAL stores floating-point numbers.KIND to control precision and storage size.selected_real_kind() for portability._sp, _dp, etc.).Let me know if you'd like help with arrays of real numbers, I/O formatting, or performance considerations!