Is there a faster and more precise way to do this guess and check square root algorithm. If I push it to calculate into the 7th or 8th decimal point, my computer crashes for overworking. Is there a better square root algorithm other than the built in square root algorithm in the Math class.
This is the script
public class Sqrt_Estimater : MonoBehaviour
{
public float numberToCalculate;
void Start()
{
print(FindSqrt(numberToCalculate));
}
// Update is called once per frame
void Update()
{
}
float FindSqrt (float number)
{
float minLimit = 0;
float maxLimit = number;
float sqrt=0;
if (number < 1)
{
return 0;
}
while ((Mathf.Abs(number - (sqrt * sqrt))>0.000005))
{
sqrt = Random.Range(minLimit, maxLimit);
float guessAmount = 0;
guessAmount = sqrt * sqrt;
if ((Mathf.Abs(number - (sqrt * sqrt)) <0.000005))
{
return sqrt;
}
if (guessAmount > number)
{
maxLimit = sqrt;
}
if (guessAmount < number)
{
minLimit = sqrt;
}
}
return sqrt;
}
}
It would be best if somebody had a guess and check algorithm but any algorithm is fine. Could anybody share their knowledge about this. Thank you.
I haven't tried anything else.
NO error messages.