0

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.

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
ninety-ninehundred
  • 126
  • 1
  • 3
  • 8
  • 1
    Welcome to SO! _"...my computer crashes...NO error messages..."_ - can you elaborate? Are you sure? Have you run it in the debugger? This will help us help you. [ask]. Good luck! –  Sep 05 '19 at 05:28
  • 1
    https://stackoverflow.com/questions/5098558/float-vs-double-precision (or any other "why difference between two floating point numbers can't be represented" discussion) should help you. If not clear - add `print(maxLimit - minLimit);` inside of your `while` loop. – Alexei Levenkov Sep 05 '19 at 05:48
  • @MickyD I don't think infinite loop can be described much better... And UI program (like Unity3d used here) would be likely killed at some point due to non-responsiveness... Which is whole other can of worms not exactly related to the question... Likely it would be much easier for OP to start with regular console app and debug it there... – Alexei Levenkov Sep 05 '19 at 05:51
  • @AlexeiLevenkov whilst true, that information was deduced by examining the code. "infinite" and "loop" is no where in the OP's question. I agree about Unity3D, I like to adopt an MVVM pattern and do the bulk of my development outside of Unity. I got the [idea](https://stackoverflow.com/a/42463149/585968) from the makers of _Homeworld: Deserts of Kharak_ :) –  Sep 05 '19 at 06:40
  • Your computer is just likely to crash again when you make your program more elaborate and the compiler has to work harder. Get ahead by fixing the real issue, get a vacuum cleaner, open the case and suck out the dust bunnies. – Hans Passant Sep 05 '19 at 13:47
  • My calculator calculates perfectly fine. Its just that I want to make the computer do less work so I can calculate more decimal points. – ninety-ninehundred Sep 06 '19 at 02:37

0 Answers0