Sponsor for PC Pals Forum

Author Topic: Math.Cos  (Read 3561 times)

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Math.Cos
« on: November 15, 2004, 23:38 »
The command Math.Cos (in VB.NET) doesn't seem to be working for me. I have:

XCompA = FormatNumber(Math.Cos(AngleA), 2) * A

The number im using (AngleA) is 62.4. It keeps showing up as 0.908191697953518. But from using my calculator, i get 0.46329603511986173188205964738969.

XCompA and AngleA are declared as Doubles. A is another number (24.1).

My main problem is that I can't get the cosine of 62.4 to work properly....
l33t h@x0r... sort of...

Offline Dack

  • Established Member
  • ****
  • Posts: 831
Re:Math.Cos
« Reply #1 on: November 15, 2004, 23:52 »
Suggest you might want to check what format the Cos function requires ;)

Bit of a difference between radians and degrees :whistle:
hey promised the earth! Then delivered mud.
Technically it did meet the spec.

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Re:Math.Cos
« Reply #2 on: November 16, 2004, 03:26 »
Ah!

lol, can u help me out a bit more? I've been lookin' for some examples on the net but I can't seem to find any. The one's I do find don't make any sense neways (to me at least...).
l33t h@x0r... sort of...

Offline Sandra

  • Ultimate Member
  • *******
  • Posts: 12155
Re:Math.Cos
« Reply #3 on: November 16, 2004, 04:03 »
I dont know anything about VB stuff but if its standard trig then that should be fairly straight forward.
I cant find a table for doing minutes as well as degrees, only whole degrees  :(

http://www.sosmath.com/tables/trigtable/trigtable.html

Cos 62 degrees is 0.46947 so 62.4 could well be 0.46329603511986173188205964738969 as you say you are getting on your calculator.

Dont know if this helps you or not  ???

Offline Dack

  • Established Member
  • ****
  • Posts: 831
Re:Math.Cos
« Reply #4 on: November 16, 2004, 19:25 »
XCompA = FormatNumber(Math.Cos(AngleA), 2) * A

would need to have a conversion of the AngleA value into radians i.e. divide by 180 and then multiply by PI

CONST PI = 3.14159265358979
XCompA = FormatNumber(Math.Cos((AngleA/180.0)*PI), 2) * A
hey promised the earth! Then delivered mud.
Technically it did meet the spec.

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Re:Math.Cos
« Reply #5 on: November 16, 2004, 21:33 »
Wow, excellent! Thank you!

You are a God when it comes to programming! ;) :D
l33t h@x0r... sort of...

Offline Dack

  • Established Member
  • ****
  • Posts: 831
Re:Math.Cos
« Reply #6 on: November 16, 2004, 21:38 »
Well people have looked at some of the code I've written and said something similar.

(I think it was "God almighty, who wrote that!" ;) )

Though if you saw what I'm currently writing the code would scare you :) (does pointers to vectors of pointers to classes mean anything to you?) - currently running at about 20,000 lines of code, and they've just changed the interface spec to it.
hey promised the earth! Then delivered mud.
Technically it did meet the spec.

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Re:Math.Cos
« Reply #7 on: November 16, 2004, 22:12 »
Quote
(does pointers to vectors of pointers to classes mean anything to you?)


Nope, lol. We just learned about "Cases" today in our VB lecture, and "If" statements in our lecture before that. I already know how to do all that stuff though so I was very bored. I think I've heard of vectors b4, but I don't quite remember...


**Since ur online now, is there any way to do "tan ^ -1"?
l33t h@x0r... sort of...

Offline Dack

  • Established Member
  • ****
  • Posts: 831
Re:Math.Cos
« Reply #8 on: November 16, 2004, 23:21 »
Vectors are part of the STL in C++.

If you mean arctan then you could use the ATN function :)

More info on useful algorithms over on:
http://www.vb-helper.com/index_algorithms.html
hey promised the earth! Then delivered mud.
Technically it did meet the spec.

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Re:Math.Cos
« Reply #9 on: November 17, 2004, 01:48 »
Ah, I don't know much about C++...I tried to learn awhile ago, but it was too complicated for me at the time.

I think "Atn" is for VB6. I tried "Math.Atan" which I assumed was the same thing for VB.NET, but I can't seem to get it working. I'm trying to get the angle of 5.364556962.

I have:

Alpha = FormatNumber(Math.Atan(Theta), 2)

I dunno whats wrong with it....on my calculator I just punch in the number, then push shift and the tan button. That would make it tan-1.
l33t h@x0r... sort of...

Offline Dack

  • Established Member
  • ****
  • Posts: 831
Re:Math.Cos
« Reply #10 on: November 17, 2004, 09:39 »
Right idea, using the System.Math.Atan function, but did you remember to convert the returned angle from radians to degrees again?

