VBA中简单的递归函数,Excel不返回预期结果 [英] Simple recursive function in VBA with Excel not returning expected result
本文介绍了VBA中简单的递归函数,Excel不返回预期结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
- logcount(5):log(5)= 0,6 ...所以函数应该返回1
- logcount(89):log(89)= 1,9 ...和log(log(89))= 0, 28 ...所以函数应该返回2
- logcount(0,4)应该返回1
etc ...
所以我写了它,它不按预期工作!它总是为结果添加+1!它看起来像最后一个Else块总是被解释。任何帮助将非常感谢
函数logcount(x As Double)As Integer
如果x <= 0则
MsgBox您必须输入正值
退出函数
ElseIf Log(x)< 1然后
logcount = 1
Else
logcount = 1 + logcount(Log(x))
End If
End Function
解决方案
显然你的意思是一个10位的对数:
Log10 = Log(X) / Log(10#)
I have a simple recursive function to write in VBA that does the following : It must count the number of times we must take the log of a parameter 'x' to find log(x) < 1
Examples :
- logcount(5) : log(5) = 0,6... so the function should return 1
- logcount(89) : log(89) = 1,9... and log(log(89)) = 0,28... so the function should return 2
- logcount(0,4) should return 1 etc...
So I wrote it and it doesn't work as expected ! It always adds +1 to the result ! It looks like the last 'Else' block is always interpreted. Any help will be really appreciated
Function logcount(x As Double) As Integer
If x <= 0 Then
MsgBox "You must enter a positive value"
Exit Function
ElseIf Log(x) < 1 Then
logcount = 1
Else
logcount = 1 + logcount(Log(x))
End If
End Function
解决方案
Log
in VBA is the natural logarithm.
Apparently you meant a base-10 logarithm:
Log10 = Log(X) / Log(10#)
这篇关于VBA中简单的递归函数,Excel不返回预期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文