Assigning a lower precision number to a higher precision in Fortran90

后端 未结 1 1841
梦如初夏
梦如初夏 2020-12-20 10:34

A few questions regarding assigning literals in Fortran90. Using gfortran 4.6.

program:

program scratch
  implicit none
  integer, parameter :: RP =         


        
相关标签:
1条回答
  • 2020-12-20 11:12

    Let's look at the assignment to y. It is a single-precision variable and you assign a single-precision value to it. The decimal value is converted to the floating point representation used, which on most platforms is IEEE single precision, a binary floating point type. This has 23 bits of fraction, 8 bits of exponent and a sign bit. Because 2.2 isn't exactly representable in binary floating point, you get the closest (hopefully) representable value.

    Those "other digits" when you print to more places are the single precision value converted to decimal - since it isn't exact in decimal it tends to have additional non-zero digits. Some implementations will give you a reasonable number of additional digits, some might start to give you zeroes after a while, and some might just give random digits.

    2.125 is exactly representable in binary floating point, so it can be exactly converted in both directions.

    0 讨论(0)
提交回复
热议问题