Description
Sometimes an algorithm needs a way to refer to positive or negative infinity (e.g., integration bounds) or test if a number is infinite. How are we going to do that? I see three directions we could take
- Only implement such algorithms for IEEE-compliant real kinds. Use
ieee_arithmetic
to set and test for infinities. - Define special constants
inf_{kind}
which is IEEE positive infinity for supported kinds and is a special value (maybehuge(x) + 1.0
) for non-IEEE kinds. Likewise, testing for infinities either usesieee_arithmetic
or tests for equality with the defined magic number. - Create a derived type that represents infinity and behaves as much like an IEEE infinity as possible when used alongside reals.
Option 1 is simple but means that some stdlib functionality just can't be implemented for all a compiler's real kinds.
Option 2 is simple for IEEE-compliant kinds but introduces a lot of undefined behavior for non-IEEE kinds. (e.g.: If k
is a non-IEEE real kind, we can't guarantee that inf_k - 1.0_k
does the "right" thing semantically.)
Option 3 is complicated and might be a pain to use in practice. However, it's the only way I know of to get close to IEEE infinity semantics with non-IEEE reals.
I'd like to hear opinions and alternatives, if people have them.