2

I am trying to change the precision of the abs and sign with gfortran (gcc version 5.3.1 and x86_64-linux-gnu). I know I can change to dabs and dsign to have double precision but what about quad precision, is it possible?

For sqrt(x) for instance, I simply change to x**(0.5q0) with the arg defined as a real(16). I read that some compilers do not have the intrinsic routines implemented in quad or extended precision, see e.g. here.

2 Answers2

4

If gfortran has been compiled with support for libquadmath (on Linux it typically is), then it supports this right away. All you need to do is declare your variables and literals quadruple precision, and use the generic versions abs and sign.

However, using real(16) is not portable (and outdated). Better use something standardized such as selected_real_kind(p=30).

Here is a short example to illustrate this:

program test
  integer, parameter  :: qp = selected_real_kind(p=30)
  real(qp)            :: a

  a = -2._qp

  print *, abs(a)
  print *, sqrt(-a)
end program

The output is:

2.00000000000000000000000000000000000      
1.41421356237309504880168872420969818

Compare this to the result of Wolfram alpha:

1.414213562373095048801688724209698078569671875376948073176
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
0

Or... https://software.intel.com/en-us/node/525335

PROGRAM!SUBROUTINE!FUNCTION
USE ISO_C_BINDING
IMPLICIT NONE
REAL(KIND=C_LONG_DOUBLE) :: A
!or maybe...
REAL(C_LONG_DOUBLE)      :: B
...
Holmz
  • 714
  • 7
  • 14
  • The table in your link says that `C_LONG_DOUBLE` is `"REAL(KIND=8 or 16)"` so it is NOT guaranteed to be quadruple precision. Not even with that compiler which is different from OP's gfortran. – Vladimir F Героям слава Jul 27 '16 at 09:11
  • Indeed *"On the x86 architecture, most C compilers implement long double as the 80-bit extended precision type supported by x86 hardware (sometimes stored as 12 or 16 bytes to maintain data structure alignment), as specified in the C99 / C11 standards (IEC 60559 floating-point arithmetic (Annex F)).* **An exception is Microsoft Visual C++ for x86, which makes long double a synonym for double."** https://en.wikipedia.org/wiki/Long_double It is an exception, but rather an important one. – Vladimir F Героям слава Jul 27 '16 at 09:13