Is it possible to set a parameter variable with NaN? and have that in a particular module. I want to use it for initialization of some other variables. Therefore, I'll be faced with a run-time error, if they are not updated, rather than simulations running with some random numbers. I am using GFORTRAN.
相关问题
- CABS(x) function for complex(8)
- Upper bound of random number generator
- Finding number of lines of a data file using comma
- When do Fortran module allocatable arrays go out o
- How are these double precision values accurate to
相关文章
- Does gfortran take advantage of DO CONCURRENT?
- Math.Max vs Enumerable.Max
- Fortran 90 - “Segmentation fault - invalid memory
- Reading records from a Fortran binary file in Pyth
- expand a dense matrix
- Unable to use f2py to link large PETSc/SLEPc Fortr
- Fortran compiler error installing PyOptSparse
- How to read comma delimited data file if some data
To add to Vladimir F's answer I'll mention that gfortran 5.0 (but not earlier) supports the IEEE intrinsic modules.
Instead of
one can use
This gives you a little flexibility over which of the NaN values you get. You also needn't worry about any signalling invalid flag. [But note that asking for
ieee_signaling_nan
may not really give you that.]Note that
ieee_value()
can't be used directly in initialization: a reference to it isn't a constant expression. For such use, take this approach to get the bit pattern and apply the method of the other answer.You'll also need to ensure that there is the support for the features for each datatype.
It is possible. You first have to find out which bit pattern represents one of the possible NaN values. You can store the bit pattern in an integer:
It gives: -2251799813685248
Then you can initialize your variables using
Similarly for 32 bit variables you get the integer -4194304, so that you can do
Many compilers have an option to do that for you for all real variables. As francescalus shows, in gfortran it is
-finit-real=nan
. Doing that manually gives you a finer control.Disclaimer: Be careful when switching to a different platform. Endianness and other issues could play a role, even though I think it could be actually OK. I assumed an IEEE conforming CPU.
See, francescalus's answer for an alternative which uses a standard function. Unfortunately, it is not applicable for
parameter
constants, but is useful.If you are stuck with a GFortran that does not have the intrinsic IEEE but does have the intrinsic iso_c_binding (like the one needed to build R on Windows), the following works and is equivalent to the C and R NaN (passes
is.nan
on R):Interestingly,
real(kind = c_double), parameter :: NAN = TRANSFER(z'7FF0000000000001', 1_c_double)
fails my check foris.nan
.