# having problam using sin and cos functions

## Recommended Posts

hi all, there is a part in my code where i'm using sin and cos functions when i'm compiling i get the next compile error: error C2668: 'sin' : ambiguous call to overloaded function error C2668: 'cos' : ambiguous call to overloaded function anyway here is the code:
int iPower = (int)(sin(iY * iX) * 128 + cos(iY * -iX) * 128);
piSurfaceBuffer[iX + iY * (kLockedRect.Pitch >> 2)] =
D3DCOLOR_XRGB(iPower, iPower, iPower);

do you got any idea of what the problam is? thanks.

##### Share on other sites
What is the type of iX * iY?

##### Share on other sites
Varaibles iX and iY are of type int, so is iX*iY, but sin and cos is overloaded for float and double. The compiler cannot decide which version of sin/cos should be called, since the int can be implicitly converted to a float and to a double.

##### Share on other sites
You should probably also note that sin and cos assume that their argument is in radians, not in degrees.

##### Share on other sites
OK. i changed the iX and iY type to float.
but now i get the next compile error:

error C2108: subscript is not of integral type

this is the related code:
D3DLOCKED_RECT kLockedRect;<...>piSurfaceBuffer[iX + iY * (kLockedRect.Pitch >> 2)] = 		D3DCOLOR_XRGB(iPower, iPower, iPower);

and iPower type is int.

##### Share on other sites
piSurfaceBuffer is an array! and you access a float index inside it.
It's the same as if you wrote: piSurfaceBuffer [5.3f] = 10;

It's also funny that the variables iX and iY are floats even though they start with 'i' in the beginning. Don't change the type of your variables just because of a compiler error, that is the worst solution because it ruins the logic of your algorithm (no matter what you are trying to do there). The type of the variables should match the purpose of your program and NOT the requests of the compiler. The solution I recommend is to keep iX and iY as integers and the first compiler error should be solved by simply casting the iX and iY to floats only inside the sin/cos.

##### Share on other sites
Quote:
 Original post by UriKiller...

This post marks the first time I've rated someone <1000 rating up. Congratulations!

##### Share on other sites
Thanks :)

By The way:
I got my low rating when I've been banned for some time after posting the same post in about all the existing forums in Gamedev. I had no idea that it was forbidden...

##### Share on other sites
also, don't use hungarian notation in variable names. Only stubborn old-schoolers do this. Embedding the type in the variable name means rigid, inflexible code. Instead choose a good name that represents what the data is used for and not what type it happens to currently be.

##### Share on other sites
Quote:
 Original post by UriKillerpiSurfaceBuffer is an array! and you access a float index inside it.It's the same as if you wrote: piSurfaceBuffer [5.3f] = 10;It's also funny that the variables iX and iY are floats even though they start with 'i' in the beginning. Don't change the type of your variables just because of a compiler error, that is the worst solution because it ruins the logic of your algorithm (no matter what you are trying to do there). The type of the variables should match the purpose of your program and NOT the requests of the compiler. The solution I recommend is to keep iX and iY as integers and the first compiler error should be solved by simply casting the iX and iY to floats only inside the sin/cos.

yeah.. that's what i did.
thanks.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628277
• Total Posts
2981774

• 10
• 11
• 17
• 10
• 9