Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

double float cast

Status
Not open for further replies.

xavstone

Technical User
Apr 12, 2005
33
GB
Hi all,

Can anyone tell me why im getting the error : " Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?) "? The compiler is highlighting the 1.0f but thats a float and i already declared magSq a float so im guessing its something to do with the sqrt method....? Im sure this is something simple but new to c# so please help guys!!! Thanks in advance as always for your help with this. Ben.

public void normalize()
{
float magSq = x * x + y * y + z * z;
if (magSq > 0.0f)//check for divide by zero and zero vectors
{
float oneOverMag = 1.0f / System.Math.Sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
}
 
You're right Sqrt returns a double.
float oneOverMag = 1.0f / (float)System.Math.Sqrt(magSq);

Marty
 
Its impossible to cast a double to a float in c#, yes? Is there a workaround for this?
 
1.0f / System.Math.Sqrt(magSq);
returns a double, a float divided by a double returns a double.
the (float) before your Sqrt operation coverts the return value of Sqrt which is a double to a float.

Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top