Thread: tan() and sin()

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    tan() and sin()

    I'm still develloping my calculator
    But, there is another problem:

    sin(number1);
    tan(number1);

    do not give the same output as my other calculator (The one which I take to school)!

    No error, and I included math.h.

    What am I doing wrong?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Feeding degrees where radians are expected?

    Show an example of expected input and output.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    math.h functions take parameters measured in radians, not degrees.
    There are 2*PI radians in a full circle, scale your angles appropriately.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    sin(1); in computer: 0.841471
    sin(1); in school calculator: 0.017452406

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Ideswa
    sin(1); in computer: 0.841471
    sin(1); in school calculator: 0.017452406
    Like the others have said, you're mixing radians and degrees. Both of those are correct.
    sin(1 degree) = .0174524
    sin(1 radian) = .8414709

    Radians and degrees are sort of like feet and meters. Just different way of measuring, for the most part. 2 * pi radians = 360 deg, as Salem said, so: pi radians = 180 degrees, pi / 2 radians = 90 deg, etc. Google it. See MathWorld's article on it, or perhaps the Wikipedia entry.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Thanks!
    this is my solution:

    tangens = tan(number1);
    answer = (2 * M_PI) * tangens;
    aswer /= 360;
    answer /= tangens;

    http://www.functionx.com/bcb/math/PI.htm says:
    "....Borland had included its value in the math.h library as M_PI 3.14159265358979323846."

    But when I

    cout << M_PI;

    the output is:
    3.14159

    And because of that there is still a little difference between the answers:
    0.017455064 (in school calc.)
    0.0174533 (in my calc.)

    Can I change that configuration or is that impossible with a Borland compiler?

  7. #7
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    cout will use formatting rules for output which concatenate the number.

    The Windows calculator will give you .017452406

    Which just goes to show that you shouldn't trust default math libraries for extreme precision.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I'm pretty sure your school calculator's answer of 0.017455064 is off the mark, because I have two calculators that give 1.7452406437, which agrees with the Windows calc. Your calculator's "answer" of 0.0174533 is an approximation of pi/180.

    And it looks like that's what you're doing, too.
    Code:
    answer = (2 * M_PI) * tangens;
    aswer /= 360;
    answer /= tangens;
    What is this supposed to do? This is equivalent to
    Code:
    answer = (2 * M_PI) * tangens / 360 / tangens
    which means that you're merely finding a convoluted way of assigning pi/180 to answer.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Ideswa
    tangens = tan(number1);
    answer = (2 * M_PI) * tangens;
    aswer /= 360;
    answer /= tangens;
    This is completely wrong (as has been said).

    The reason it almost works is that
    tan(1 degree) = tan( pi/180 radians) ~= pi/180.

    For small angles (in radians), tan(x)~=sin(x)~=x. This approximation is often used in mechanics.


    EDIT: Approximations of tan(1 degree):
    Windows calc: 0.017455064928217585765128895219728
    MATLAB: 0.01745506492822
    Last edited by Sang-drax; 02-04-2006 at 10:37 AM.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Can I change that configuration or is that impossible with a Borland compiler?
    You can #undef PI * and then redefine PI:
    Code:
    #undef PI
    #define PI 3.1415926535897932384626433832795028841971693993751\
    05820974944592307816406286208998628034825342117067\
    98214808651328230664709384460955058223172535940812\
    84811174502841027019385211055596446229489549303819\
    64428810975665933446128475648233786783165271201909\
    14564856692346034861045432664821339360726024914127\
    37245870066063155881748815209209628292540917153643\
    67892590360011330530548820466521384146951941511609\
    43305727036575959195309218611738193261179310511854\
    80744623799627495673518857527248912279381830119491\
    * You may not have to #undef PI, but I would put it in anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    dwks, but the double can hold only so much.
    so even if you gave one million digits the resultant number would be rounded?

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dwks
    You can #undef PI * and then redefine PI:
    Code:
    #undef PI
    #define PI 3.1415926535897932384626433832795028841971693993751\
    05820974944592307816406286208998628034825342117067\
    98214808651328230664709384460955058223172535940812\
    84811174502841027019385211055596446229489549303819\
    64428810975665933446128475648233786783165271201909\
    14564856692346034861045432664821339360726024914127\
    37245870066063155881748815209209628292540917153643\
    67892590360011330530548820466521384146951941511609\
    43305727036575959195309218611738193261179310511854\
    80744623799627495673518857527248912279381830119491\
    * You may not have to #undef PI, but I would put it in anyway.
    That's a complete waste of time. M_PI is already defined with as many digits as can be stored in a double, so you're longer digit sequence is not going to add anything at all. Besides, theres probably more inaccuracy in the sin function, than in your digit sequence above.

    Just use this:
    inline double sin_degrees(double x)
    {
    return sin(x * (M_PI / 180.0));
    }

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Technically, M_PI is non-portable. While it's true several compilers (or their libraries) support it, there is no requirement in the C++ standard that it be supported.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating function of sin cos and tan
    By Niz in forum C Programming
    Replies: 12
    Last Post: 01-09-2007, 05:55 PM
  2. calculating sin, cos, and tangent function
    By rcastel2 in forum C++ Programming
    Replies: 12
    Last Post: 02-23-2006, 01:32 PM
  3. Calculator v2.2.0
    By Quantrizi in forum Game Programming
    Replies: 7
    Last Post: 10-08-2002, 04:29 PM
  4. need help to logically lay out program
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 04-07-2002, 10:11 PM