Feedback

Quadratwurzel ziehen

Sprache: C#

Hay. Mit diesem Snippet kann man die Quadratwurzel einer Zahl ziehen. Anhand des Heron-Verfahrens. Wir hatten das Thema gerade in der Schule, also nur als Übung.
        public static double Wurzel(double wurzel, int round)
        {
            double a0 = (double) wurzel / 10;
            double b0 = (double) 10;
            while (true)
            {
                a0 = (double)((a0 + b0) / 2);
                b0 = (double)(wurzel / a0);
                        if (Math.Round((a0 % 1), round) == (Math.Round((b0 % 1), round)))
                    {
                        return (double)((int)a0) + Math.Round((a0 % 1), round);
                    }
            }
        }
        public static double Wurzel(double wurzel, int round)
        {
            double a0 = (double) wurzel / 10;
            double b0 = (double) 10;
            while (true)
            {
                a0 = (double)((a0 + b0) / 2);
                b0 = (double)(wurzel / a0);
                        if (Math.Round((a0 % 1), round) == (Math.Round((b0 % 1), round)))
                    {
                        return (double)((int)a0) + Math.Round((a0 % 1), round);
                    }
            }
        }

1 Kommentar