Sign in to follow this  

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

This topic is 4051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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
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
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
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
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

This topic is 4051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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