0

I write this code in the module:

Public Function first()
If (x + 1 < 0) Or (1 - 2 * Sin(x) < 0) Or Sqr(1 - 2 * Sin(x)) = 0 Then
first = "error"
Else
first = Sqr(x + 1) / Sqr(1 - 2 * Sin(x))
End If
End Function

It gives an error with certain values:

enter image description here

Where is the problem?

3
  • 1
    Why not describe what the problem is? What are these "certain values"? Also -- what is x? It is odd that you are not passing x as a parameter. Global variables are more often than not a design flaw. Commented Oct 4, 2017 at 14:05
  • 1
    When Sin(x) is 0.5 (30 degrees...?) then Sqr(1 - 2 * Sin(x)) is zero and you cannot divide by zero. Commented Oct 4, 2017 at 14:07
  • In context, x must be the parameter of the function. It looks like they translated it from Lithuanian (?) (where "pirma" means "first") and probably just dropped the x from the arguments. Commented Oct 4, 2017 at 14:26

2 Answers 2

1

I'm pretty sure that your intention is to evaluate Sin(x) where x is measured in degrees (if for no other reason than that evaluating at radians which are whole numbers other than 0 is quite rare), but the function Sin(x) works with radians. You can use the function Randians() to fix this:

Public Function first(ByVal x As Double) As Double
    x = Application.Radians(x)
    If (x + 1 < 0) Or (1 - 2 * Sin(x) < 0) Or Sqr(1 - 2 * Sin(x)) = 0 Then
        first = "error"
    Else
        first = Sqr(x + 1) / Sqr(1 - 2 * Sin(x))
    End If
End Function

Then, for example, first(7) evaluates to 1.218130941.

Sign up to request clarification or add additional context in comments.

Comments

0

When x is 7, Sin(x) is equal to 0.656986598718789.

When you plug this into the formula 1 - 2 * Sin(x), you get -0.313973197437578.

You cannot take the square root (i.e. Sqr(...)) of a negative number. I would suggest adding Abs(...) as a wrapper to guarantee a positive number but I have no idea what you are ultimately trying to accomplish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.