Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Calculate the Square Root of a Float in C#

How can I calculate the square root of a Float in C#, similar to Core.Sqrt in XNA?

like image 955
Chris Avatar asked Dec 03 '10 21:12

Chris


People also ask

How do you find the square root in C?

The sqrt() function in C returns the square root of the number. It is defined in the math. h header file.

Does sqrt return a float?

sqrt() method always returns floats, no matter what type you give it as an argument. If you need the result to be an integer, convert it back using int() .

What is sqrt function in C?

In C programming, the sqrt() function is a pre-defined library function used to calculate the square root of a number. The sqrt() function is defined in the math. h header file.

How do you write a program to find the square root of a number in C++?

The sqrt() function in C++ returns the square root of a number. This function is defined in the cmath header file. Mathematically, sqrt(x) = √x .


2 Answers

Since .net core 2.0 you can use MathF.Sqrt.

In older versions, you can calculate it for double and then cast back to float. May be a bit slow, but should work.

(float)Math.Sqrt(inputFloat)
like image 98
CodesInChaos Avatar answered Oct 27 '22 14:10

CodesInChaos


Hate to say this, but 0x5f3759df seems to take 3x as long as Math.Sqrt. I just did some testing with timers. Math.Sqrt in a for-loop accessing pre-calculated arrays resulted in approx 80ms. 0x5f3759df under the same circumstances resulted in 180+ms

The test was conducted several times using the Release mode optimizations.

Source below:

/*
    ================
    SquareRootFloat
    ================
    */
    unsafe static void SquareRootFloat(ref float number, out float result)
    {
        long i;
        float x, y;
        const float f = 1.5F;

        x = number * 0.5F;
        y = number;
        i = *(long*)&y;
        i = 0x5f3759df - (i >> 1);
        y = *(float*)&i;
        y = y * (f - (x * y * y));
        y = y * (f - (x * y * y));
        result = number * y;
    }

    /*
    ================
    SquareRootFloat
    ================
    */
    unsafe static float SquareRootFloat(float number)
    {
        long i;
        float x, y;
        const float f = 1.5F;

        x = number * 0.5F;
        y = number;
        i = *(long*)&y;
        i = 0x5f3759df - (i >> 1);
        y = *(float*)&i;
        y = y * (f - (x * y * y));
        y = y * (f - (x * y * y));
        return number * y;
    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        int Cycles = 10000000;
        Random rnd = new Random();
        float[] Values = new float[Cycles];
        for (int i = 0; i < Cycles; i++)
            Values[i] = (float)(rnd.NextDouble() * 10000.0);

        TimeSpan SqrtTime;

        float[] Results = new float[Cycles];

        DateTime Start = DateTime.Now;

        for (int i = 0; i < Cycles; i++)
        {
            SquareRootFloat(ref Values[i], out Results[i]);
            //Results[i] = (float)Math.Sqrt((float)Values[i]);
            //Results[i] = SquareRootFloat(Values[i]);
        }

        DateTime End = DateTime.Now;

        SqrtTime = End - Start;

        Console.WriteLine("Sqrt was " + SqrtTime.TotalMilliseconds.ToString() + " long");
        Console.ReadKey();
    }
}
like image 29
Devin Avatar answered Oct 27 '22 12:10

Devin