Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program in Fortran print different results with each execution

This program prints different values for the variable ar each time it is run. Nothing was changed in the code between runs.

      IMPLICIT REAL *8 (A-H,O-Z)

      PI = 0
      PI=4.D0*ATAN(1.D0)
      PRINT *, 'PI =', PI
    
      ar = 0
      ar = 243.90472242d0 * PI / 180d0
      PRINT *, 'ra in radians=', ra

      END

First time programming in Fortran. I’m compiling with gfortran on Ubuntu 18.04.6. Does anyone have an idea how to resolve this?

I’m putting below the outputs obtained in terminal:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 PI =   3.1415926535897931     
 ra in radians=   1.0600151175395439E-023

 PI =   3.1415926535897931     
 ra in radians=  -2.0629687487667335E-113

 PI =   3.1415926535897931     
 ra in radians=  -3.8284964032531453E+030

 PI =   3.1415926535897931     
 ra in radians=   3.1129032950886427E-197

 PI =   3.1415926535897931     
 ra in radians=  -1.9759068544432418E+296

 PI =   3.1415926535897931     
 ra in radians=   6.9844978200929715E-043

 PI =   3.1415926535897931     
 ra in radians=  -5.1456199117269666E-069

 PI =   3.1415926535897931     
 ra in radians=   8.9199956919604065E-293

 PI =   3.1415926535897931     
 ra in radians=  -4.2627025759448686E-296

 PI =   3.1415926535897931     
 ra in radians=   6.6564214481535599E+085

 PI =   3.1415926535897931     
 ra in radians=  -4.9868560013955360E-189

 PI =   3.1415926535897931     
 ra in radians=  -2.6981838685313715E+176

 PI =   3.1415926535897931     
 ra in radians=  -6.8783573079901012E+290

 PI =   3.1415926535897931     
 ra in radians=   2.5019209606350738E+051

 PI =   3.1415926535897931     
 ra in radians=   2.5043572241487943E+102

 PI =   3.1415926535897931     
 ra in radians=  -8.4319767436500338E+073

I expected to get something like:

PI = 3.1415926535897931

ra in radians= 4.256940467391832

>Solution :

PRINT *, ‘ra in radians=’, ra should be PRINT *, ‘ra in radians=’, ar
If I remember fortran Uninitialized variables may have random values.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading