By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

cos not working

P: n/a
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",cos(Pi));
return;
}

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

thanks everyone :)
Nov 11 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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
Nov 11 '08 #2

P: n/a
In article <Ra*****************@newsfe01.iad>,
Miles Sorenson <ms*************@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",cos(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.
Nov 11 '08 #3

P: n/a
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
Nov 11 '08 #4

P: n/a
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",cos(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
es*****@ieee-dot-org.invalid
Nov 11 '08 #5

P: n/a
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: he******@jabber.org, ICQ: 134682867

Nov 11 '08 #6

P: n/a
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
Nov 11 '08 #7

P: n/a
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.
Nov 11 '08 #8

P: n/a
"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.

http://en.wikipedia.org/wiki/Indiana_Pi_Bill

<snip>
Nov 11 '08 #9

P: n/a
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.
Nov 11 '08 #10

P: n/a
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.1415926535897932384626433832795029L

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: he******@jabber.org, ICQ: 134682867

Nov 11 '08 #11

P: n/a
Miles Sorenson <ms*************@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 /.
Nov 11 '08 #12

P: n/a
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.
Nov 11 '08 #13

P: n/a
On Nov 11, 5:40*am, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
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",cos(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.
Nov 11 '08 #14

P: n/a
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",cos(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;
}

Nov 11 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.