Jump to content
  • Advertisement
Sign in to follow this  
Project-Delta-One

[.net] Square roots in C#?

This topic is 4809 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

A friend of mine wants me to make him a command-line based program that solves this equation that he made (god knows why!) So myquestion is, is there a way to do square roots in C#? this equation which is HUGE has two different times when you need to find the exact sqare root. Ok Thx PDO

Share this post


Link to post
Share on other sites
Advertisement
How would you go about doing it Carmacks way? The code below is in c++, and I'm not skilled enough to do bit shifts in c#
 float InvSqrt (float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well, just add the unsafe keyword ...

I don't know what this code does, but it is not the square root of x ...

Share this post


Link to post
Share on other sites
it's the inverse of the square root of x. You'll have to divide 1 by the result of that function to give you the actual square root of X. Interesting note: division is expensive!

Now, if you are dividing by the square root of some value in your original expression, then Carmack's function is great, you just multiply by its return value instead.

For games, it's nice for normalizing vectors (and judging from the looks of the code, it's probably not 64bit CPU compatible), but that's about it.

Also, instead of these crazy-person casts with pointers and crap, I'd much rather use a union.

union IntFltBitSwap{
int i;
float f;
}
float InvSqrt (float x)
{
IntFltBitSwap a;
float xhalf = 0.5f*x;
a.f = x;
a.i = 0x5f3759df - (a.i >> 1);
a.f = a.f*(1.5f - xhalf*a.f*a.f);
return a.f;
}





edit: bollocks, C# doesn't have unions.

edit2: I've heard this will work to replace the union

[StructLayout(LayoutKind.Explicit)]
public struct IntFltBitSwap
{
[FieldOffset(0)] public int i;
[FieldOffset(0)] public float f;
}



Though floats are evil (I've been burned by their lack of precision too many times), you can't get around it unless A) you use a long instead of an int for the code above, and B) you figure out what 0x5f3759df means and what its 64bit value should be.

[Edited by - capn_midnight on September 14, 2005 12:44:11 PM]

Share this post


Link to post
Share on other sites
Okay, what about....crud. I don't know how to say this because I'm not a math wiz. But, above the radical sign, there is supposed to be a "2" to find the sqare root, but no one puts it there because it is the....default(?) So is there a way to make a variable, that when entered changes that number on top of the radical symbol? sorry if I was unclear. I haven't learned this stuff yet (only in 8th grade) so I'm sorry.

Thank you very much,
PDO

Share this post


Link to post
Share on other sites
Quote:
Original post by Project-Delta-One
Okay, what about....crud. I don't know how to say this because I'm not a math wiz. But, above the radical sign, there is supposed to be a "2" to find the sqare root, but no one puts it there because it is the....default(?) So is there a way to make a variable, that when entered changes that number on top of the radical symbol? sorry if I was unclear. I haven't learned this stuff yet (only in 8th grade) so I'm sorry.

Thank you very much,
PDO


Are you saying you want a root other than the square root? Such as the cube root, or 16th root?

Erm, well you can always translate it into powers:

sqrt(X) = X^(1/2)

Or for any variable:read this

I'm not sure if this is what you want or not, so if not just say so and I'll try to help you.

Using this you can just do this:

double x = Math.pow(x,(1/2));

Note that I didn't test that so you may need to do some changes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gunslinger RR
Note that I didn't test that so you may need to do some changes.


Such as changing that line of code to
double x = Math.pow(x,(1.0/2.0));
so that it doesn't do integer math for 1/2 and round to 0...

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Quote:
Original post by Gunslinger RR
Note that I didn't test that so you may need to do some changes.


Such as changing that line of code to
double x = Math.pow(x,(1.0/2.0));
so that it doesn't do integer math for 1/2 and round to 0...


Yeah, I thought about that last night, but never got to editing it. Thanks for catching that [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!