Demo entry 5739920

I hate floating point arithmetic.

   

Submitted by anonymous on Jul 11, 2016 at 23:11
Language: Fortran. Code size: 745 Bytes.

program main
implicit none

  integer            :: i,j
  real(8), parameter :: M(4,4) = reshape([( exp(real(-i,8)) * sin(real(i,8)) + cos(real(-i+1,8)), i = 1,16 )], [4,4])
  real(8), parameter :: vec1(4) = [2.0_8, 3.9_8, 4.1_8, 5.3_8], vec2(4) = [7.9_8, 5.0_8, 4.0_8, 3.1_8]
  real(8)            :: result1, result2, resultlist(16), result3

  result1 = 0
  do i = 1,4
    do j = 1,4
      result1 = result1 + vec1(i) * M(i,j) * vec2(j)
    end do
  end do
  print *, result1

  resultlist = [( [( vec1(i)*M(i,j)*vec2(j), j = 1,4 )], i = 1,4 )]
  print *, sum(resultlist)

  result2 = dot_product(vec1, matmul(M, vec2))
  print *, result2

  result3 = dot_product(matmul(vec1, M), vec2)
  print *, result3
end program

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).