文章
姚 鑫 · 三月 22 阅读大约需 2 分钟

第八十九章 SQL函数 LOG

第八十九章 SQL函数 LOG

返回给定数值表达式的自然对数的标量数值函数。

大纲

{fn LOG(expression)}

参数

  • expression - 数值表达式。

LOG 返回 NUMERICDOUBLE 数据类型。如果表达式为 DOUBLE 数据类型,则 LOG 返回 DOUBLE;否则,它返回 NUMERIC

描述

LOG 返回表达式的自然对数(以 e 为底)。 LOG 返回一个精度为 21、小数位数为 18 的值。

LOG 只能用作 ODBC 标量函数(使用大括号语法)。

示例

以下示例返回整数的自然对数:

SELECT {fn LOG(5)} AS Logarithm

1.609437912434100375

以下嵌入式 SQL 示例显示了整数 110LOGEXP 函数之间的关系:

请注意,此处需要 ROUND 函数来纠正由系统计算限制引起的非常小的差异。在上面的示例中,ROUND 被任意设置为 12 位十进制数字。

/// d ##class(PHA.TEST.SQLFunction).Log()
ClassMethod Log()
{
    s a = 1
    while a < 11 {
        &sql(
            SELECT {fn LOG(:a)} INTO :b
        )
        if SQLCODE '= 0 {
            w !,"Error code ",SQLCODE 
            q 
        } else {
            w !,"Logarithm of ",a," = ",b 
        }
        &sql(
            SELECT ROUND({fn EXP(:b)},12) INTO :c
        )
        if SQLCODE '= 0 {
            w !,"Error code ",SQLCODE
            q 
        } else {
            w !,"Exponential of log ",b," = ",c
            s a = a + 1 
        }
    }
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).Log()

Logarithm of 1 = 0
Exponential of log 0 = 1
Logarithm of 2 = .6931471805599453089
Exponential of log .6931471805599453089 = 2
Logarithm of 3 = 1.098612288668109691
Exponential of log 1.098612288668109691 = 3
Logarithm of 4 = 1.386294361119890618
Exponential of log 1.386294361119890618 = 4
Logarithm of 5 = 1.609437912434100375
Exponential of log 1.609437912434100375 = 5
Logarithm of 6 = 1.791759469228055002
Exponential of log 1.791759469228055002 = 6
Logarithm of 7 = 1.945910149055313306
Exponential of log 1.945910149055313306 = 7
Logarithm of 8 = 2.079441541679835929
Exponential of log 2.079441541679835929 = 8
Logarithm of 9 = 2.197224577336219384
Exponential of log 2.197224577336219384 = 9
Logarithm of 10 = 2.302585092994045684
Exponential of log 2.302585092994045684 = 10
0
0 13
讨论 (0)1
登录或注册以继续