Stored procedure to calculate factorial with natural logarithm

Pocket

This site has shown stored procedure and function to calculate factorial with SQL. Although the logic is so simple as extracting one from previous argument and multiplying to it, it requires high cost of computing power. Therefore, the maximum of argument is restricted to so relatively small integer. In this article, you can calculate factorial with converting argument to natural logarithm, adding them and re-converting it to power of e, the base of natural logarithm.

CREATE PROCEDURE [dbo].[LOG_FACT]
    @SrcNumber    FLOAT
AS
BEGIN
    DECLARE	@DesNumber    FLOAT
    SET @DesNumber = LOG(1)
    WHILE @SrcNumber > 0
    BEGIN
        SET @DesNumber = @DesNumber + LOG(@SrcNumber)
        SET @SrcNumber = @SrcNumber - 1
    END
    SELECT	EXP(@DesNumber)
END

Maximum of argument is 170.

EXEC LOG_FACT 170;
7.25741561530971E+306

Pocket

投稿者: admin

趣味:写真撮影とデータベース. カメラ:TOYO FIELD, Hasselblad 500C/M, Leica M6. SQL Server 2008 R2, MySQL, Microsoft Access.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です