Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
can someone pls help me with the syntax.
I need to use math.h for Pi but I do not understand how to use it. the net says a lot of things like saying use cmath but others say math.h

they are also a few errors concering the syntax in the statememts under if and else.

C#
#include<iostream>
#include<math.h>
using namespace std;

int main(){

    int  iTerm;
    int iSum=0,iDenom=1,iError;
    float M_PI;
/*
cout<<"| Term | Value | Error " ;*/


for(iTerm=0;iTerm<100;iTerm++){
if(iTerm%2==1)
cout<<"|"<<iTerm<<"|" <<iSum= iSum + (4/iDenom)<<"|" <<iError=iSum - M_PI<< "|" endl;
else
cout<<"|"<<iTerm<<"|" <<iSum= iSum - (4/iDenom)<< "|" <<iError=iSum - M_PI<<"|" endl;


iDenom = iDenom + 2;


}
}
Posted
Comments
Reuben Cabrera 2-Feb-15 7:01am    
cout<<"|"<<iTerm<<"|" <<iSum= iSum + (4/iDenom)<<"|" <<iError=iSum - iPi<< "|" endl;
else
cout<<"|"<<iTerm<<"|" <<iSum= iSum - (4/iDenom)<< "|" <<iError=iSum - iPi<<"|" endl;

whats wrong with the syntax? :(
[no name] 2-Feb-15 19:30pm    
There is no point in writing it out for you as you need to work this out yourself. As a starting point look for missing <<, rewrite the computations to take them out of the cout lines and fix the mix of floats and ints.
Albert Holguin 2-Feb-15 14:50pm    
OP, see my comment associated with solution 2...
Reuben Cabrera 3-Feb-15 4:30am    
what does OP stand for?
Stefan_Lang 3-Feb-15 6:07am    
OP may stand for "Original Poster", i. e., the person who made the first contribution that started an ongoing discussion. So it means you!

OP may also mean "Original Posting", referring to the original message or question, maybe before being edited. It is often used that way in multi-page forum threads to remind people of the original statement.

It's acos(-1). :-)

—SA
 
Share this answer
 
Comments
Argonia 3-Feb-15 8:42am    
This is evil, pure evil. What if the OP is some 6th grader who is just learning to program and you use such evil words like cos? How you gonna explain to a sixth grader the acos function? Why should he or she see what evil awaits him/her ?
Sergey Alexandrovich Kryukov 3-Feb-15 11:47am    
This is just pure logic you did not bother to use: if someone knows what π is, this person should know about cos and acos. If not, this person does not need π.

Explain? Why? This is not the rules of the game. I answered up the the level implied by the topic of the question. If something is unclear, Reuben can always ask me. Besides, I have no idea who is a 6-grader and who is not, but I think I can reasonably assume that people, no matter on what level of mathematical knowledge, are not morons.

I really, really appreciate any kind of criticism. But I appreciate it if one thinks at least a bit before criticizing.

Thank you,
—SA
Reuben Cabrera 4-Feb-15 8:25am    
thank you.
Sergey Alexandrovich Kryukov 4-Feb-15 15:15pm    
You are very welcome. This is what I often did to get the value of π. Will you accept the answer formally?
You follow-up questions will be very welcome, too.
—SA
Argonia 4-Feb-15 2:40am    
If I remember correctly in the schools (at least when I was in one) we learned about pi way before sin and cos. asin and acos were taught in university.
Pi was never exactly explained what it was, just that it came from nature and we should use it like some magical number who has no end. And circles are far before trigonometry.
Maybe I remember correctly or maybe the education program wasn't good enough. After all not all of us had the opportunity to learn math in Russia.
To use the constant M_PI in c++ we must include the lines:
C++
#define _USE_MATH_DEFINES // for C++
#include <cmath>
at the top of the file. See here:

https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx[^]

So now we can use in this way:

C++
#define _USE_MATH_DEFINES
#include <cmath>
#include<conio.h>
#include <iostream>

using namespace std;

int main()
{
    double myPi = M_PI;

    cout << M_PI << endl;
    cout << myPi << endl;

    _getch();
}
 
Share this answer
 
v2
Comments
Albert Holguin 2-Feb-15 14:50pm    
FYI for the OP... this isn't part of standard C/C++. These aren't included in the gcc version of that header file so don't use them if compiling standard code for use in multiple platforms.
[no name] 2-Feb-15 19:01pm    
Yes the link I gave points that out directly. While I wouldn't use M_PI myself it was the question.
Albert Holguin 2-Feb-15 21:26pm    
Posting for the OP and anyone who may browse the Q&A.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900