Is it possible to obtain more than 16 digits with double
precision without using quadruple
? If it is possible, does it depend on compiler or something else? Because I know someone said he was working with double
precision and had 22 digit precision.
问题:
回答1:
The data type double precision
stems from Fortran 77, and the only requirement for that type is that is has more precision than real
. You shouldn't use that any more.
In Fortran 90/95 and beyond, at least two sizes of real numbers are supported. The precision is determined by the kind
parameter, of which the value depends on the compiler.
real(kind=8) :: a, b
To have a portable way of defining precision, you can obtain a kind
value that allows a certain precision by using:
integer, parameter :: long_double = SELECTED_REAL_KIND(22)
then you can declare your variables as
real(kind=long_double) :: a, b
but it is not certain your compiler will support that precision, in which case the SELECTED_REAL_KIND
function will return a negative number.
see also this post
回答2:
As the first answer states, the most portable way is to specify the precision of number is to use the intrinsic functions. The design concept of the language is that you figure out what precision is required for your calculation and request it, and the compiler provides that precision or better. E.g., if you calculate that your differential equation solution is stable with 11 decimal figures, then you request this value and the compiler provides its best type meeting your requirement. This is a foreign approach to most programmers, who are used to thinking about what the few choices provided by hardware rather than what they need, and maybe not so easy since few of us are numerical analysts.
If you want to use the SELECTED_REAL_KIND intrinsic and optimize for your particular compiler and hardware, you may wish to experiment. Some combinations will provide quadrupole precision in software, which will be slow. A compiler that has double precision and 10-byte extended precision will provide that longer type via selected_real_kind (17). A compiler that has double precision and quadruple precision but not 10-byte extended precision will provide quadrupole precision via selected_real_kind (17) or selected_real_kind (32). (I'm not aware of any compiler that supports both 10-byte extended and quadrupole.) The compiler that lacks quadrupole precision will return -1 for selected_real_kind (32).