The .NET sqrt function returns double, but I need a decimal value returned (double is not accurate enough) does any one know of a squareroot fucntion that returns a decimal value
thanks in advanced
The .NET sqrt function returns double, but I need a decimal value returned (double is not accurate enough) does any one know of a squareroot fucntion that returns a decimal value
thanks in advanced
Math::sqrt() not cutting it
Marko B. Simic
You might be able to roll your own by wrapping sqrtl();
Which uses a long double
Dan
Jim Perry
Sorry, I opened my big newbie mouth, it seems sqrtl() still returns an 8 byte value which is the same precision you get with a double
Dan
tweetys
The problem is double is the largest floating point type available. If you want to have higher precision then you have to use a different method for storing the values.
What are you trying to calculate the square root of which makes you need such high precision, and why do you need it with that kind of precision.
kjak
Here's some pseudocode that uses the Newton Rhapson method. There's an optimal seed for this, but I don't remember off the top of my head:
decimal sqrt_decimal(decimal inbound, double dNumDecimalsPrecision)
{
// Probably want to check for a negative number, or do you want imaginary results too
double dActualPrecision = 0;
decimal dCurrentEstimate = 2.; // Seed this with something good.
decimal dPreviousEstimate = -1;
decimal dCheckValue = 10 ^ (-dNumDecimalsPrecision);
bool bFound = false;
while (!bFound)
{
// Depending on how fast you want this to go, you may want to throw in a counter and break out if it takes too long.
dPreviousEstimate = dCurrentEstimate;
dCurrentEstimate = dCurrentEstimate - ((dCurrentEstimate ^ 2 - inbound ^ 2)/(2 * dCurrentEstimate));
if (dCheckValue > Abs(dCurrentEstimate - dPreviousEstimate))
{
bFound = true;
}
}
return dCurrentEstimate
}
Jon Watte
Sounds good, how could I go about doing that What is the sqrt algorithim
wEmmay
Hello
Re: Math::sqrt() not cutting it
Please do not post the same question in multiple forums (http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1063426&SiteID=1)
This question is outside the scope of this forum - for the scope of the VC General forum please look at: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=19445&SiteID=1
The other forum is a better match for your question.
OTP
Thanks
Damien