Solved

Trig functions in radians

Posted on 2002-07-09
3
588 Views
Last Modified: 2012-08-14

I'm trying to set up a query which involves finding the cos of an angle.  Unfortunately the equipment which provides the angles gives them in degrees, and access seems to work in radians.  

How do I convert them - help suggests the RADIANS function, but the query returns an error message with this.

Thanks
0
Comment
Question by:bigc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
perove earned 100 total points
ID: 7140469
I'm not very good at trigomery (not good is a understatement) BUT I had this snippets if code lying around.

Somthing you can use maby..

Option Compare Database   'Use database order for string comparisons
Option Explicit

Function ArcCos(x As Double) As Double
' Inverse Cosine
  If x = 1 Then
    ArcCos = 0
  ElseIf x = -1 Then
    ArcCos = -PI()
  Else
    ArcCos = Atn(x / Sqr(-x * x + 1)) + PI() / 2
  End If
End Function

Function Arccosec(x As Double) As Double
' Inverse Cosecant
  Arccosec = Atn(x / Sqr(x * x - 1)) + (Sgn(x) - 1) * PI() / 2
End Function

Function Arccotan(x As Double) As Double
' Inverse Cotangent
  Arccotan = Atn(x) + PI() / 2
End Function

Function ArcSec(x As Double) As Double
' Inverse Secant
  ArcSec = Atn(x / Sqr(x * x - 1)) + Sgn(Sgn(x) - 1) * PI() / 2
End Function

Function ArcSin(x As Double) As Double
' Inverse Sine
  If x = 1 Then
    ArcSin = PI() / 2
  ElseIf x = -1 Then
    ArcSin = -PI() / 2
  Else
    ArcSin = Atn(x / Sqr(-x * x + 1))
  End If
End Function

Function ATan2(x As Double, y As Double) As Double
'
' Returns the ArcTangent based on X and Y coordinates
' If both X and Y are zero an error will occur.
'
' The positive X axis is assumed to be 0, going poistive in the
' counterclockwise direction, and negative in the clockwise direction.
'
  If x = 0 Then
    If y = 0 Then
      ATan2 = 1 / 0
    ElseIf y > 0 Then
      ATan2 = PI() / 2
    Else
      ATan2 = -PI() / 2
    End If
  ElseIf x > 0 Then
    If y = 0 Then
      ATan2 = 0
    Else
      ATan2 = Atn(y / x)
    End If
  Else
    If y = 0 Then
      ATan2 = PI()
    Else
      ATan2 = (PI() - Atn(Abs(y) / Abs(x))) * Sgn(y)
    End If
  End If
End Function

Function Cosec(x As Double) As Double
' Cosecant
  Cosec = 1 / Sin(x)
End Function

Function Cotan(x As Double) As Double
' Cotangent
  Cotan = 1 / Tan(x)
End Function

Function Deg2Rad(x As Double) As Double
' Degrees to radians
  Deg2Rad = x / 180 * PI()
End Function

Function HArccos(x As Double) As Double
' Inverse Hyperbolic Cosine
  HArccos = Log(x + Sqr(x * x - 1))
End Function

Function HArccosec(x As Double) As Double
' Inverse Hyperbolic Cosecant
  HArccosec = Log((Sgn(x) * Sqr(x * x + 1) + 1) / x)
End Function

Function HArccotan(x As Double) As Double
' Inverse Hyperbolic Tangent
  HArccotan = Log((x + 1) / (x - 1)) / 2
End Function

Function HArcsec(x As Double) As Double
' Inverse Hyperbolic Secant
  HArcsec = Log((Sqr(-x * x + 1) + 1) / x)
End Function

Function HArcsin(x As Double) As Double
' Inverse Hyperbolic Sine
  HArcsin = Log(x + Sqr(x * x + 1))
End Function

Function HArctan(x As Double) As Double
' Inverse Hyperbolic Tangent
  HArctan = Log((1 + x) / (1 - x)) / 2
End Function

Function HCos(x As Double) As Double
' Hyperbolic Cosine
  HCos = (Exp(x) + Exp(-x)) / 2
End Function

Function HCosec(x As Double) As Double
' Hyperbolic Cosecant = 1/HSin(X)
  HCosec = 2 / (Exp(x) - Exp(-x))
End Function

