To improve the time efficiency of my code, I'd like to test a lower precision for real number, using e.g. half precision (2 bytes).

However, I'm not sure if I can do that in Fortran. After playing with the intrinsic SELECTED_REAL_KIND procedure, it seems that gfortran can only deal with 4, 8, 10 or 16 bytes precisions.

Edit: Actually, I could have directly read the manual...

Is it then possible to use half precision floats in Fortran (maybe with another compiler)?

If not, would there be an easy alternative to prototype half precision code?

  • $\begingroup$ As far as I know, Fortran recognizes 4 byte floats (single precision, iso_fortran_env :: real32, iso_c_binding :: c_float, etc) as the smallest floating point data type. Are you sure that the 8 and 10 are separate floating point precisions? $\endgroup$
    – cbcoutinho
    Aug 2 '17 at 14:53
  • $\begingroup$ Out of curiosity, what operation does your code do that you only need 3 digits of precision? I've never seen this before. $\endgroup$ Aug 2 '17 at 15:30
  • $\begingroup$ @cbcoutinho I tried SELECTED_REAL_KIND with several arguments and got the integers 4, 8, 10 and 16 (and some error codes) as results. I assume those are the available precisions in bytes. (80 bits precision is a thing according to Wikipedia). $\endgroup$
    – Matthieu
    Aug 2 '17 at 15:52
  • 2
    $\begingroup$ @Steve For parametric studies and optimization problems. Since I'm running a lot of “simple” independent computations, the rounding errors should not pile up too much. And 3 digits can be sufficient to discard the worse solutions in an optimization algorithm. $\endgroup$
    – Matthieu
    Aug 2 '17 at 16:00
  • 6
    $\begingroup$ Most modern CPUs have instructions that perform floating point operations on IEEE standard single (4-byte) and double precision (8-byte) numbers. So, unless you are running on an unusual CPU, the best performance is likely to be with 4-byte floats. $\endgroup$ Aug 2 '17 at 16:33

The Fortran standard doesn't specify what precisions and ranges of floating point numbers need be supported by a given compiler. All it says is that at least two different kinds of real numbers need be supported and that larger of them takes exactly twice the memory to represent a number as the other (smaller) kind. What is actually supported depends upon what the compiler writer chose, though of course today to not find something that looks like IEEE floating point numbers would be rare.

However note I say "at least two", the compiler is free to support as many as the compiler writer wants to implement. Thus in theory there may be a compiler that supports what you want. Unfortunately I don't know of one.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.