-- math_pkg.vhd -- by Toshio Iwata May/10/00 -- for sin(x) and cos(x) library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; package math_pkg is type realarray is array(natural range <>) of real; constant pai : real := 3.1415927; constant delta: real := 0.01; function sin( x : real ) return real; function cos( x : real ) return real; end math_pkg; package body math_pkg is -- calculate sin(x) by Taylor Tenkai function sin(x : real) return real is variable alpha, beta : real; variable result : real; variable thita : real; variable temp, temp2 : real; constant kaijo : realarray(0 to 11) := ( -6.0, -- -3! +120.0, -- 5! -5040.0, -- -7! +362880.0, -- 9! -39916800.0, -- -11! +6.227021e9, -- 13! -1.307674e12, -- -15! +3.556874e14, -- 17! -1.216451e17, -- -19! +5.109094e19, -- 21! -2.585201e22, -- -23! +1.551121e25 -- 25! ); begin thita := x/(2.0*pai); thita := thita - real(integer(thita)); alpha := 2.0*pai*thita; if (abs(alpha) < delta) then return alpha; end if; beta := alpha * alpha; temp := alpha; result := alpha; for i in 0 to 11 loop temp := temp * beta; temp2 := temp / kaijo(i); result := result + temp2; end loop; return result; end sin; -- cos(x) = sin(x+pai/2); function cos(x : real) return real is begin return sin(x+pai/2.0); end cos; end math_pkg;