Function HCotan(x As Double) As Double
' Hyperbolic Cotangent = 1/HTan(X)
  HCotan = (Exp(x) + Exp(-x)) / (Exp(x) - Exp(-x))
End Function

Function HSec(x As Double) As Double
' Hyperbolic Secant = 1/HCos(X)
  HSec = 2 / (Exp(x) + Exp(-x))
End Function

Function HSin(x As Double) As Double
' Hyperbolic Sine
  HSin = (Exp(x) - Exp(-x)) / 2
End Function

Function HTan(x As Double) As Double
' Hyperbolic Tangent = HSin(X)/HCos(X)
  HTan = (Exp(x) - Exp(-x)) / (Exp(x) + Exp(-x))
End Function

Function PI() As Double
  PI = Atn(1) * 4
End Function

Function Rad2Deg(x As Double) As Double
' Radians to Degrees
  Rad2Deg = x / PI() * 180
End Function

Function Sec(x As Double) As Double
' Secant
' This will choke at PI/2 and 3PI/2 radians (90 & 270 degrees)
'
  Sec = 1# / Cos(x)
End Function
0
 
LVL 9

Expert Comment

by:perove
ID: 7140497
I found this also..

**
Option Compare Database   'Use database order for string comparisons
Option Explicit

Type Complex
  i As Double
  r As Double
End Type

Type Polar
  z As Double        ' Magnitude
  Th As Double       ' Theta - angle
End Type

Sub Complex2Polar(a As Complex, Result As Polar)
'
' Converts a Complex number into Polar form
'
  Result.z = IMMag(a)
  Result.Th = ATan2(a.r, a.i)       ' From Trig module
End Sub

Sub IMAdd(a As Complex, b As Complex, Result As Complex)
'
' Adds 2 complex numbers
'
  Result.i = a.i + b.i
  Result.r = a.r + b.r
End Sub

Sub IMConjugate(a As Complex, Result As Complex)
'
' Returns the Conjugate of A
'
  Result.r = a.r
  Result.i = -a.i
End Sub

Sub IMDiv(a As Complex, b As Complex, Result As Complex)
'
' Divides A by B
'
Dim B1 As Complex
  IMInverse b, B1
  IMProduct a, B1, Result
End Sub

Sub IMExp(a As Complex, Result As Complex)
'
' Does e^A where A is a complex number
'
Dim A1 As Complex, z As Double
  A1 = a
  z = Exp(A1.r)
  Result.r = z * Cos(A1.i)
  Result.i = z * Sin(A1.i)
End Sub

Function IMFormat(a As Complex) As String
'
' Formats a Complex number into a string
'
  If a.i >= 0 Then
    IMFormat = a.r & " + " & a.i & "i"
  Else
    IMFormat = a.r & " - " & Abs(a.i) & "i"
  End If
End Function

Sub IMInverse(a As Complex, Result As Complex)
'
' Finds the inverse value of A, such that A * Result = 1
'
Dim Temp As Double
  Temp = a.r * a.r + a.i * a.i
  Result.r = a.r / Temp
  Result.i = -a.i / Temp
End Sub

Sub IMln(a As Complex, Result As Complex)
'
' Returns natural Logarithm of A
'
Dim A1 As Complex
  A1 = a
  Result.r = Log(IMMag(A1))
  Result.i = ATan2(A1.r, A1.i)
End Sub

Sub IMLog10(a As Complex, Result As Complex)
'
' Takes Log A base 10
'
  IMln a, Result
  IMRProduct Result, Log10(Exp(1)), Result
End Sub

Sub IMLog2(a As Complex, Result As Complex)
'
' Takes Log A base 2
'
  IMln a, Result
  IMRProduct Result, Log2(Exp(1)), Result
End Sub

Function IMMag(a As Complex) As Double
'
' Returns Magnitude of Complex number
'
  IMMag = Sqr(a.r * a.r + a.i * a.i)
End Function

Function IMPFormat(a As Polar) As String
'
' Formats a Polar number into a string
'
  If a.Th >= 0 Then
    IMPFormat = a.z & " + " & a.Th & "Th"
  Else
    IMPFormat = a.z & " - " & Abs(a.Th) & "Th"
  End If
End Function

