C's trig functions

Discussion in 'C Programming' started by John Smith, Apr 26, 2005.

  1. John Smith

    John Smith Guest

    I just broke up with my girlfriend, so, to sublimate my sexual tensions,
    I began reading this:

    http://forensics.calcinfo.com/

    This guy has developed a method that he uses to determine chip lineage
    in old calculators (he needs a girlfriend too). Various chips will
    produce various results when you make this calculation:

    n = sin(cos(tan(atan(acos(asin(9.0)))))) in degrees.

    With the Windows calculator I get the following results starting with
    sin(9.0)in degree mode:

    0.156434465040230869010105319467167
    0.99999627274288502411751620501135
    0.0174549998554886607913941409283485
    0.99999627274288502411751620501135
    0.156434465040230869010105319467167
    9.00000000000000000000000000000003

    I decided to try it with C's trig functions:

    #include <stdio.h>
    #include <math.h>
    #define PI 3.14159265358979323846
    #define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))

    int main(void)
    {
    long double n;

    n = sin(DEG2RAD(9.0));
    printf("%.20Lf\n", n);
    n = cos(DEG2RAD(n));
    printf("%.20Lf\n", n);
    n = tan(DEG2RAD(n));
    printf("%.20Lf\n", n);
    n = atan(DEG2RAD(n));
    printf("%.20Lf\n", n);
    n = acos(DEG2RAD(n));
    printf("%.20Lf\n", n);
    n = asin(DEG2RAD(n));
    printf("%.20Lf\n", n);
    /* final n = sin(cos(tan(atan(acos(asin(9.0)))))) */

    return 0;
    }

    The program produces this output:

    0.15643446504023086901
    0.99999627274288502409
    0.01745499985548866218
    0.00030464720898864997
    1.57079100969804273100
    0.02741891042497898145

    The first three values are OK, but when the inverse functions are
    invoked it goes off the tracks. What's wrong?
    John Smith, Apr 26, 2005
    #1
    1. Advertising

  2. Here's a quick check: Wouldn't the last three calls (atan, acos, asin)
    be DEG2RAD(atan(n)), DEG2RAD(acos(n)), and DEG2RAD(asin(n)) instead of
    the way you have them?

    Happy debugging, Gregory Pietsch
    Gregory Pietsch, Apr 26, 2005
    #2
    1. Advertising

  3. John Smith wrote:
    > I began reading this:
    >
    > http://forensics.calcinfo.com/
    >
    > With the Windows calculator I get the following results starting with


    > sin(9.0)in degree mode:
    >
    > 0.156434465040230869010105319467167
    > 0.99999627274288502411751620501135
    > 0.0174549998554886607913941409283485
    > 0.99999627274288502411751620501135
    > 0.156434465040230869010105319467167
    > 9.00000000000000000000000000000003
    >
    > I decided to try it with C's trig functions:
    >
    > #include <stdio.h>
    > #include <math.h>
    > #define PI 3.14159265358979323846
    > #define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))
    >
    > int main(void)
    > {
    > long double n;
    >
    > n = sin(DEG2RAD(9.0));
    > printf("%.20Lf\n", n);
    > n = cos(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = tan(DEG2RAD(n));
    > printf("%.20Lf\n", n);


    Fine, to here.

    > n = atan(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = acos(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = asin(DEG2RAD(n));
    > printf("%.20Lf\n", n);


    Try...

    #define RAD2DEG(RAD) ((RAD)*180/PI)

    n = RAD2DEG(atan(n));
    printf("%.20Lf\n", n);
    n = RAD2DEG(acos(n));
    printf("%.20Lf\n", n);
    n = RAD2DEG(asin(n));
    printf("%.20Lf\n", n);

    > /* final n = sin(cos(tan(atan(acos(asin(9.0)))))) */
    >
    > return 0;
    > }


    --
    Peter
    Peter Nilsson, Apr 26, 2005
    #3
  4. John Smith <> writes:
    > I just broke up with my girlfriend, so, to sublimate my sexual
    > tensions, I began reading this:
    >
    > http://forensics.calcinfo.com/
    >
    > This guy has developed a method that he uses to determine chip lineage
    > in old calculators (he needs a girlfriend too). Various chips will
    > produce various results when you make this calculation:
    >
    > n = sin(cos(tan(atan(acos(asin(9.0)))))) in degrees.


    What do you expect asin(9.0) to do? asin() expects an argument in the
    range -1.0 to +1.0, and returns an angle (normally in radians); it
    fails if its argument is outside that range. Converting asin()'s
    argument from degrees to radians makes no mathematical sense; if you
    want to work in degrees, you need to convert the *result* from radians
    to degrees.

    > With the Windows calculator I get the following results starting with
    > sin(9.0)in degree mode:

    [...]

    Starting with sin(9.0) is inconsistent with the expression above. I
    suspect what you mean is:

    n = asin(acos(atan(tan(cos(sin(9.0))))))

    (where 9.0 is in degrees).

    As you know, unlike most interactive calculators, C's trig functions
    have no "degree mode", which is why you need to do the conversions
    manually. The C equivalent of sin(x), where x is expressed in
    degrees, would be sin(DEG2RAD(x)). The C equivalent of asin(x), where
    the result is to be expressed in degrees, would be RAD2DEG(asin(x)).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 26, 2005
    #4
  5. John Smith wrote the demented claim that:
    > #define PI 3.14159265358979323846
    > #define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))
    >
    > int main(void)
    > {
    > long double n;
    >
    > n = sin(DEG2RAD(9.0));
    > printf("%.20Lf\n", n);
    > n = cos(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = tan(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = atan(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = acos(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > n = asin(DEG2RAD(n));
    > printf("%.20Lf\n", n);
    > /* final n = sin(cos(tan(atan(acos(asin(9.0)))))) */

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Do you really thing that
    asin(acos(atan(tan(cos(sin(x)))))
    is the same operationally as
    sin(cos(tan(atan(acos(asin(x)))))?
    Martin Ambuhl, Apr 26, 2005
    #5
  6. John Smith

    Thomas Guest

    ??,
    "Keith Thompson" <> ???? news:...
    > John Smith <> writes:
    > > I just broke up with my girlfriend, so, to sublimate my sexual
    > > tensions, I began reading this:
    > >
    > > http://forensics.calcinfo.com/
    > >
    > > This guy has developed a method that he uses to determine chip lineage
    > > in old calculators (he needs a girlfriend too). Various chips will
    > > produce various results when you make this calculation:
    > >
    > > n = sin(cos(tan(atan(acos(asin(9.0)))))) in degrees.

    >
    > What do you expect asin(9.0) to do? asin() expects an argument in the
    > range -1.0 to +1.0, and returns an angle (normally in radians); it
    > fails if its argument is outside that range. Converting asin()'s
    > argument from degrees to radians makes no mathematical sense; if you
    > want to work in degrees, you need to convert the *result* from radians
    > to degrees.
    >
    > > With the Windows calculator I get the following results starting with
    > > sin(9.0)in degree mode:

    > [...]
    >
    > Starting with sin(9.0) is inconsistent with the expression above. I
    > suspect what you mean is:
    >
    > n = asin(acos(atan(tan(cos(sin(9.0))))))
    >
    > (where 9.0 is in degrees).
    >
    > As you know, unlike most interactive calculators, C's trig functions
    > have no "degree mode", which is why you need to do the conversions
    > manually. The C equivalent of sin(x), where x is expressed in
    > degrees, would be sin(DEG2RAD(x)). The C equivalent of asin(x), where
    > the result is to be expressed in degrees, would be RAD2DEG(asin(x)).
    >
    > --
    > Keith Thompson (The_Other_Keith)

    <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*>

    <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.
    Thomas, Apr 26, 2005
    #6
  7. "Thomas" <> writes:
    > ??,
    > "Keith Thompson" <> ???? news:...

    [snip]

    You wrote two question marks and then quoted my entire article.

    Did you have a question?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 26, 2005
    #7
  8. John Smith

    John Smith Guest

    Keith Thompson wrote:

    > Starting with sin(9.0) is inconsistent with the expression above. I
    > suspect what you mean is:
    >
    > n = asin(acos(atan(tan(cos(sin(9.0))))))


    About two minutes after I posted this, I realized I had written it
    backwards.
    >
    > (where 9.0 is in degrees).
    >
    > As you know, unlike most interactive calculators, C's trig functions
    > have no "degree mode", which is why you need to do the conversions
    > manually. The C equivalent of sin(x), where x is expressed in
    > degrees, would be sin(DEG2RAD(x)). The C equivalent of asin(x), where
    > the result is to be expressed in degrees, would be RAD2DEG(asin(x)).
    >


    I hate to look stupid, but thanks to you and others who pointed out my
    mistakes.

    JS
    John Smith, Apr 26, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF1bA==?=

    SortExpression makes SortCommand event not to trig

    =?Utf-8?B?UGF1bA==?=, Sep 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    =?Utf-8?B?UGF1bA==?=
    Sep 22, 2005
  2. Cary West

    Python trig precision problem

    Cary West, May 18, 2006, in forum: Python
    Replies:
    4
    Views:
    836
    Robert Kern
    May 19, 2006
  3. Mark Healey
    Replies:
    7
    Views:
    1,479
    Tim Prince
    May 22, 2006
  4. kwikius
    Replies:
    1
    Views:
    389
    Default User
    May 22, 2008
  5. kwikius
    Replies:
    1
    Views:
    347
    Default User
    May 22, 2008
Loading...

Share This Page