CMATH trig/PI issues

Hey guys!

Very simple,

 
  cout << cos(PI/2) << endl;


Returns:
6.12323e-17

I have
 
const double PI = 3.141592653589793238463;

defined globally.

I assume the issue is my PI is not precise enough to get the answer 0 (instead giving me 0.0000etc)

How could I fix this? Is there a perfect PI?
On my online IDE (I don't know what C version it is, cloud9) cmath does not define PI.
The answer you're getting is pretty darn close to zero. Exactly what kind of precision are you looking for?

(Don't forget that floating-point calculations are all very close approximations. Google "what every computer scientist should know about floating point arithmetic" for more reading.)

Hope this helps.

The answer you're getting is pretty darn close to zero. Exactly what kind of precision are you looking for?


I am using the console output of the application for a presentation (to non-computer science peers), so I would much rather prefer the clean output of "0" rather than "6.12323e-17" (since the cos of pi/2 is 0, after all).

Is there an easy way to round my output as to get "0"?
I should also add:

I realize I could just make a variable to settle this:

int x = cos(PI/2);
cout << x << endl;

Would return 0.

This however, seems like a work-around more than a solution. Do I have to create a temporary variable every time I want something to round? It would work fine for me, but on large programs this seems very messy. I want to form good habits, so if there is another way I would like to know.
you can check the value against an epsilon value:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

const double PI = 3.141592653589793238463;
 
int main() {
	double cos_pi_2 = cos(PI/2);
	
	cout << ((cos_pi_2 < 0.0001 && cos_pi_2 > -0.0001) ? 0 : cos_pi_2) << endl;
}
http://ideone.com/Ewilvh
Last edited on
In response to naraku9333:

Thank you for the response.
You solved the issue.

Unfortunately, I didn't expect things to get this complicated. I thought I was making a simple blooper or something.
In response to keskiverto.

Your answer is completely useless.

You give no explanation to your response. Simply dropping off a link provides no insight.

Going through the assumption that you want me to use setprecision to simplify the answer:

This accomplishes nothing.
The setprecision(3) of 6.12323e-17 is simply 6.12e-17

I would have more respect if you at least tried to explain yourself.
Your answer is completely useless.


keskiverto is a very knowledgeable bloke, I would recommend not shooting him down so badly at this stage.

Realise that people are busy, even though it was only a link, at least he put forward the idea that std::cout can be modified. If you look at the reference for it, you can see other ways of modifying std::cout.

Perhaps you can tone down you comments a bit?
Do you need a non-scientific copy-paste?
1
2
3
4
5
6
7
8
9
10
#include <cmath>
#include <iostream>
#include <iomanip>
 
int main() {
  constexpr double PI = 3.141592653589793238463;
  constexpr auto cos_pi_2 = cos(PI/2);
  std::cout << std::fixed << std::setprecision(0) << cos_pi_2 << '\n';
  return 0;
}

keskiverto is a very knowledgeable bloke, I would recommend not shooting him down so badly at this stage.

Realise that people are busy, even though it was only a link, at least he put forward the idea that std::cout can be modified. If you look at the reference for it, you can see other ways of modifying std::cout.

Perhaps you can tone down you comments a bit?


I do not doubt keskiverto's knowledge, I have no place to in the beginners section of a forum, lol. I also understand he may be busy. But regardless, I don't think dropping unexplained hyperlinks is remotely useful in a beginner forum. Simply not answering would have at least caused less confusion because I wasted 15 minutes trying to decipher what he wanted me to do. I did not guess what he wanted me to do properly and I have no shame about it.

His second reply is very useful, but that does not change my stance on his first response, which, to a beginner, is still useless.

If you still disagree with the way I handled my response that is understandable. Since I don't have anything against you and the topic is already solved, I wouldn't suggest replying; I probably won't revisit this topic and I don't want to give the impression I'm ignoring you.


Actually, nomnum, it is you who has trespassed the norms of proper forum etiquette.

Even if you did not think the answer to be useful, it was, actually, very simple, direct, and correct. It is true that you may not have understood it as such, and such is part of the problem with beginners, but it is the normal modus operandi for people in CS.

If you feel an answer was not helpful to you -- regardless of its actual value -- you can typically ignore it and simply have the grace to be thankful that the poster tried to help.

What you should have done is click the link, read it, and then consider what it may have to do with your problem. Had you done that, you would have discovered that he was referring you to the exact solution to your problem, complete with working example you could have compiled and modified.

For future reference, try not to be so touchy, and enjoy the learning.

Unfortunately, I didn't expect things to get this complicated. I thought I was making a simple blooper or something.
You'll recall that above I did recommend you to google this paper: http://www.validlab.com/goldberg/paper.pdf. I will also qualify that by saying that FP calculations aren't just approximations -- in addition they have surprisingly counter-intuitive behaviors and it trips a lot of people up, even people who have been programming for years.

Hope this helps.
Duoas

On a personal level, ignoring someone's comment seems far more insulting than a curt response, but I will respect the fact that things are different in forum etiquette (etiquettecy is not a word?! O_O)

I did in fact read the link; I couldn't have replied in the way that I did without having read it. The issue was the lack of std::fixed, which would have converted my answer into mere 0 rather than a scientific notation.

I do appreciate the FP link; my program is time-sensitive so the sheer size of it deterred me from reading it at the moment.
Topic archived. No new replies allowed.