Sub IMPower(a As Complex, b As Complex, Result As Complex)
'
' A^B where both are complex numbers
'
Dim Temp1 As Complex, Temp2 As Complex
  IMln a, Temp1
  IMProduct Temp1, b, Temp2
  IMExp Temp2, Result
End Sub

Sub IMProduct(a As Complex, b As Complex, Result As Complex)
'
' Multiples 2 complex numbers
'
Dim A1 As Complex, B1 As Complex          ' Prevents problems if do IMult A, A, A
  A1 = a
  B1 = b
  Result.i = A1.i * B1.r + A1.r * B1.i
  Result.r = A1.r * B1.r - A1.i * B1.i
End Sub

Sub IMRDiv(a As Complex, b As Double, Result As Complex)
'
' Divides A by the scalar value B
'
  Result.r = a.r / b
  Result.i = a.i / b
End Sub

Sub IMRPower(a As Complex, b As Double, Result As Complex)
'
' Does A^B where A in a Complex number and B is Real number
'
Dim APolar As Polar
  Complex2Polar a, APolar
  APolar.z = APolar.z ^ b
  APolar.Th = APolar.Th * b
  Polar2Complex APolar, Result
End Sub

Sub IMRProduct(a As Complex, b As Double, Result As Complex)
'
' Multiplies A by the scalar value B
'
  Result.r = a.r * b
  Result.i = a.i * b
End Sub

Sub IMSqr(a As Complex, Result As Complex)
'
' Takes the square root of a complex number
'
Dim A1 As Polar
  Complex2Polar a, A1
  A1.z = Sqr(A1.z)
  A1.Th = A1.Th / 2
  Polar2Complex A1, Result
End Sub

Sub IMSub(a As Complex, b As Complex, Result As Complex)
'
' Subtracts 2 complex numbers
'
  Result.i = a.i - b.i
  Result.r = a.r - b.r
End Sub

Sub IMTest()
'
' This procedure tests all the Complex Math functions and outputs results to the Debug Window.
'
Dim a As Complex, b As Complex, C As Complex, D As Complex, E As Complex, P As Polar
  a.r = 3: a.i = 4
  b.r = 5: b.i = -3
  Debug.Print "A", IMFormat(a)
  Debug.Print "B", IMFormat(b)
  Complex2Polar a, P
  Polar2Complex P, D
  Debug.Print "A -> P -> A", IMPFormat(P), IMFormat(D)
  IMInverse a, C
  IMProduct a, C, D
  Debug.Print "Inverse A", IMFormat(C), IMFormat(D)
  IMConjugate a, C
  Debug.Print "Conjugate", IMFormat(C)
  Debug.Print "Magnitude", IMMag(a)
  IMAdd a, b, C
  IMSub C, b, D
  Debug.Print "A+B,-B", IMFormat(C), IMFormat(D)
  IMProduct a, b, C
  IMDiv C, b, D
  Debug.Print "A*B,/B", IMFormat(C), IMFormat(D)
  IMRProduct a, 5, C
  IMRDiv C, 5, D
  Debug.Print "A*5,/5", IMFormat(C), IMFormat(D)
  IMSqr a, C
  IMRPower C, 2, D
  Debug.Print "Sqr(A),^2", IMFormat(C), IMFormat(D)
  IMln a, C
  IMExp C, D
  Debug.Print "Ln A,e^", IMFormat(C), IMFormat(D)
  IMLog10 a, C
  D.r = 10: D.i = 0
  IMPower D, C, E
  Debug.Print "Log10 A,10^", IMFormat(C), IMFormat(E)
  IMLog2 a, C
  D.r = 2: D.i = 0
  IMPower D, C, E
  Debug.Print "Log2 A,2^", IMFormat(C), IMFormat(E)
  IMPower a, b, C
  IMInverse b, D
  IMPower C, D, E
  Debug.Print "A^B,^(1/B)", IMFormat(C), IMFormat(E)
End Sub

Sub Polar2Complex(a As Polar, Result As Complex)
'
' Converts a Polar form into Complex number
'
  Result.r = a.z * Cos(a.Th)
  Result.i = a.z * Sin(a.Th)
End Sub

perove
0
 

Author Comment

by:bigc
ID: 7140505
I'm a mathematician, and those functions scared me!

I can't believe that access doesn't have the ability to convert angles, but I've got enough of an idea from your functions to sort it out myself from here.

Thanks for your impressively speedy answer
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question