Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why can't the cosine of Pi divided by 2 equal zero?

0 votes
asked Apr 1, 2019 by anonymous
#include <iostream>

#include <cmath>

using namespace std;

int main()

{

const int PI = 3.14159;

cout << "cos(PI / 2) = " << cos(PI / 2) << "\n";

return 0;

}

2 Answers

+1 vote
answered Apr 2, 2019 by tom
because 3.14159 is not an int ... try this :
const double PI = 3.14159;

-Tom.
commented Apr 2, 2019 by anonymous
I tried that. It didn't give me "0" like it should, it just gave me a decimal number like 0.54. I also tried using all 14 data types associated with storing numbers to help out, none of them worked when I used them as constant variables.
commented Apr 3, 2019 by Frankje
If I run this code with a const double, I get 1.32679e-06, which is pretty close to zero (this number stands for 0.00000132679). If you enter a number that is closer to the actual value of pi, with more decimals, you get even closer to zero. (But don't expect to ever get exactly zero; whatever you enter is just an approximation of pi.)
Hope this helps!
commented Apr 5, 2019 by ich ich
i run your code, i get  0.540302 = cos(1)
because int PI = 3.14...  -> PI = 3
and cos(PI/2) -> cos(3/2) -> cos(1) because you are calculating integer values.
0 votes
answered Apr 7, 2019 by Neelima Vechalapu (190 points)
u can try this by using macros i.e. #define pi=3.14
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...