VB6 Tan Function
The Tan function returns the tangent of an angle specified in radians.
Syntax
Tan(number)
Parameters
number: Required. A numeric expression representing an angle in radians.
Returns
Returns a Double representing the tangent of the angle.
Remarks
- The argument must be in radians, not degrees. To convert degrees to radians, multiply by
Pi/180. - Returns a
Doublevalue. - If the argument is a multiple of π/2 (except 0), the result is undefined (overflow error).
- Returns Null if the argument is Null.
- Use
Atnto get the arctangent (inverse tangent). - The tangent function is periodic with period π.
- For very large or very small arguments, floating-point rounding may affect results.
Typical Uses
- Trigonometric calculations
- Geometry and graphics
- Physics and engineering formulas
- Calculating slopes and angles
- Animation and simulation
- Signal processing
- Scientific computation
- Converting between coordinate systems
Basic Examples
Example 1: Tangent of 45 degrees
result = Tan(45 * 3.14159265358979 / 180)
' result = 1
Example 2: Tangent of Pi/4 radians
result = Tan(3.14159265358979 / 4)
' result = 1
Example 3: Using with Atn
angle = Atn(1)
result = Tan(angle)
' result = 1
Example 4: Handling Null
result = Tan(Null)
' result = Null
Common Patterns
Pattern 1: Convert degrees to radians
Function DegreesToRadians(degrees As Double) As Double
DegreesToRadians = degrees * 3.14159265358979 / 180
End Function
result = Tan(DegreesToRadians(60))
Pattern 2: Calculate slope from angle
Function SlopeFromAngle(angleRadians As Double) As Double
SlopeFromAngle = Tan(angleRadians)
End Function
Pattern 3: Use in triangle calculations
Function OppositeFromAdjacent(adjacent As Double, angleRadians As Double) As Double
OppositeFromAdjacent = adjacent * Tan(angleRadians)
End Function
Pattern 4: Animation rotation
angle = t * 3.14159265358979 / 180
y = Tan(angle) * x
Pattern 5: Periodic function
For i = 0 To 360 Step 45
Debug.Print Tan(i * 3.14159265358979 / 180)
Next i
Pattern 6: Error handling for undefined values
On Error Resume Next
result = Tan(3.14159265358979 / 2)
If Err.Number <> 0 Then
Debug.Print "Overflow error"
End If
On Error GoTo 0
Pattern 7: Use with arrays
For i = LBound(arr) To UBound(arr)
arr(i) = Tan(arr(i))
Next i
Pattern 8: Inverse calculation
angle = Atn(Tan(x))
Pattern 9: Normalize angle
angle = angle Mod (2 * 3.14159265358979)
result = Tan(angle)
Pattern 10: Use in coordinate conversion
y = r * Tan(theta)
Advanced Usage
Example 1: Trigonometric Table
For i = 0 To 90 Step 15
Debug.Print "Tan(" & i & ") = " & Tan(i * 3.14159265358979 / 180)
Next i
Example 2: Slope Calculation
Function Slope(degrees As Double) As Double
Slope = Tan(degrees * 3.14159265358979 / 180)
End Function
Example 3: Handling Undefined Values
On Error Resume Next
result = Tan(3.14159265358979 / 2)
If Err.Number <> 0 Then
result = Null
End If
On Error GoTo 0
Example 4: Use in Physics Formula
' Calculate projectile height
height = distance * Tan(angleRadians)
Error Handling
- Returns Null if argument is Null.
- Overflow error if argument is a multiple of π/2 (except 0).
Performance Notes
- Fast, constant time O(1).
- Floating-point rounding may affect results for large/small arguments.
Best Practices
- Always use radians, not degrees.
- Convert degrees to radians as needed.
- Handle possible overflow for undefined values.
- Use error handling for edge cases.
- Test with a range of values.
- Use with Atn for inverse calculations.
- Document expected input range.
- Avoid using with multiples of π/2.
- Use with arrays for batch calculations.
- Normalize angles for periodicity.
Comparison Table
| Function | Purpose | Input | Returns |
|---|---|---|---|
Tan |
Tangent | radians | Double |
Atn |
Arctangent | number | Double |
Sin |
Sine | radians | Double |
Cos |
Cosine | radians | Double |
Platform Notes
- Available in VB6, VBA,
VBScript - Consistent across platforms
- Returns Double
Limitations
- Argument must be in radians
- Undefined for odd multiples of π/2 (except 0)
- Returns Null for Null input
- No support for complex numbers
- Floating-point rounding errors possible