i.e. multiply by 180 and divide by PI?
hey promised the earth! Then delivered mud.
Technically it did meet the spec.

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Re:Math.Cos
« Reply #11 on: November 17, 2004, 22:16 »
I have no clue, lol....I added in the * 180 / PI, but it doesn't seem to work. Maybe its just me.

This is what I have as my code:

Public Class Form1
    Inherits System.Windows.Forms.Form

' General Declarations
Dim Alpha, A, B, C, R, Tan, Theta, AngleA, AngleB, AngleC, XCompA, XCompB, XCompC, YCompA, YCompB, YCompC, XCompTotal, YCompTotal As Double

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Clear the Listbox
        lstOutput.Items.Clear()

        ' Declarations

        Dim fmtStr As String = "{0, 0} {1, 14} {2, 15}"
        ' Declare PI
        Const PI = 3.14159265358979

        ' Add text to Variables
        A = CDbl(Val(txtA.Text))
        B = CDbl(Val(txtB.Text))
        C = CDbl(Val(txtC.Text))
        AngleA = CDbl(Val(txtAngleA.Text))
        AngleB = CDbl(Val(txtAngleB.Text))
        AngleC = CDbl(Val(txtAngleC.Text))

        ' Add titles to Chart/Listbox
        lstOutput.Items.Add(String.Format("{0, 0} {1, 15} {2, 15}", "", "X Comp.", "Y Comp."))

        ' Find Cosine of Angle A...multiply by A
        XCompA = FormatNumber(Math.Cos((AngleA / 180.0) * PI), 2) * A
        ' Find Sine of Angle A...multiply by A
        YCompA = FormatNumber(Math.Sin((AngleA / 180.0) * PI), 2) * A

        ' Find Cosine of Angle B...multiply by B
        XCompB = FormatNumber(Math.Cos((AngleB / 180.0) * PI), 2) * B
        ' Find Sine of Angle B...multiply by B
        YCompB = FormatNumber(Math.Sin((AngleB / 180.0) * PI), 2) * B

        ' Find Cosine of Angle C...multiply by C
        XCompC = FormatNumber(Math.Cos((AngleC / 180.0) * PI), 2) * C
        ' Find Sine of Angle C...multiply by C
        YCompC = FormatNumber(Math.Sin((AngleC / 180.0) * PI), 2) * C

        ' Add all XComp and YComp's together into more variables
        XCompTotal = FormatNumber(XCompA + XCompB + XCompC, 2)
        YCompTotal = FormatNumber(YCompA + YCompB + YCompC, 2)

        ' Find "R"
        R = FormatNumber(Math.Sqrt((XCompTotal ^ 2) + (YCompTotal ^ 2)), 2)

        ' Find "Tan"
        Tan = FormatNumber((YCompTotal / XCompTotal), 2)

        ' Find "Theta"
        Theta = FormatNumber(Math.Tan((Tan * 180.0) / PI), 2)

        ' Find Alpha
        Alpha = FormatNumber(Math.Atan((Theta * 180.0) / PI), 2)

        ' Add each answer to the Listbox
        With lstOutput.Items

            .Add(String.Format(fmtStr, "", XCompA, YCompA))
            .Add(String.Format(fmtStr, "", XCompB, YCompB))
            .Add(String.Format(fmtStr, "", XCompC, YCompC))
            .Add(String.Format("{0, 5} {1, 8} {2, 15}", "Total:", XCompTotal, YCompTotal))
            .Add(String.Format(""))
            .Add(String.Format("{0, 5} {1, 0} {2, 11}", "R =", "", R))
            .Add(String.Format("{0, 0} {1, 0} {2, 1}", "Tan Alpha =", "", Tan))
            .Add(String.Format("{0, 5} {1, 0} {2, 8}", "Alpha =", "", Alpha))
            .Add(String.Format("{0, 5} {1, 2} {2, 4}", "Theta R =", "", Theta))
            .Add(String.Format(""))
            .Add(String.Format("{0, 5} {1, 0} {2, 2}", "R =", R & "N", Alpha & " Degrees"))

        End With
    End Sub
End Class
l33t h@x0r... sort of...

Offline Hiatus

  • Regular Member
  • **
  • Posts: 193
    • JoshSalverda.com
Re:Math.Cos
« Reply #12 on: November 18, 2004, 02:18 »
Ah, excellent...I got it sorted out!  ;D

The proper format was:

Alpha = FormatNumber((Math.Atan(Tan) * 180.0 / PI), 2)

I messed up on a couple things, but its all good now. Thanx for ur help Dack!
l33t h@x0r... sort of...

Offline Dack

  • Established Member
  • ****
  • Posts: 831
Re:Math.Cos
« Reply #13 on: November 18, 2004, 19:42 »
I did wonder why you were taking the tan of a tangent :)

BTW you should use math.PI instead of defining PI - it was just a quick way of doing it.
hey promised the earth! Then delivered mud.
Technically it did meet the spec.


Show unread posts since last visit.
Sponsor for PC Pals Forum