アプリ版:「スタンプのみでお礼する」機能のリリースについて

ベクトルからcosθを求めるペログラムを作ってみたのですが、実際計算した値と実行結果の値が一致しないのですが。プログラム上に問題があるのでしょうか?教えて下さい。よろしくお願いします。
module subprogs
implicit none
contains
function vec_cos(a,b) result(vcos)
real(8), intent(in) :: a(:), b(:)
real(8) ab, vcos
if (size(a) /= size(b)) stop ' er : size(a) /= size(b) '
ab = dot_product(a,a)* dot_product(b,b)
if (ab == 0.0d0) then
vcos = 0.0d0
else
vcos = dot_product(a,b)/sqrt(ab)
endif
end function vec_cos
end module subprogs

programmain
use subprogs
implicit none
real(8) :: x(1:2) = (/1.2d0, 3.4d0/), y(1:2) = (/5.6d0, 7.8d0/)
write(*,*) 'cos = ', vec_cos(x,y)
end program main

実行結果 cos = 0.9601163787292428

A 回答 (1件)

その「実際計算した値」というのはどのように計算したもので値はいくつになったのでしょうか?

    • good
    • 0
この回答へのお礼

cos = ( 1.2*5.6+3.4*7.8)/sqrt((1.2*1.2+3.4*3.4)*(5.6*5.6+7.8*7.8))=0.9531....
再度計算し直したらちゃんと実行結果の値と同じになりました。こちらのケアレスミスでした。ありがとうございました。

お礼日時:2009/10/20 23:40

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!