Numeric models

Contents
Epsilon spacing
Huge and tiny
Digits and radix
Choose kinds deliberately

Epsilon spacing

Real numbers have gaps. The epsilon intrinsic returns the distance from 1.0 to the next larger value for a given kind.

Use epsilon to set sensible tolerances instead of comparing reals for exact equality.

program eps_demo implicit none real :: x x = 1.0 print *, epsilon(x) if (abs(x - 1.0) .lt. epsilon(x)) then print *, "close to one" end if end program eps_demo

Huge and tiny

The huge intrinsic returns the largest finite value and tiny returns the smallest positive normal value for a kind.

Check limits before a risky computation to avoid overflow to infinity or underflow to zero.

program limits_demo implicit none real :: x x = 1.0 print *, huge(x) print *, tiny(x) end program limits_demo

Digits and radix

The digits intrinsic counts significant decimal digits and radix reports the number base used by the model, usually two.

Query them when you document accuracy or compare what two different kinds can represent.

program digits_demo implicit none real :: x integer :: d integer :: r x = 1.0 d = digits(x) r = radix(x) print *, "digits =", d, "radix =", r end program digits_demo

Choose kinds deliberately

Pick a kind once with selected_real_kind and name it, then use that name for every real in the unit.

This keeps precision in one place. To change accuracy later you edit one line instead of the whole file.

program kind_demo implicit none integer, parameter :: dp = selected_real_kind(15, 307) real(dp) :: a a = 1.0_dp print *, epsilon(a), huge(a) end program kind_demo

Next: Performance

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: