Article: 227 of comp.lang.vhdl Path: ucsbcsl!network.ucsd.edu!usc!zaphod.mps.ohio-state.edu!think.com!ames!haven.umd.edu!darwin.sura.net!gatech!ukma!nntp.msstate.edu!nntp.msstate.edu!ashworth From: ashworth@ERC.MsState.Edu (Tom Ashworth) Newsgroups: comp.lang.vhdl Subject: factorial,ln,exp,real**real,sqrt Message-ID: Date: Fri, 21 Feb 92 06:57:21 PST Sender: news@ra.msstate.edu Organization: Microsystem Prototyping Laboratories Lines: 143 Nntp-Posting-Host: sam.erc.msstate.edu I was requested to post this again. If you can use it great. If you have any comments send them to me personally @ ashworth@erc.msstate.edu --filename: math_.vhdl -- --****************************************************************************** -- -- Mississippi State University -- Microsystem Prototyping Laboratory -- P.O. Box 6176 -- Mississippi State, MS 39762 -- phone: (601) 325-7559 -- email: mpl-vhdl@erc.msstate.edu -- --****************************************************************************** -- --Package : math_functions --Author : Tom Ashworth --Version : 1.1 --Last Modification Date : 02-20-92 --History: --02-20-92 T. Ashworth Created -- -- --Description: -- This package contains some common math functions. -- These functions are as follows: -- -- factorial -- ln -- exp -- ** -- sqrt --****************************************************************************** package math_functions is constant LN2 : real := 0.693147181; function factorial (X : integer) return real; function ln (X : real) return real; function exp (X : real) return real; function "**" (X, Y : real) return real; function sqrt (X : real) return real; end math_functions; package body math_functions is -------------------------------------------------------------------------------- function factorial(X : integer) return real is variable result : real; begin result := 1.0; if (X > 1) then for i in 2 to X loop result := result * real(i); end loop; end if; return result; end factorial; -------------------------------------------------------------------------------- function ln (X : real) return real is variable Result : real; variable tmpx : real; variable n : integer; variable acc : integer; begin assert not (X <= 0.0) report "ERROR : Can't take the LN of a negative number" severity error; tmpx := X; n := 0; --reduce X to a number less than one in order --to use a more accurate model that converges --much faster. This will render the log function --to ln(X) = ln(tmpx) + n * ln(2) where ln(2) is --defined as a constant. while (tmpx >= 1.0) loop tmpx := tmpx / 2.0; n := n +1; end loop; --acc determines the number of iterations of the series --these values are results from comparisons with the SUN --log() C function at a accuracy of at least 0.00001. if (tmpx < 0.5) then acc := 100; else acc := 20; end if; tmpx := tmpx - 1.0; result := real(n) * LN2; n := 1; while (n < acc) loop result := result + (tmpx**n)/real(n) - (tmpx**(n+1))/real(n+1); n := n +2; end loop; return Result; end ln; -------------------------------------------------------------------------------- function exp (X : real) return real is variable result,tmp : real; variable i : integer; begin if (X = 0.0) then result := 1.0; else result := X + 1.0; i := 2; tmp := (X**i)/factorial(i); result := result + tmp; while (abs(tmp) > 0.00001) loop i := i +1; tmp := (X**i)/factorial(i); result := result + tmp; end loop; end if; return result; end exp; -------------------------------------------------------------------------------- function "**" (X, Y : real) return real is begin return exp(Y * ln(X)); end "**"; -------------------------------------------------------------------------------- function sqrt (X : real) return real is begin return X**0.5; end; -------------------------------------------------------------------------------- end math_functions;