fortran real data type
- User
fortran real data type
Assistant
The Fortran
REALdata type is used to represent floating-point numbers. Here's a comprehensive overview:
1. Basic REAL Types
Default REAL
real :: x, y, z x = 3.14159 y = -2.5e-3 ! Scientific notation- Precision: Typically 32-bit (single precision), ~7 decimal digits
- Range: Approximately ±1.2E-38 to ±3.4E+38
Double Precision
real(8) :: a, b ! Non-portable way double precision :: c, d ! Old-style declaration real(kind=8) :: e ! Kind parameter (compiler-dependent)- Precision: Typically 64-bit, ~15 decimal digits
- Range: Approximately ±2.2E-308 to ±1.8E+308
2. Modern Fortran: KIND Parameters
Portable Precision Control
use, intrinsic :: iso_fortran_env implicit none ! Standard kinds from iso_fortran_env real(real32) :: single_var ! 32-bit real(real64) :: double_var ! 64-bit real(real128) :: quad_var ! 128-bit (if supported) ! Using selected_real_kind integer, parameter :: sp = selected_real_kind(6, 37) ! ~6 digits, exponent ±37 integer, parameter :: dp = selected_real_kind(15, 307) ! ~15 digits, exponent ±307 real(sp) :: x real(dp) :: yChecking Precision
program check_precision use, intrinsic :: iso_fortran_env implicit none print *, "Single precision digits:", precision(1.0_real32) print *, "Double precision digits:", precision(1.0_real64) print *, "Single precision range:", range(1.0_real32) print *, "Double precision range:", range(1.0_real64) end program
3. Literal Constants
real :: a, b, c, d real(8) :: e, f ! Single precision literals a = 3.14159 ! Default real b = 3.14159e0 ! Explicit single precision c = 3.14159_4 ! With kind suffix ! Double precision literals d = 3.14159d0 ! Old-style double precision e = 3.14159_8 ! With kind suffix f = 3.141592653589793238_8 ! Extended precision
4. Common Operations
Arithmetic
real :: x = 2.5, y = 3.7, result result = x + y ! Addition result = x - y ! Subtraction result = x * y ! Multiplication result = x / y ! Division result = x**y ! Exponentiation result = mod(x, y) ! Modulo (remainder)Intrinsic Functions
real :: x = -3.7, y = 2.5 ! Basic math print *, abs(x) ! Absolute value: 3.7 print *, sqrt(y) ! Square root: 1.58... print *, exp(y) ! Exponential: e^y print *, log(y) ! Natural logarithm print *, log10(y) ! Base-10 logarithm ! Trigonometry (angles in radians) print *, sin(x), cos(x), tan(x) print *, asin(0.5), acos(0.5), atan(0.5) ! Rounding print *, int(x) ! Truncate to integer: -3 print *, nint(x) ! Round to nearest integer: -4 print *, ceiling(x) ! Round up: -3 print *, floor(x) ! Round down: -4
5. Arrays of REAL
real :: vector(100) real, dimension(10,10) :: matrix real, allocatable :: dynamic_array(:) ! Initialize vector = 0.0 matrix = reshape([(real(i), i=1,100)], [10,10]) ! Dynamic allocation allocate(dynamic_array(1000)) dynamic_array = 3.14159
6. Input/Output Formatting
real :: x = 123.456789 real(8) :: y = 123.456789012345_8 ! Default format print *, x, y ! Formatted output print '(F10.3)', x ! Fixed format: 123.457 print '(E12.4)', x ! Exponential: 0.1235E+03 print '(ES12.4)', x ! Scientific: 1.2346E+02 print '(G12.4)', x ! General format ! Reading input read(*,*) x ! Free format read(*,'(F10.3)') x ! Fixed format
7. Precision Issues
Floating-Point Comparison
real :: a = 0.1, b = 0.2, c = 0.3 real :: epsilon = 1.0e-6 ! WRONG: Direct comparison if (a + b == c) then ! May fail due to rounding ! CORRECT: Use tolerance if (abs(a + b - c) < epsilon) then print *, "Equal within tolerance" end ifMachine Epsilon
print *, "Machine epsilon:", epsilon(1.0) ! Single precision print *, "Machine epsilon:", epsilon(1.0d0) ! Double precision
8. Best Practices
-
Use
iso_fortran_envfor portability:use, intrinsic :: iso_fortran_env real(real64) :: x ! Portable 64-bit real -
Always specify precision for literals:
real(real64) :: pi = 3.141592653589793_real64 -
Avoid mixed precision:
real(real32) :: x = 1.0_real32 real(real64) :: y = 2.0_real64 ! y = x + y ! Promotes x to real64, but be explicit y = real(x, real64) + y -
Use parameter constants:
integer, parameter :: dp = kind(1.0d0) real(dp), parameter :: pi = 3.141592653589793_dp
This covers the essential aspects of Fortran's REAL data type. Let me know if you need specific examples or have questions about particular use cases!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.