cos not working

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

    cos not working

    HI

    I'm newby with C and this is not working. we all know that cos Pi is -1
    but answer here is 1.000000 !!!! i use MS compile so please help!!!

    extern void main();
    extern int printf();
    extern double cos();
    void main(){
    #define Pi 22/7
    printf("%lf",co s(Pi));
    return;
    }

    answer
    1.000000 <--- not right!!!!!

    thanks everyone :)
  • Ian Collins

    #2
    Re: cos not working

    Miles Sorenson wrote:
    HI
    >
    I'm newby with C and this is not working. we all know that cos Pi is -1
    but answer here is 1.000000 !!!! i use MS compile so please help!!!
    >
    extern void main();
    extern int printf();
    extern double cos();
    What are all those there for?

    Let's try again, shall we?

    #include <math.h>
    #include <stdio.h>

    int main(void)
    {
    const double pi = 22.0/7;

    printf("%lf\n", cos(pi));

    return 0;
    }

    c99 -lm test.c

    result:

    -0.999999

    --
    Ian Collins

    Comment

    • Richard Tobin

      #3
      Re: cos not working

      In article <Ra8Sk.5938$vD2 .3045@newsfe01. iad>,
      Miles Sorenson <msorenson44599 9@aol.comwrote:
      >extern int printf();
      >extern double cos();
      Why are you declaring these functions yourself? Doing so is extremely
      error prone. Include the standard headers instead.
      >#define Pi 22/7
      22/7 is 3. Is that what you want?
      >printf("%lf",c os(Pi));
      What kind of argument does cos() take? A double. But you're
      passing it an integer. Because of your poor declaration, which
      isn't a prototype, it won't be converted to a double, so you are
      passing complete rubbish to the function.

      -- Richard
      --
      Please remember to mention me / in tapes you leave behind.

      Comment

      • pete

        #4
        Re: cos not working

        Ian Collins wrote:
        Miles Sorenson wrote:
        >HI
        >>
        >I'm newby with C and this is not working. we all know that cos Pi is -1
        >but answer here is 1.000000 !!!! i use MS compile so please help!!!
        >>
        >extern void main();
        >extern int printf();
        >extern double cos();
        >
        What are all those there for?
        >
        Let's try again, shall we?
        >
        #include <math.h>
        #include <stdio.h>
        >
        int main(void)
        {
        const double pi = 22.0/7;
        >
        printf("%lf\n", cos(pi));
        >
        return 0;
        }
        >
        c99 -lm test.c
        >
        result:
        >
        -0.999999

        %f is for outputting type double values in both C89 and C99.

        The letter 'l' is allowed, but does nothing, in "%lf"
        as a printf format specifer in C99.

        The %lf makes that program undefined in C89.

        --
        pete

        Comment

        • Eric Sosman

          #5
          Re: cos not working

          Miles Sorenson wrote:
          HI
          >
          I'm newby with C and this is not working. we all know that cos Pi is -1
          but answer here is 1.000000 !!!! i use MS compile so please help!!!
          >
          extern void main();
          Not right.
          extern int printf();
          Not right.
          extern double cos();
          Not right.
          void main(){
          Not right.
          #define Pi 22/7
          Not right.
          printf("%lf",co s(Pi));
          Not right, not right (two errors).
          return;
          Not right.
          }
          Right! Congratulations !
          answer
          1.000000 <--- not right!!!!!
          In light of all the earlier errors, why be surprised?
          thanks everyone :)
          You're welcome, but only sort of. If you would *read*
          your C textbook instead of just using it as a drinks coaster,
          your welcome would grow warmer.

          --
          Eric Sosman
          esosman@ieee-dot-org.invalid

          Comment

          • Wolfgang Draxinger

            #6
            Re: cos not working

            Miles Sorenson wrote:
            #define Pi 22/7
            You know, that there's a pi constant in math.h? At least there
            should be and most compilers come with it. It's called M_PI
            usuallay.

            Wolfgang Draxinger
            --
            E-Mail address works, Jabber: hexarith@jabber .org, ICQ: 134682867

            Comment

            • pete

              #7
              Re: cos not working

              Wolfgang Draxinger wrote:
              Miles Sorenson wrote:
              >
              >#define Pi 22/7
              >
              You know, that there's a pi constant in math.h? At least there
              should be and most compilers come with it. It's called M_PI
              usuallay.
              I use

              #include <math.h>
              (4 * atan(1))

              --
              pete

              Comment

              • Tim Prince

                #8
                Re: cos not working

                Wolfgang Draxinger wrote:
                Miles Sorenson wrote:
                >
                >#define Pi 22/7
                >
                You know, that there's a pi constant in math.h? At least there
                should be and most compilers come with it. It's called M_PI
                usuallay.
                >
                And, it's usually guarded by something like

                #if defined __USE_BSD || defined __USE_XOPEN

                so as to make clear that it's not standard C, so irrevelant to the
                discussion about errors made under standard C.
                I believe the idea of rounding off the ratio of circumference to diameter
                to 3 has biblical support, not a crucial topic here.

                Comment

                • osmium

                  #9
                  Re: cos not working

                  "Miles Sorenson" wrote:
                  I'm newby with C and this is not working. we all know that cos Pi is -1
                  but answer here is 1.000000 !!!! i use MS compile so please help!!!
                  >
                  extern void main();
                  extern int printf();
                  extern double cos();
                  void main(){
                  #define Pi 22/7
                  That only works in Indiana.



                  <snip>


                  Comment

                  • James Kuyper

                    #10
                    Re: cos not working

                    Wolfgang Draxinger wrote:
                    Miles Sorenson wrote:
                    >
                    >#define Pi 22/7
                    >
                    You know, that there's a pi constant in math.h? At least there
                    should be and most compilers come with it. It's called M_PI
                    usuallay.
                    No implementation of C that is invoked in a mode that fully conforms to
                    the standard can #define M_PI in <math.hwhen translating strictly
                    conforming C code. However, on many compilers there's some way to make
                    this happen, either by use of compiler options that render the
                    implementation non-conforming, or by inserting something into the user
                    code that makes it no longer strictly conforming.

                    I did a quick search found one source that claims that M_PI is a
                    BSD/Unix innovation that was never adopted into either the C or POSIX
                    standards. I can't vouch for the accuracy of that claim, but it seems
                    plausible.

                    Comment

                    • Wolfgang Draxinger

                      #11
                      Re: cos not working

                      Tim Prince wrote:
                      And, it's usually guarded by something like
                      >
                      #if defined __USE_BSD || defined __USE_XOPEN
                      >
                      so as to make clear that it's not standard C, so irrevelant to
                      the discussion about errors made under standard C.
                      If in the standard or not, one should always write down
                      transzendental constants like pi or e with as much digits that
                      can be represented on the architecture.
                      I believe the idea of rounding off the ratio of circumference
                      to diameter to 3 has biblical support, not a crucial topic
                      here.
                      Yeah, but the OP intended to use 22./7. which is quite a good
                      approximation of PI for simple things, but not nearly good
                      enough if you want to do things like calculate satellite orbits.

                      3.1415926535897 932384626433832 795029L

                      is however good enough to do calculations on lengths of the
                      universe's size, and it takes exactly the same amount of memory
                      like 22./7. (in the final binary).

                      Wolfgang Draxinger
                      --
                      E-Mail address works, Jabber: hexarith@jabber .org, ICQ: 134682867

                      Comment

                      • Phil Carmody

                        #12
                        Re: cos not working

                        Miles Sorenson <msorenson44599 9@aol.comwrites :
                        HI
                        Hi Han from China.

                        [SNIP - obvious troll]

                        Congrats - that should hook a fair proportion of the less-prepared!

                        Phil
                        --
                        I tried the Vista speech recognition by running the tutorial. I was
                        amazed, it was awesome, recognised every word I said. Then I said the
                        wrong word ... and it typed the right one. It was actually just
                        detecting a sound and printing the expected word! -- pbhj on /.

                        Comment

                        • Sjouke Burry

                          #13
                          Re: cos not working

                          James Kuyper wrote:
                          Wolfgang Draxinger wrote:
                          >Miles Sorenson wrote:
                          >>
                          >>#define Pi 22/7
                          >You know, that there's a pi constant in math.h? At least there
                          >should be and most compilers come with it. It's called M_PI
                          >usuallay.
                          >
                          No implementation of C that is invoked in a mode that fully conforms to
                          the standard can #define M_PI in <math.hwhen translating strictly
                          conforming C code. However, on many compilers there's some way to make
                          this happen, either by use of compiler options that render the
                          implementation non-conforming, or by inserting something into the user
                          code that makes it no longer strictly conforming.
                          >
                          I did a quick search found one source that claims that M_PI is a
                          BSD/Unix innovation that was never adopted into either the C or POSIX
                          standards. I can't vouch for the accuracy of that claim, but it seems
                          plausible.
                          Of five compilers I have, only BC5 has M_PI.

                          Comment

                          • user923005

                            #14
                            Re: cos not working

                            On Nov 11, 5:40 am, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
                            Miles Sorenson wrote:
                            HI
                            >
                            I'm newby with C and this is not working. we all know that cos Pi is -1
                            but answer here is 1.000000 !!!! i use MS compile so please help!!!
                            >
                            extern void main();
                            >
                                 Not right.
                            >
                            extern int printf();
                            >
                                 Not right.
                            >
                            extern double cos();
                            >
                                 Not right.
                            >
                            void main(){
                            >
                                 Not right.
                            >
                            #define Pi 22/7
                            >
                                 Not right.
                            >
                            printf("%lf",co s(Pi));
                            >
                                 Not right, not right (two errors).
                            >
                            return;
                            >
                                 Not right.
                            >
                            }
                            >
                                 Right!  Congratulations !
                            >
                            answer
                            1.000000 <--- not right!!!!!
                            >
                                 In light of all the earlier errors, why be surprised?
                            >
                            thanks everyone :)
                            >
                                 You're welcome, but only sort of.  If you would *read*
                            your C textbook instead of just using it as a drinks coaster,
                            your welcome would grow warmer.
                            It simply *has* to be a troll.

                            Comment

                            • John Bode

                              #15
                              Re: cos not working

                              On Nov 10, 10:50 pm, Miles Sorenson <msorenson445.. .@aol.comwrote:
                              HI
                              >
                              I'm newby with C and this is not working. we all know that cos Pi is -1
                              but answer here is 1.000000 !!!! i use MS compile so please help!!!
                              >
                              extern void main();
                              extern int printf();
                              extern double cos();
                              Replace the above with the following:

                              #include <stdio.h>
                              #include <math.h>
                              void main(){
                              main() returns int, not void. Change that to

                              int main(void)
                              #define Pi 22/7
                              Both 22 and 7 are integers, so the result of the division will also be
                              an integer, in this case 3. Either use 22.0/7, 22/7.0, 22.0/7.0, or
                              good old 3.14159.
                              printf("%lf",co s(Pi));
                              "%f" should be good enough.
                              return;
                              >
                              As main() always returns an int, this should be

                              return 0;
                              }
                              >
                              answer
                              1.000000 <--- not right!!!!!
                              >
                              thanks everyone :)
                              #include <stdio.h>
                              #include <math.h>

                              #define PI 3.14159265

                              int main(void)
                              {
                              printf("%f\n", cos(PI));
                              return 0;
                              }

                              Comment

                              Working...