文章
姚 鑫 · 二月 14 阅读大约需 3 分钟

第五十四章 SQL函数 EXP

第五十四章 SQL函数 EXP

返回数字的指数(自然对数的倒数)的标量数值函数。

大纲

{fn EXP(expression)}

参数

  • expression - 对数指数,数值表达式。

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

描述

Exp是指数函数e n,其中e是常数2.718281828。因此,要返回e的值,可以指定{fn exp(1)}Exp是自然对数函数log的逆函数。

EXP返回一个精度为36、小数位数为18的值。如果传递的值为NULL,则EXP返回NULL

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

示例

下面的示例返回常量e:

SELECT {fn EXP(1)} AS e_constant

2.718281828459045235

下面的嵌入式SQL示例返回整数010的指数值:

ClassMethod Exp()
{
    s a = 0
    while a < 11 {
    &sql(SELECT {fn EXP(:a)} INTO :b)
    if SQLCODE'=0 {
        w !,"错误代码 ",SQLCODE
        q 
    } else {
        w !,"指数 ",a," = ",b
        s a=a+1 }
    }
}
DHC-APP>d ##class(PHA.TEST.SQLCommand).Exp()

指数 0 = 1
指数 1 = 2.718281828459045235
指数 2 = 7.389056098930650228
指数 3 = 20.08553692318766774
指数 4 = 54.59815003314423907
指数 5 = 148.4131591025766034
指数 6 = 403.4287934927351225
指数 7 = 1096.633158428458599
指数 8 = 2980.957987041728276
指数 9 = 8103.083927575384008
指数 10 = 22026.46579480671652

下面的嵌入式SQL示例演示了explog相反:

ClassMethod Exp1()
{
    s x=7
    &sql(SELECT {fn EXP(:x)} AS Exp,
            {fn LOG(:x)} AS Log,
            {fn EXP({fn LOG(:x)})} AS ExpOfLog
        INTO :a,:b,:c)
    if SQLCODE'=0 {
        w !,"Error code ",SQLCODE
        q 
    } else {
        w "Exponential of ",x," = ",a,!
        w "Natural log of ",x," = ",b,!
        w "Exp of Log of  ",x," = ",c
    }
}
DHC-APP> d ##class(PHA.TEST.SQLCommand).Exp1()
Exponential of 7 = 1096.633158428458599
Natural log of 7 = 1.945910149055313306
Exp of Log of  7 = 7.000000000000000004

注意,在第三个函数中,调用数字输入和计算的返回值之间的小差异。下一个示例显示如何处理这种计算差异。

下面的嵌入式SQL示例显示了整数1到10的logexp函数之间的关系:

ClassMethod Exp2()
{
    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 
        } else {
            w !,"Exponential of log ",b," = ",c 
            s a = a + 1 
        }
    }
}
DHC-APP>d ##class(PHA.TEST.SQLCommand).Exp2()

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

请注意,这里需要ROUND函数来校正系统计算限制导致的非常小的差异。在上面的示例中,为此目的将舍入任意设置为12位十进制数字。

0
0 10
讨论 (0)1
登录或注册以继续