0

I have the following code in an Excel VBA module:

Function f(a, b, c, d)
f = 1.95 * a * (b / c) ^ 0.06 * (d / c) ^ 0.06
End Function

Sub callf()
Debug.Print (f(-7, 10, 3.5, -6))
End Sub

When I run the sub callf I get an error:

Run time error '5': Invalid procedure call or argument

I am at a loss as to why this occurs. Am I not doing simple floating point operations here? Any ideas?

user32882
  • 5,094
  • 5
  • 43
  • 82
  • 2
    Because you are [raising `-1.7` to the power `0.06`](https://math.stackexchange.com/q/2174393). The [result](https://www.wolframalpha.com/input/?i=%28-6+%2F+3.5%29+%5E+0.06) is a complex number. – GSerg Oct 24 '21 at 15:25
  • 1
    Right.... raising a negative number to a fractional power results in a complex number *face palm* – user32882 Oct 24 '21 at 15:29
  • In your case it does, though not always, of course. -8^(1/3), for example, is real. – Jos Woolley Oct 24 '21 at 15:31
  • @JosWoolley yes, or the cubic root of -64. But trying in powershell actually still gives NaN which is strange `[Math]::Pow(-8,1/3)` – user32882 Oct 24 '21 at 15:34
  • 1
    @user32882 Because the result of calculating `1/3` is [not exactly](https://stackoverflow.com/q/588004/11683) one third. – GSerg Oct 24 '21 at 15:42
  • @GSerg In that case why does PowerShell not have an issue with `[Math]::Pow(8,1/3)`, correctly returning 2? – Jos Woolley Oct 24 '21 at 15:46
  • @JosWoolley Because the result of `Pow(8, 0.333333333333333)` is `1.9999999999999986...`, which is rounded to `2` according to [this rule](https://stackoverflow.com/a/1658420/11683). – GSerg Oct 24 '21 at 15:53
  • @GSerg you want to post your comment as an answer so I can accept it? – user32882 Oct 25 '21 at 19:50

1 Answers1

1

Because you are raising -1.7 to the power 0.06. The result is a complex number.

GSerg
  • 76,472
  • 17
  • 159
  • 346