Results 1 to 10 of 10

Thread: [RESOLVED] VB6 facing problems with Sine and Cosine

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    70

    Resolved [RESOLVED] VB6 facing problems with Sine and Cosine

    I was translating some codes from C++ to vb6 and I notice the results output when using sine or cosine is different for instance, sin(0.224654) will yield a result of 0.222769 when it should be 0.0039209. Any idea why?

    Thanks.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VB6 facing problems with Sine and Cosine

    But Sine of 0.224654 is 0.222769070022729
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 facing problems with Sine and Cosine

    Code:
    ?sin(.224654 *3.14159265/180)
     3.92094192660541E-03
    
    ?cdec(sin(.224654 *3.14159265/180))
     0.00392094192660541
    it does not quite match the value using excel worksheet functions
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    70

    Re: VB6 facing problems with Sine and Cosine

    Quote Originally Posted by westconn1 View Post
    Code:
    ?sin(.224654 *3.14159265/180)
     3.92094192660541E-03
    
    ?cdec(sin(.224654 *3.14159265/180))
     0.00392094192660541
    it does not quite match the value using excel worksheet functions
    Sorry I am a little confused here, so which is which?

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VB6 facing problems with Sine and Cosine

    Sin(0.224654) in vb6 and sin(0.224654) in Excel both give me 0.222769070022729
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    70

    Re: VB6 facing problems with Sine and Cosine

    Quote Originally Posted by koolsid View Post
    Sin(0.224654) in vb6 and sin(0.224654) in Excel both give me 0.222769070022729
    Hmm..when I work in C++ it gives me 0.0039209419... same when I use my window xp calculator.

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VB6 facing problems with Sine and Cosine

    Yes that's because what you pass to sin is very imp.

    Sin returns the sine of the given angle(Radians). if 0.224654 is an angle(Radians) then 0.222769070022729 is correct but if 0.224654 is degrees then you need to change it to angle(Radians) and then 0.0039209 is correct...

    1 radian = 57.2957795 degrees

    So

    Code:
    Format(Sin(0.224654 / 57.2957795), "0.0000000") will give you 0.0039209
    Last edited by Siddharth Rout; Mar 8th, 2010 at 04:13 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2009
    Posts
    70

    Re: VB6 facing problems with Sine and Cosine

    Quote Originally Posted by koolsid View Post
    Yes that's because what you pass to sin is very imp.

    Sin returns the sine of the given angle(Radians). if 0.224654 is an angle(Radians) then 0.222769070022729 is correct but if 0.224654 is degrees then you need to change it to angle(Radians) and then 0.0039209 is correct...

    1 radian = 57.2957795 degrees

    So

    Code:
    Format(Sin(0.224654 / 57.2957795), "0.0000000") will give you 0.0039209
    Thanks a lot for the explanation!

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VB6 facing problems with Sine and Cosine

    Kool. If your query is solved then do remember to mark the thread resolved Check my signature on how to mark a thread resolved
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 facing problems with Sine and Cosine

    Sorry I am a little confused here, so which is which?
    the return from sin is a double
    first example
    by type casting to decimal you get the returned value as a decimal value
    both are equal

    sid converted the return to a formatted string, but i prefered to keep as a value
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width