title: Division of double-precision by an integer in Fortran
body: <p>Right now I am dealing with Fortran codes of the man who worked on the project before me. The code is relatively huge so I would not provide it here. In a lot of places in this codes there are division of double precision value over integer. I.e.:</p>
  
  <pre><code>double precision :: a,c
  integer :: b
  a = 1.0d0
  b = 3
  c = a/b
  </code></pre>
  
  <p>This sometimes leads to a mistakes much worse than a regular floating point number mistake. I will copypaste one of such mistakes for you: 3.00000032899483. For his purposes this was OK, but for me this mistake is awful. It seems like fortran is dividing real number over integer and then enlarge number to double precision, adding some random stuff. It can be treated of course if I will write:</p>
  
  <pre><code>c = a/(real(b,8))
  </code></pre>
  
  <p>So, I can do it, but it will take very long time to search through all his codes. That's why, I tried to overload (/) operator in the next way:</p>
  
  <pre><code>MODULE divisionoverload
  !-----------------------------------------------------
     INTERFACE OPERATOR (/)
        MODULE PROCEDURE real_divoverinteger
     END INTERFACE OPERATOR ( / )
  CONTAINS
  !-----------------------------------------------------
  
  DOUBLE PRECISION FUNCTION real_divoverinteger(a,b)
     IMPLICIT NONE
     DOUBLE PRECISION, INTENT (IN) :: a 
     INTEGER, INTENT(IN) :: b 
  
     real_divoverinteger = a/real(b,8)
  END FUNCTION real_divoverinteger
  
  END MODULE divisionoverload
  </code></pre>
  
  <p>But the mistake gfortran4.8 gives me clearly indicates that this conflicts with intrinsic division operator:</p>
  
  <pre><code> MODULE PROCEDURE real_divoverinteger
                                            1
  Error: Operator interface at (1) conflicts with intrinsic interface
  </code></pre>
  
  <p>So what can I do to solve this problem without manually handling all the divisions by integer?</p>
  
  <p>Code which generates such numbers with printing format: </p>
  
  <pre><code> hx = (gridx(Nx1+1)-gridx(Nx1))/modx)
   do i = 2,lenx
      sub%subgridx(i)=sub%subgridx(i-1) + hx
   end do
   !*WHERE*
   !hx,gridx[1d array], sub%subgridx[1d array] - double precision
   !modx, lenx - integer
  </code></pre>
  
  <p>code for printing</p>
  
  <pre><code>subroutine PrintGrid(grid,N)
      integer, intent (in) :: N       
      double precision, dimension(N), intent (in) :: grid
      integer :: i
      print"(15(f20.14))", (grid(i), i=1,N)
          print*, ""
  end subroutine PrintGrid
  </code></pre>
  
  <hr>
  
  <p><strong>Solution:</strong>
  I apologize for this question. I should read code more careful. I found a mistake. The reproduction for the problem you get get in the next way:</p>
  
  <pre><code>program main
     double precision :: a
     a = 1.0/3
     print*, a
  end program main
  </code></pre>
  
  <p>So basically, he was performing real/integer division and assigned this value to real*8. I changed everything with codes like this: </p>
  
  <pre><code> program main
     double precision :: a
     a = 1.0d0/3
     print*, a
  end program main
  </code></pre>
  
  <p>And it worked! Thank you very much for help, without your answers I would be dealing with this problem much more.</p>