-7

I understand that cos(); in c++ uses radians right.. and you can get radians with..

 (angle * PI ) / 180;

So why does

 float value = cos( (90 * PI / 180 ); // == 6.1 etc... and not 0?

If I use the scientific calculator in windows for cos(90) I get zero. Yet as an experiment, when I push cosh(90), I get that same 6.1 etc... value that calling the function in C++ gave me.

Any ideas what is going on? Here is my code as it is now...

http://ideone.com/YQgLz

What I am asking basically is why is cos(90 degrees) in C++ coming back with the same number as doing cosh(90) on the windows calculator. Isn't cos(90 degrees) supposed to be zero anyway?

aJynks
  • 677
  • 2
  • 14
  • 27
  • ... how is this a programming question? –  Sep 21 '12 at 05:27
  • 12
    You *really* got 6.1 for a cosine value? –  Sep 21 '12 at 05:27
  • 1
    it is a programming question as I am trying to work out why cos is not working as I thought it should... here is my code - http://ideone.com/YQgLz – aJynks Sep 21 '12 at 05:30
  • (If so, this must be a *complex* question...) –  Sep 21 '12 at 05:31
  • well if you do it on a calculator you get that as well if you use cosh... .SCREENSHOT - http://tinyurl.com/c22sfjn – aJynks Sep 21 '12 at 05:33
  • You mean cos(pi/2) = 6.1e-17 not 6.1. Google scientific notation. This is **not** a programming question. – talonmies Sep 21 '12 at 05:36
  • @aJynks coincidence. OP is not asking about the hyperbolic cosine, only the normal trigonometric cosine. –  Sep 21 '12 at 05:36
  • @H2CO3 he said, even in the first revision "== 6.1 etc..." Perhaps I'm alone here (11 comment upvotes?), but it's obvious to me what "etc." was a placeholder for. – HostileFork says dont trust SE Sep 21 '12 at 09:58
  • @HostileFork yes, it really is obvious. The sarcasm in my comment was intended to point out the low-quality nature of this question. –  Sep 21 '12 at 10:07
  • @H2CO3 How high and mighty you are, indeed. There's a time and a place for that behavior and I'm sorry you let it out on those more vulnerable than yourself. Guess you don't have kids, nor let them use the internet. – HostileFork says dont trust SE Sep 21 '12 at 10:14
  • @HostileFork Let us not continue to insult each other. (You are right, I don't have children, but if you go to my profile, you will quickly find out why.) –  Sep 21 '12 at 10:18
  • @H2CO3 Ah...I see. Linux user. :-P For technical reasons, there is actually no one on record as the youngest father, although the [youngest mothers have been recorded](http://en.wikipedia.org/wiki/List_of_youngest_birth_mothers). But hey, you live in a great time where you can actually edit the questions to improve them...help and guide...better to light a candle than curse the darkness (as they say over here). – HostileFork says dont trust SE Sep 21 '12 at 10:22

1 Answers1

3

So you didn't really get 6.1 (a cosine/sine value that is greater than 1 is only possible for certain complex numbers), but 6.1 * 10^-17. The thing is that floating-point numbers aren't exact values (by nature - that's how the base-2 representation works), nor do the maths functions return precise values - they use various approximation formulæ to calculate a value - don't ever expect them to be exact.

  • thanks, but the question though is why is cosh on a calculator giving me the same output as my program. And isn't cos(90 degrees) supposed to be 0? – aJynks Sep 21 '12 at 05:35
  • 2
    @aJynks yes, cosine(90deg) is supposed to be 0, but don't you see why it isn't? Just read my answer. Also, cosh() doesn't return the same. It returns 6.1*10^38 and not 6.1*10^-17 - it's just a coincidence that the first two digits of the mantissa are the same. –  Sep 21 '12 at 05:38
  • 1
    @aJynks Sorry to see you have been attacked so hard for an innocent question, it's an aspect of StackOverflow that bothers me. However, the two things you need are the Wikipedia article on [Scientific Notation](http://en.wikipedia.org/wiki/Scientific_notation) and [What every programmer should know about floating point](http://floating-point-gui.de/). The answer to your question is contained in understanding those articles. – HostileFork says dont trust SE Sep 21 '12 at 10:06