Fortran Problems when averaging a value

AI Thread Summary
The issue arises from the precision difference between real (4 bytes) and real(8) (8 bytes) in Fortran 90. When using real(4), the limited precision means that small changes, like adding 0.0116 to a large number (262144.00), may not register, resulting in suma2 not updating correctly. Switching to real(8) increases the precision, allowing for more accurate calculations and ensuring that small increments are reflected in the average. This change enhances the ability to capture subtle variations in the data being processed. Understanding this precision difference is crucial for accurate numerical computations in programming.
jbenet
Messages
2
Reaction score
0
Hi,
I am running a fortran 90 program and I have some problems when getting an average. It happens that at some point the value of suma2 is not updated, even when the value of the term s(fixed2(j))/real(nfixed) is not zero. Below is the output of fort.32. The problem dissapears when suma2 is declared as real(8), could anyone please tell me the reason?

Thank you very much
Code:
real :: suma2

10 do i = 1, ncycle

  call   mc_swap_thermo(n1,nsites,s,sorted,beta,lambda,seed,neigh,iacc,fixed1,fixed2,nfixed,sblock,sfixed,switch_thermo)

  do j = 1, nfixed

    suma2 = suma2 + s(fixed2(j))/real(nfixed)
    write(32,*) suma2, s(fixed2(j)), s(fixed2(j))/real(nfixed)

  end do
 
  steps=steps+1

end do 

   262144.000       0.00000000       0.00000000  
   262144.000      0.139704779       1.16420649E-02
   262144.000       0.00000000       0.00000000  
   262144.000      0.139704779       1.16420649E-02
   262144.000       0.00000000       0.00000000  
   262144.000      0.139704779       1.16420649E-02
   262144.000       0.00000000       0.00000000  
   262144.000      0.139704779       1.16420649E-02
   262144.000      0.139704779       1.16420649E-02
 
Last edited by a moderator:
Technology news on Phys.org
I think the real(8) increases the precision to double ie 64-bit precision whereas the real or real(4) is only 32-bit precision.
 
  • Like
Likes jbenet
jbenet said:
Hi,
I am running a fortran 90 program and I have some problems when getting an average. It happens that at some point the value of suma2 is not updated, even when the value of the term s(fixed2(j))/real(nfixed) is not zero. Below is the output of fort.32. The problem dissapears when suma2 is declared as real(8), could anyone please tell me the reason?

Thank you very much

real :: suma2

10 do i = 1, ncycle

call mc_swap_thermo(n1,nsites,s,sorted,beta,lambda,seed,neigh,iacc,fixed1,fixed2,nfixed,sblock,sfixed,switch_thermo)

do j = 1, nfixed

suma2 = suma2 + s(fixed2(j))/real(nfixed)
write(32,*) suma2, s(fixed2(j)), s(fixed2(j))/real(nfixed)

end do

steps=steps+1

end do

262144.000 0.00000000 0.00000000
262144.000 0.139704779 1.16420649E-02
262144.000 0.00000000 0.00000000
262144.000 0.139704779 1.16420649E-02
262144.000 0.00000000 0.00000000
262144.000 0.139704779 1.16420649E-02
262144.000 0.00000000 0.00000000
262144.000 0.139704779 1.16420649E-02
262144.000 0.139704779 1.16420649E-02
What you're encountering is the difference in precision between real (4 bytes) and real(8) (8 bytes). With 4-byte reals, you have only about 7 digits of precision. When you add .0116 to 262144.00, the change is too small to affect the larger number. Changing to 8-byte reals gives you precision out to 12 or 13 places, if I'm remembering correctly.
 
  • Like
Likes jbenet
Mark44 said:
Changing to 8-byte reals gives you precision out to 12 or 13 places, if I'm remembering correctly.
real*8 is the same as double in C, so the machine epsilon is 2−52 ≈ 2.22e-16, giving 15 to 16 decimal places.
 
  • Like
Likes jbenet and Mark44
Ok, I understand now. Thank you very much
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
6
Views
3K
Back
Top