473,378 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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",cos(Pi));
return;
}

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

thanks everyone :)
Nov 11 '08 #1
14 1776
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
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
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
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
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
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
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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mullin Yu | last post by:
hi, i have a web service that has file operations on Windows OS, and there may be a file concurrency issue if only one working directory e.g. c:\working therefore, i want to have a unique sub...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
5
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
8
by: jojobar | last post by:
Okay, I am trying to do is to test the webresource in 2.0 1. I created a new project with assembly name (and default assembly name) "Office". 2. I added the following to the AssemblyInfo.cs...
2
by: Don | last post by:
I'm having problems with intellisense, autocomplete, etc. suddenly not working in certain classes of a project I'm working on. All the options are set, and it all works fine for most classes, but...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
4
by: qbproger | last post by:
I'm developing a plugin for some software. The previous version of the software didn't require a start in directory to be set. This allowed me to leave the working directory to the default in the...
3
by: Jason Huang | last post by:
Hi, In our C# Windows Form application, we are using the SQL Server 2000 as the database server. The Database table MyTable has a field RegistrationDate which represents the Date a client comes...
0
by: WORKING IN FAITH | last post by:
three years I LOVE You Monica More options 1 message - Collapse all WORKING IN FAITH View profile More options Nov 13, 11:29 am three years I LOVE You Monica
3
by: lds | last post by:
On our server we have both applications that have been migrated to use v2.0 of the framework as well as apps that have not yet been migrated and still use 1.1. When I tried to deploy my v2.0 app...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.