0

A simple question. A similar question was there, but I did not get the exact one I am looking for.

I was just checking the limits of real data type in fortran 90 (using ifort compiler), reason being my actual code might reach that limit. To test, I simply gave a parameter mval1 (see code) and multiplied by 2. In ifort manual it says that the maximum value it can take is 10E38 and my value is much smaller than this. But while printing it is taking random digits towards the end (output is given at the bottom). Could anyone help me through this

PROGRAM TEST

  IMPLICIT NONE
  REAL(KIND=8), PARAMETER :: mval1 = 1073E12
  REAL(KIND=8) :: j
  j = real(2*mval1)
  PRINT *, j,mval1

END PROGRAM TEST

Output

 2.146000005234688E+015  1.073000002617344E+015
Vaidyanathan
  • 379
  • 3
  • 6
  • 16
  • 1
    You're seeing the loss of precision associated with floating-point numbers. See [here](http://floating-point-gui.de/) or [here](http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf) –  Jul 15 '14 at 22:21
  • In intel compiler manual it is saying 15-17 decimal precisions for real(kind =8) . I am looking at the 9th digit and still does not work – Vaidyanathan Jul 15 '14 at 22:24
  • 1
    possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) –  Jul 15 '14 at 22:27
  • Not a direct duplicate, of that one. Possibly of some other one, no time to search. Here the problem is in assignment of the default kind real as explained by High Performance Mark. – Vladimir F Героям слава Jul 16 '14 at 07:50
  • 2
    Beware that directly using kind numbers (as `real(kind=8)`) is not portable. In different compiler this kind might not exist at all or mean something different. See http://stackoverflow.com/questions/838310/fortran-90-kind-parameter (and ignore the first answer). – Vladimir F Героям слава Jul 16 '14 at 07:53
  • possible duplicate of [Why Are Floating Point Numbers Inaccurate?](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Alexander Vogt Jul 16 '14 at 08:05

1 Answers1

3

Two points to add to the commentary you've already had.

One: In Intel Fortran real(kind=8) variables are the same as IEEE-754 double precision numbers, and the maximum positive value is 1.797693134862316E+308. For single precision IEEE floating-point numbers, Intel Fortran real(kind=4), the maximum value is 3.4028235E+38 which is approximately your 10E38.

Two: Your declaration

REAL(KIND=8), PARAMETER :: mval1 = 1073E12

assigns a value of default real kind to mval1. I don't know what your default compiler settings are but check what happens if you change that line to

REAL(KIND=8), PARAMETER :: mval1 = 1073E12_8

which explicitly declares that the literal is of kind=8.

The answer to the question you don't quite ask, and which you are not quite given by the other comments is: floating-point arithmetic is tricky and programmers who would use it ought to familiarise themselves with at least the basic tricks. The Wikipedia article on floating-point numbers and arithmetic is a good starting point for your self-education. Whatever anyone else tells you ignore Goldberg's paper What every computer scientist should know about floating-point arithmetic until you start trying to implement your own floating-point numbers and arithmetic.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161