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

## 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 on other sites
I forgot to add that I just recompiled all of this code in VS2005. The same code previously worked in VS.NET 2003.

##### Share on other sites
Meh. There must be some namespace pollution interfering (no, I didn't do it, and I'm very annoyed).

Thanks anyway to anyone who was scratching their heads.

##### 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 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 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 on other sites
Quote:
 Original post by phil_tDon'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 dofloat 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 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 on other sites
Quote:
 Original post by ZahlmanAs of 1998, it's spelled , 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978697

• 21
• 14
• 12
• 10
• 12