使用VBA和Excel函数,对数不同

时间:2022-09-02 09:02:05

I'm trying to get the log of a number but it looks like the Worksheet function gives one answer, and VBA another.

我正在尝试获取数字的日志,但看起来像Worksheet函数给出了一个答案,而VBA则是另一个答案。

In VB, using Log(Range("A2")) where A2 is 7, I get 1.94591014905531, which is the correct answer. But, if in the worksheet, I use =Log(A2,10) or =Log10(a2), I get 0.84509804. What's going on here?

在VB中,使用Log(范围(“A2”)),其中A2为7,我得到1.94591014905531,这是正确的答案。但是,如果在工作表中,我使用= Log(A2,10)或= Log10(a2),我得到0.84509804。这里发生了什么?

2 个解决方案

#1


12  

VBA's Log function is the natural log. If you want log base ten you will have to use the logarithmic identity for converting bases. Like so: Log(x)/Log(10).

VBA的Log功能是自然日志。如果您想要log base ten,则必须使用对数标识来转换碱基。像这样:Log(x)/ Log(10)。

#2


0  

Function roundit1(nn As Variant, sd As Variant)
    nn = Val(nn)
    If Not (nn = 0) Then
        xx = (1 + Int(Log(Abs(nn)) / Log(10)))
    Else
        xx = 0
    End If
    ' significant digits
    roundit = sd - xx
    If roundit < 0 Then roundit = 0
   roundit1 = Round(nn, roundit)
End Function

#1


12  

VBA's Log function is the natural log. If you want log base ten you will have to use the logarithmic identity for converting bases. Like so: Log(x)/Log(10).

VBA的Log功能是自然日志。如果您想要log base ten,则必须使用对数标识来转换碱基。像这样:Log(x)/ Log(10)。

#2


0  

Function roundit1(nn As Variant, sd As Variant)
    nn = Val(nn)
    If Not (nn = 0) Then
        xx = (1 + Int(Log(Abs(nn)) / Log(10)))
    Else
        xx = 0
    End If
    ' significant digits
    roundit = sd - xx
    If roundit < 0 Then roundit = 0
   roundit1 = Round(nn, roundit)
End Function