Sign in to follow this  
smitty1276

tan (math.h) doesn't work?!?

Recommended Posts

smitty1276    560
Or am I missing something? This line of code... float temp = tan(1.5); ...results in temp equal to -.69027. According to my calculations, and to every calculator's calculations, it should equal 14.something. Does anyone know what's up with that?

Share this post


Link to post
Share on other sites
erissian    727
Is that the actual code causing the trouble?
Could you post the actual function you're having trouble with, and a little context code from around where you call it?

Share this post


Link to post
Share on other sites
phil_t    8084
Don't you get a warning when compiling that? VC2003 gives me a warning, since there is an implicit conversion from double to float there.

Shouldn't matter... but just for kicks, what happens if you do
float temp = tan(1.5f);

Share this post


Link to post
Share on other sites
Zahlman    1682
As of 1998, it's spelled <cmath>, and it lives in namespace std. Voila, no more namespace pollution, unless someone deliberately put things into namespace std or defined a macro for tan - either of which is grounds for Nasty Disciplinary Action (TM).

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by phil_t
Don't you get a warning when compiling that? VC2003 gives me a warning, since there is an implicit conversion from double to float there.

Shouldn't matter... but just for kicks, what happens if you do
float temp = tan(1.5f);
That wont change anything. 1.5 is already perfectly representable as a float, so there is no loss of precision.
What's more, the result of tan isn't able to be perfectly represented as a float, so you will get a conversion warning there. You should use:
float temp = tanf(1.5);

Share this post


Link to post
Share on other sites
phil_t    8084
Actually
temp float = tan(1.5f);
is fine, because there is an overload of tan with a float as a parameter. That code produces no warnings.

I'm not sure where its being included from though. MSVC won't show me. It doesn't appear to be in math.h though.

edit: ah, ok, it's this line in math.h

inline float __cdecl tan(float _X)
{return (tanf(_X)); }

Share this post


Link to post
Share on other sites
smitty1276    560
Quote:
Original post by Zahlman
As of 1998, it's spelled <cmath>, and it lives in namespace std. Voila, no more namespace pollution, unless someone deliberately put things into namespace std or defined a macro for tan - either of which is grounds for Nasty Disciplinary Action (TM).
Unfortunately, we are integrating a LOT of c code from other people, and this code is theirs. Even though I probably can't make the cmath change myself, I'm going to do it experimentally and see what happens.

Share this post


Link to post
Share on other sites
smitty1276    560
And to answer the question someone asked, this is the code I am looking at at the moment, simply a test case that I could play with...

float temp = std::tan(1.5f);
temp = std::atan(temp);
temp = 0.0f;



I have traced into the tan function call, which passes through a couple of defines from the header, and eventually calls into the actual tan function, which results in temp having a value of -.69... I'm more confused now. It is clearly calling the correct function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this