cos gives strange results

Started by
8 comments, last by Fruny 17 years, 9 months ago
Hi u->angle=90.0; float pop=-cos(32*12.3455*u->angle); float slope=-cos(u->angle* 3.1415926535897932384626433832795 / 180.0); --------------------- pop and slope becomes: -0.0641614 4.37114e-008 ------------------- Why is this? Is the value in slope corrupted? If i mult with 100 and cast to int slope is 0 while pop is -6. Thanks Erik
Advertisement
Thos are the same values I get. What results were you expecting?
Fist it was your “My awesome game” post and then your “About boring jokes (my awesome game)” post and now this. Obviously all results are correct. So why do you waste the time of people that are here to help?
Quote:Original post by suliman
Why is this?


Floating point numbers have limited precision.

Quote:Is the value in slope corrupted?


The difference between 4.4e-8 and 0 isn't that great.

Quote:If i mult with 100 and cast to int slope is 0 while pop is -6.


4.4e-8 is 0.000000044
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by suliman
pop and slope becomes:
-0.0641614
4.37114e-008

-------------------

If i mult with 100 and cast to int slope is 0 while pop is -6.

If you cast that float to int then it will equal 0. Going from float to int usually drops the decimals. :/ Also for future reference the "e" in a number like that usually means scientific notation.
Floating point numbers can do some interesting things. For example. here's part of the C++ FAQ Lite: Why is cos(x) != cos(y) even though x == y? (Or sine or tangent or log or just about any other floating point computation).

If you want to know more, the floating point wikipedia page is a good place to start.
Ah i think i get it now. But it's a bit confusing that the compiler mix different modes of representation. Anyways thanks for clearing it out.

@Anonymous Poster
How is this in ANY way related to the "my awesome game" tread? Im sorry if i offended you with that atrocity but please inject some self-distance. Or at least stop posting as anonymous.

Erik
i think i have never really dealt (conciensely) with those e-00X numbers. It all make sense when reading the stuff recommended by you guys. As often, these calculations failed becouse of another change i made earlier. Thats why i though it was corrupt.
Quote:Original post by Anonymous Poster
Fist it was your “My awesome game” post and then your “About boring jokes (my awesome game)” post and now this. Obviously all results are correct. So why do you waste the time of people that are here to help?


This is the beginner's forum. People frequently ask questions with answers that are obvious to others. That is the purpose of this forum, so I wouldn't waste my time flaming people for it.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
This is the beginner's forum. People frequently ask questions with answers that are obvious to others.


And the poster responsible for this anonymous flame has been suspended. Such behaviour is not tolerated in For Beginners.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement