Paste: quick cos in fortran

Author: Rex
Mode: fortran
Date: Mon, 27 Apr 2009 08:19:53
Plain Text |
module pa7_tools
	contains
	real function my_cos(x)
		implicit none
		real, intent(in)::x
		real::x2,x4,x6
		
		x2=x**2
		x4=x2**2
		x6=x4*x2
		
		! Horner's method brought error
		! Unless done to this point
		my_cos = 1.+x2*(-1./2.+x2*(1./24.-&
		x2/720.+x4/40320.-x6/3628800.))
	
		return
	end function my_cos
end module pa7_tools

program pa7
	use pa7_tools
	real::x=3.14159265/4.
	write(*,*)"cos(",x,")="
	write(*,*)my_cos(x)
	stop
end program pa7

Annotation: fixed horner's method implementation

Author: Rex
Mode: fortran
Date: Tue, 28 Apr 2009 04:21:23
Plain Text |
module pa7_tools
	contains
	real function my_cos(x)
		implicit none
		real, intent(in)::x
		real::x2,x4,x6
		x2=x**2
		my_cos = 1.+x2*(-1./2.+x2*(1./24.+&
		x2*(-1./720.+x2*(1./40320.-x2*(1./3628800.)))))
		return
	end function my_cos
end module pa7_tools

program pa7
	use pa7_tools
	real::x=3.14159265/4.
	write(*,*)"cos(",x,")="
	write(*,*)my_cos(x)
	stop
end program pa7

New Annotation

Summary:
Author:
Mode:
Body: