IEEE arithmetic

Contents
Use ieee_arithmetic
Detect NaN
Handle infinities
Halt on exceptions

Use ieee_arithmetic

The intrinsic module ieee_arithmetic gives portable access to IEEE floating point queries. Add a use statement for it at the top of the unit, before implicit none. Start by asking whether the processor supports quiet NaN values.

program ieee_use use, intrinsic :: ieee_arithmetic implicit none real :: x = 1.0 logical :: ok ok = ieee_is_supported(ieee_quiet_nan, x) print *, ok end program ieee_use

Detect NaN

Quiet NaN values poison every computation they touch, so detect them early. Build one portably with ieee_value, then test with ieee_is_nan. Never compare with == because NaN is unequal to everything, including itself.

program nan_demo use, intrinsic :: ieee_arithmetic implicit none real :: x x = ieee_value(x, ieee_quiet_nan) if (ieee_is_nan(x)) write(*, "(a)") "got NaN" end program nan_demo

Handle infinities

Overflow can produce infinities instead of stopping the program. Build positive infinity with ieee_value and test with ieee_is_finite. Branch to safe fallback code when a value is not finite.

program inf_demo use, intrinsic :: ieee_arithmetic implicit none real :: x x = ieee_value(x, ieee_positive_inf) if (.not. ieee_is_finite(x)) write(*, "(a)") "infinite" end program inf_demo

Halt on exceptions

By default most builds continue past exceptions and just record a flag. Read the current halting mode and turn halting on for the exceptions you care about, such as overflow. Do this once at startup so faults surface at once.

program halt_demo use, intrinsic :: ieee_arithmetic implicit none logical :: halting call ieee_get_halting_mode(ieee_overflow, halting) call ieee_set_halting_mode(ieee_overflow, .true.) end program halt_demo

Next: Intrinsic matrix math

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: