Sine code for ANSI C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • suri

    Sine code for ANSI C

    Hello
    I downloaded glibc and tried looking for the code that implements the
    sine function
    i couldnt find the file.
    i went to the math directory and found math.h.. i guess that needs to be
    included for the sine function. but which .c file implements the
    sine/cosine and other trig fns
    thanks
  • Nudge

    #2
    Re: Sine code for ANSI C

    suri wrote:
    [color=blue]
    > I downloaded glibc and tried looking for the code that implements the
    > sine function
    > i couldnt find the file.
    > i went to the math directory and found math.h.. i guess that needs to be
    > included for the sine function. but which .c file implements the
    > sine/cosine and other trig fns[/color]



    Do you really want an implementation for sin?

    Apparently, you are running an Intel-compatible processor which
    comes with a sin instruction implemented in hardware.

    Comment

    • suri

      #3
      Re: Sine code for ANSI C

      No i knew that faq.
      my question is where is the sine fn imlementation?
      since u say its a native instruction set means that there is no code in
      ansi C for implementing sine.?
      well libm.a must surely have it. but where are the C files?

      Nudge wrote:[color=blue]
      > suri wrote:
      >
      >[color=green]
      >>I downloaded glibc and tried looking for the code that implements the
      >>sine function
      >>i couldnt find the file.
      >>i went to the math directory and found math.h.. i guess that needs to be
      >>included for the sine function. but which .c file implements the
      >>sine/cosine and other trig fns[/color]
      >
      >
      > http://www.eskimo.com/~scs/C-faq/q14.3.html
      >
      > Do you really want an implementation for sin?
      >
      > Apparently, you are running an Intel-compatible processor which
      > comes with a sin instruction implemented in hardware.
      >[/color]

      Comment

      • -wombat-

        #4
        Re: Sine code for ANSI C

        suri wrote:[color=blue]
        > No i knew that faq.
        > my question is where is the sine fn imlementation?
        > since u say its a native instruction set means that there is no code in
        > ansi C for implementing sine.?[/color]

        Some platforms have hardware instructions that compute sin() and the
        compiler will emit them, bypassing the libm library's implementation. This
        is pretty much true for ia32 and ia64.
        [color=blue]
        > well libm.a must surely have it. but where are the C files?[/color]

        For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
        looking code, but it works fast.] You're almost better off grabbing a
        calculus book and having a look at a Taylor series expansion if you truly
        want to understand the math. Or look at "Numerical Recipes in C".

        Comment

        • osmium

          #5
          Re: Sine code for ANSI C


          suri <hsuri@usc.ed u> wrote in message
          news:c73mom$i1u 48$2@ID-233334.news.uni-berlin.de...
          [color=blue]
          > I downloaded glibc and tried looking for the code that implements the
          > sine function
          > i couldnt find the file.
          > i went to the math directory and found math.h.. i guess that needs to be
          > included for the sine function. but which .c file implements the
          > sine/cosine and other trig fns[/color]

          Here is one way, but not the most advanced way. It is fairly suitable for a
          float (precision wise) but not for a double.

          #include <iostream.h>
          #include <iomanip.h>
          #include <math.h>

          // Source AMS 55, eqn 4.3.97. Handbook of Mathematical Functions, Pub by
          U.S. Dept of Commerce
          float sinx(float x)
          {
          static const float a[] =
          {-.1666666664,.00 83333315,-.0001984090,.00 00027526,-.0000000239};
          float xsq = x*x;
          float temp = x*(1 + a[0]*xsq + a[1]*xsq*xsq + a[2]* xsq*xsq*xsq
          +a[3]*xsq*xsq*xsq*xs q
          + a[4]*xsq*xsq*xsq*xs q*xsq);
          return temp;
          }
          // ------------------
          void test()
          {
          float x;
          while(1)
          {
          cin >> x;
          if(x<0. || x > (3.1416/2) )
          {
          cout << "Argument to sinx must be in range 0>= x <= pi/2 \n";
          continue;
          }
          cout << sinx(x) << setw(12) << (float)sin(x) << endl;
          }

          The test code, but not the sinx() code, is in C++ which I suppose will lead
          to some hissy fits since this is a C group. It is also a pre-standard
          version of C++ so I suppose there will be some more hissy fits from that
          quarter. So be it.



          Comment

          • suri

            #6
            Re: Sine code for ANSI C

            I do know the series expansion of sine i was just interested to know how
            its implemented in the ansi C library. like how many terms of the
            infinite series are included.

            I have linux and use glibc. so i could find the file in the path u mentioned

            -wombat- wrote:[color=blue]
            > suri wrote:
            >[color=green]
            >>No i knew that faq.
            >>my question is where is the sine fn imlementation?
            >>since u say its a native instruction set means that there is no code in
            >>ansi C for implementing sine.?[/color]
            >
            >
            > Some platforms have hardware instructions that compute sin() and the
            > compiler will emit them, bypassing the libm library's implementation. This
            > is pretty much true for ia32 and ia64.
            >
            >[color=green]
            >>well libm.a must surely have it. but where are the C files?[/color]
            >
            >
            > For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
            > looking code, but it works fast.] You're almost better off grabbing a
            > calculus book and having a look at a Taylor series expansion if you truly
            > want to understand the math. Or look at "Numerical Recipes in C".[/color]

            Comment

            • suri

              #7
              Re: Sine code for ANSI C

              im sorry i meant i *could not* find the file

              suri wrote:[color=blue]
              > I do know the series expansion of sine i was just interested to know how
              > its implemented in the ansi C library. like how many terms of the
              > infinite series are included.
              >
              > I have linux and use glibc. so i could find the file in the path u
              > mentioned
              >
              > -wombat- wrote:
              >[color=green]
              >> suri wrote:
              >>[color=darkred]
              >>> No i knew that faq.
              >>> my question is where is the sine fn imlementation?
              >>> since u say its a native instruction set means that there is no code in
              >>> ansi C for implementing sine.?[/color]
              >>
              >>
              >>
              >> Some platforms have hardware instructions that compute sin() and the
              >> compiler will emit them, bypassing the libm library's implementation.
              >> This
              >> is pretty much true for ia32 and ia64.
              >>
              >>[color=darkred]
              >>> well libm.a must surely have it. but where are the C files?[/color]
              >>
              >>
              >>
              >> For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
              >> looking code, but it works fast.] You're almost better off grabbing a
              >> calculus book and having a look at a Taylor series expansion if you truly
              >> want to understand the math. Or look at "Numerical Recipes in C".[/color][/color]

              Comment

              • Jack Klein

                #8
                Re: Sine code for ANSI C

                On Sun, 02 May 2004 06:55:43 -0700, suri <hsuri@usc.ed u> wrote in
                comp.lang.c:

                First, please don't top-post. Material you add in a reply goes AFTER
                material you are quoting. Top-posting makes technical discussions
                hard to follow and is considered rude in comp.lang.c.
                [color=blue]
                > I do know the series expansion of sine i was just interested to know how
                > its implemented in the ansi C library. like how many terms of the
                > infinite series are included.[/color]

                There is no such thing as "the" ANSI C library. The C standard
                defines the functions that must be provided, their interface, and
                their results when used as defined. It does not specify an
                implementation at all.

                The standard library functions supplied with an implementation do not
                even have to be written in C.

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++

                Comment

                • Grumble

                  #9
                  Re: Sine code for ANSI C

                  -wombat- wrote:
                  [color=blue]
                  > Some platforms have hardware instructions that compute sin() and
                  > the compiler will emit them, bypassing the libm library's
                  > implementation. This is pretty much true for ia32 and ia64.[/color]

                  <OT>

                  As far as I can tell, IA-64 has no such instruction :-)

                  Comment

                  • Grumble

                    #10
                    Re: Sine code for ANSI C

                    suri wrote:
                    [color=blue]
                    > my question is where is the sine fn imlementation?
                    > since u say its a native instruction set means that there
                    > is no code in ansi C for implementing sine?
                    > well libm.a must surely have it. but where are the C files?[/color]

                    Why don't you ask in a forum dedicated to the GNU libc?

                    You might try:

                    comp.os.linux.q uestions
                    comp.os.linux.d evelopment.apps


                    (Please do not top-post.)

                    Comment

                    • Mark McIntyre

                      #11
                      Re: Sine code for ANSI C

                      On Sun, 02 May 2004 02:41:31 -0700, in comp.lang.c , suri <hsuri@usc.ed u>
                      wrote:
                      [color=blue]
                      >since u say its a native instruction set means that there is no code in
                      >ansi C for implementing sine.?[/color]

                      Quite possibly.
                      [color=blue]
                      >well libm.a must surely have it. but where are the C files?[/color]

                      If its native to the processor, there IS no code to do it. Whether some
                      (offtopic) library has an alternate implementation is offtopic here.

                      But if you want code, then write it yourself. Sin(x) is calculable to a
                      good approximation using a power series.
                      --
                      Mark McIntyre
                      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                      CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


                      ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                      http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                      ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                      Comment

                      • P.J. Plauger

                        #12
                        Re: Sine code for ANSI C

                        "Mark McIntyre" <markmcintyre@s pamcop.net> wrote in message
                        news:2h6c90didj p7a52r7aj05u4ma re07l42m2@4ax.c om...
                        [color=blue]
                        > On Sun, 02 May 2004 02:41:31 -0700, in comp.lang.c , suri <hsuri@usc.ed u>
                        > wrote:
                        >[color=green]
                        > >since u say its a native instruction set means that there is no code in
                        > >ansi C for implementing sine.?[/color]
                        >
                        > Quite possibly.
                        >[color=green]
                        > >well libm.a must surely have it. but where are the C files?[/color]
                        >
                        > If its native to the processor, there IS no code to do it. Whether some
                        > (offtopic) library has an alternate implementation is offtopic here.
                        >
                        > But if you want code, then write it yourself. Sin(x) is calculable to a
                        > good approximation using a power series.[/color]

                        A *good* sine function is one of the hardest math functions to
                        write, believe it or not. For x < |pi| / 4, it it indeed easy
                        to approximate with a polynomial in x^2 -- a truncated power
                        series will do, but you can save a term or two by fiddling the
                        coefficients. But doing proper argument reduction is an open
                        ended exercise in frustration. Just reducing the argument modulo
                        2*pi quickly accumulates errors unless you do arithmetic to
                        many extra bits of precision.

                        Some processors have instructions that do *parts* of the job
                        of computing the sine, like evaluating it in the easy region
                        described above. Some even have functions that appear to do
                        the argument reduction well for you, but they don't always
                        deliver.

                        See my book, The Standard C Library, for one implementation of
                        sin(x) in Standard C. It's only moderately naive.

                        P.J. Plauger
                        Dinkumware, Ltd.



                        Comment

                        • CBFalconer

                          #13
                          Re: Sine code for ANSI C

                          "P.J. Plauger" wrote:[color=blue]
                          >[/color]
                          .... snip ...[color=blue]
                          > coefficients. But doing proper argument reduction is an open
                          > ended exercise in frustration. Just reducing the argument modulo
                          > 2*pi quickly accumulates errors unless you do arithmetic to
                          > many extra bits of precision.[/color]

                          And that problem is inherent. Adding precision bits for the
                          reduction will not help, because the input value doesn't have
                          them. It is the old problem of differences of similar sized
                          quantities.

                          --
                          A: Because it fouls the order in which people normally read text.
                          Q: Why is top-posting such a bad thing?
                          A: Top-posting.
                          Q: What is the most annoying thing on usenet and in e-mail?

                          Comment

                          • osmium

                            #14
                            Re: Sine code for ANSI C

                            CBFalconer writes:
                            [color=blue]
                            > "P.J. Plauger" wrote:[color=green]
                            > >[/color]
                            > ... snip ...[color=green]
                            > > coefficients. But doing proper argument reduction is an open
                            > > ended exercise in frustration. Just reducing the argument modulo
                            > > 2*pi quickly accumulates errors unless you do arithmetic to
                            > > many extra bits of precision.[/color]
                            >
                            > And that problem is inherent. Adding precision bits for the
                            > reduction will not help, because the input value doesn't have
                            > them. It is the old problem of differences of similar sized
                            > quantities.[/color]

                            Huh? If I want the phase of an oscillator after 50,000 radians are you
                            saying that is not computable? Please elaborate.

                            There was a thread hereabouts many months ago on this very subject and AFAIK
                            no one suggested that it was not computable, it just couldn't be done with
                            doubles. And I see no inherent problems.


                            Comment

                            • Rich Gibbs

                              #15
                              Re: Sine code for ANSI C

                              -wombat- said the following, on 05/02/04 18:43:[color=blue]
                              > suri wrote:
                              >[color=green]
                              >>No i knew that faq.
                              >>my question is where is the sine fn imlementation?
                              >>since u say its a native instruction set means that there is no code in
                              >>ansi C for implementing sine.?[/color]
                              >
                              >[/color]
                              [snip][color=blue]
                              >
                              > For FreeBSD, check out /usr/src/lib/libm/common/{trig.h,sincos. c} [nasty
                              > looking code, but it works fast.] You're almost better off grabbing a
                              > calculus book and having a look at a Taylor series expansion if you truly
                              > want to understand the math. Or look at "Numerical Recipes in C".[/color]

                              There are (at least) two possible questions that might lie behind the
                              OP's request. One, which is more or less on-topic, is "How might one
                              implement the sin() function of the C library in standard C?"

                              The other is something like, "What is the best numerical method for
                              calculating the sine function?" The answer to THAT question has
                              something to do with C, if that is the implementation language, but has
                              more to do with numerical analysis. Wombat has suggested _Numerical
                              Recipes in C_, which is good. The elementary N/A text I used way back
                              when was F.S. Acton's _Numerical Methods That [Usually] Work_. (BTW,
                              calculating function like this by straightforward application of the
                              "textbook" power series is often not the best approach.)

                              I'd also recommend P.J. Plauger's excellent book, _The Standard C
                              Library_, which includes consideration of both questions.

                              --
                              Rich Gibbs
                              rgibbs AT alumni DOT princeton DOT edu

                              Comment

                              Working...